Issues getting Arduino program to run on ESP01s (ESP8266) - arduino-uno

I am trying to create a server with a simple webpage using the ESP01s module. My pin connections are as follows:
ESP TX to Arduino Uno Tx pin
ESP RX to Arduino Uno Rx pin
ESP VCC to Uno 3.3V pin
ESP GND to Uno GND
ESP EN to Uno 3.3V through 10K resistor
I am using the Arduino IDE and serial to USB cable, when I run a blank program I can send AT commands to the ESP module with no issues.
However when I try to run a script to build and deploy the simple webpage, I either get one of two messages in the IDE serial monitor or nothing:
Unreadable characters - jibberish
Boot instructions
load 0x40100000, len 27728, room 16
tail 0
chksum 0x2a
load 0x3ffe8000, len 2124, room 8
tail 4
chksum 0x07
load 0x3ffe8850, len 9276, room 4
tail 8
chksum 0xba
csum 0xba
No output to serial monitor even though code uploads
This is my code:
#include <SoftwareSerial.h>
SoftwareSerial esp8266(0,1); // rx, tx;
#define serialCommunicationSpeed 115200
#define DEBUG true
void setup()
{
Serial.begin(serialCommunicationSpeed);
esp8266.begin(serialCommunicationSpeed);
InitWifiModule();
}
void loop() {
if (esp8266.available()) {
if (esp8266.find("+IPD,")) {
delay(1000);
int connectionId = esp8266.read() - 48;
String webpage = "<h1>Capstone Group 45: SolarFi</h1>";
String cipSend = "AT+CIPSEND=";
cipSend += connectionId;
cipSend += ",";
cipSend += webpage.length();
cipSend += "\r\n";
sendData(cipSend, 1000, DEBUG);
sendData(webpage, 1000, DEBUG);
String closeCommand = "AT+CIPCLOSE=";
closeCommand += connectionId; // append connection id
closeCommand += "\r\n";
sendData(closeCommand, 3000, DEBUG);
}
}
}
String sendData(String command, const int timeout, boolean debug) {
String response = "";
esp8266.print(command);
long int time = millis();
while ((time + timeout) > millis()) {
while (esp8266.available()) {
char c = esp8266.read();
response += c;
}
}
if (debug) {
Serial.print(response);
}
return response;
}
void InitWifiModule() {
sendData("AT+RST\r\n", 2000, DEBUG);
sendData("AT+CWJAP=\"USERNAME\",\"PASSWORD\"\r\n", 2000, DEBUG);
delay(3000);
sendData("AT+CWMODE=1\r\n", 1500, DEBUG);
delay(1500);
sendData("AT+CIFSR\r\n", 1500, DEBUG);
delay(1500);
sendData("AT+CIPMUX=1\r\n", 1500, DEBUG);
delay(1500);
sendData("AT+CIPSERVER=1,80\r\n", 1500, DEBUG);
}

Related

Slave ESP32 send back wrong data to master esp32

I am currently trying to implement an SPI communication between 2 ESP32 using arduino IDE but i am facing a problem.
I set 1 as a master using the normal SPI library and the second one as slave using ESP32SPISlave.
Master SW is basically writing 0 or 1 to slave device and wait for a response
if the master send a 0 the slave will send back 2 in the next communication,
if the master send 1 the slave will send back 1 in the next communication.
the problem that sometimes even if the master send 1 i receive the number 2 instead of 1 so it seems that it doesn't override the previous value.
I am not sure exactly what's the problem. could you please support me?
I will paste both SW
You can find the picture of the issue in the link below
https://drive.google.com/file/d/1K08ntvOcR7fH9SyWb-RZu7XXCOKP2msH/view?usp=sharing
Master SW
#include <SPI.h>
// Define ALTERNATE_PINS to use non-standard GPIO pins for SPI bus
#ifdef ALTERNATE_PINS
#define VSPI_MISO 26
#define VSPI_MOSI 27
#define VSPI_SCLK 25
#define VSPI_SS 32
#else
#define VSPI_MISO MISO
#define VSPI_MOSI MOSI
#define VSPI_SCLK SCK
#define VSPI_SS SS
#endif
static const int spiClk = 1000000; // 1 MHz
int data1;
//uninitalised pointers to SPI objects
SPIClass * vspi = NULL;
void setup() {
Serial.begin(115200);
delay(2000);
//initialise instance of the SPIClass attached to HSPI
vspi = new SPIClass(VSPI);
//clock miso mosi ss
#ifndef ALTERNATE_PINS
//initialise hspi with default pins
//SCLK = 14, MISO = 12, MOSI = 13, SS = 15
vspi->begin();
#else
//alternatively route through GPIO pins
vspi->begin(VSPI_SCLK, VSPI_MISO, VSPI_MOSI, VSPI_SS); //SCLK, MISO, MOSI, SS
#endif
//set up slave select pins as outputs as the Arduino API
//doesn't handle automatically pulling SS low
pinMode(VSPI_SS, OUTPUT); //HSPI SS
}
// the loop function runs over and over again until power down or reset
void loop() {
vspi_send_command();
delay(1000);
}
void vspi_send_command() {
byte data_on = 0b00000001; // data 1 to turn on LED of slave
byte data_off = 0b0000000; // data 0 to turn off LED of slave
vspi->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0));
digitalWrite(VSPI_SS, LOW);
data1 = vspi->transfer(data_on);
digitalWrite(VSPI_SS, HIGH);
vspi->endTransaction();
Serial.print("data sent is 1 and data received is ");
Serial.println(data1);
delay(1000);
vspi->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0));
digitalWrite(VSPI_SS, LOW);
data1 = vspi->transfer(data_off);
digitalWrite(VSPI_SS, HIGH);
vspi->endTransaction();
Serial.print("data sent is 0 and data received is ");
Serial.println(data1);
delay(1000);
}
Slave SW
#include <ESP32SPISlave.h>
ESP32SPISlave slave;
static constexpr uint32_t BUFFER_SIZE {32};
uint8_t spi_slave_tx_buf[BUFFER_SIZE];
uint8_t spi_slave_rx_buf[BUFFER_SIZE];
#define LED 2
void setup() {
Serial.begin(115200);
delay(2000);
pinMode(LED, OUTPUT);
// begin() after setting
// HSPI = CS: 15, CLK: 14, MOSI: 13, MISO: 12 -> default
// VSPI = CS: 5, CLK: 18, MOSI: 23, MISO: 19
slave.setDataMode(SPI_MODE0);
//slave.begin();
slave.begin(VSPI); // you can use VSPI like this
// clear buffers
memset(spi_slave_tx_buf, 0, BUFFER_SIZE);
memset(spi_slave_rx_buf, 0, BUFFER_SIZE);
}
void loop() {
// block until the transaction comes from master
slave.wait(spi_slave_rx_buf, spi_slave_tx_buf, BUFFER_SIZE);
// if transaction has completed from master,
// available() returns size of results of transaction,
// and buffer is automatically updated
char data;
while (slave.available()) {
// show received data
Serial.print("Command Received: ");
Serial.println(spi_slave_rx_buf[0]);
data = spi_slave_rx_buf[0];
slave.pop();
}
if(data == 1 )
{
Serial.println("Setting LED active HIGH ");
digitalWrite(LED, HIGH);
memset(spi_slave_tx_buf, 1, BUFFER_SIZE);
}
else if(data == 0 )
{
Serial.println("Setting LED active LOW ");
digitalWrite(LED, LOW);
memset(spi_slave_tx_buf, 2, BUFFER_SIZE);
}
Serial.println("");
}
Thank you again for your cooperation.
SPI is a very specific interface and slave can only transmit if the master transmits.
If your slave starts to send a data based on what has been received from the master, then you only get the data from the slave on the 2nd transmission of your master transmission. That is, you need to send two bytes of data from the master, the first byte is the data you want the slave to receive, the second byte is a dummy in order to clock-in the received data.

Saving an image from ESP32-CAM to SD Card

I've try to save an image from ESP32-CAM to SD Card. After uploading the code, i opened the Serial Monitor at a baud rate of 115200, then pressed the ESP32-CAM reset button to turn on ESP32CAM, i got an error like the following. For your information, the memory was formatted to FAT32. Can anyone help?
This is the error output :
Card Mount Failed
#include "esp_camera.h"
#include "Arduino.h"
#include "FS.h" // SD Card ESP32
#include "SD_MMC.h" // SD Card ESP32
#include "soc/soc.h" // Disable brownour problems
#include "soc/rtc_cntl_reg.h" // Disable brownour problems
#include "driver/rtc_io.h"
#include <EEPROM.h> // read and write from flash memory
// define the number of bytes you want to access
#define EEPROM_SIZE 1
// Pin definition for CAMERA_MODEL_AI_THINKER
#define PWDN_GPIO_NUM 32
#define RESET_GPIO_NUM -1
#define XCLK_GPIO_NUM 0
#define SIOD_GPIO_NUM 26
#define SIOC_GPIO_NUM 27
#define Y9_GPIO_NUM 35
#define Y8_GPIO_NUM 34
#define Y7_GPIO_NUM 39
#define Y6_GPIO_NUM 36
#define Y5_GPIO_NUM 21
#define Y4_GPIO_NUM 19
#define Y3_GPIO_NUM 18
#define Y2_GPIO_NUM 5
#define VSYNC_GPIO_NUM 25
#define HREF_GPIO_NUM 23
#define PCLK_GPIO_NUM 22
int pictureNumber = 0;
void setup() {
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); //disable brownout detector
Serial.begin(115200);
//Serial.setDebugOutput(true);
//Serial.println();
camera_config_t config;
config.ledc_channel = LEDC_CHANNEL_0;
config.ledc_timer = LEDC_TIMER_0;
config.pin_d0 = Y2_GPIO_NUM;
config.pin_d1 = Y3_GPIO_NUM;
config.pin_d2 = Y4_GPIO_NUM;
config.pin_d3 = Y5_GPIO_NUM;
config.pin_d4 = Y6_GPIO_NUM;
config.pin_d5 = Y7_GPIO_NUM;
config.pin_d6 = Y8_GPIO_NUM;
config.pin_d7 = Y9_GPIO_NUM;
config.pin_xclk = XCLK_GPIO_NUM;
config.pin_pclk = PCLK_GPIO_NUM;
config.pin_vsync = VSYNC_GPIO_NUM;
config.pin_href = HREF_GPIO_NUM;
config.pin_sscb_sda = SIOD_GPIO_NUM;
config.pin_sscb_scl = SIOC_GPIO_NUM;
config.pin_pwdn = PWDN_GPIO_NUM;
config.pin_reset = RESET_GPIO_NUM;
config.xclk_freq_hz = 20000000;
config.pixel_format = PIXFORMAT_JPEG;
if(psramFound()){
config.frame_size = FRAMESIZE_UXGA; // FRAMESIZE_ + QVGA|CIF|VGA|SVGA|XGA|SXGA|UXGA
config.jpeg_quality = 10;
config.fb_count = 2;
} else {
config.frame_size = FRAMESIZE_SVGA;
config.jpeg_quality = 12;
config.fb_count = 1;
}
// Init Camera
esp_err_t err = esp_camera_init(&config);
if (err != ESP_OK) {
Serial.printf("Camera init failed with error 0x%x", err);
return;
}
//Serial.println("Starting SD Card");
if(!SD_MMC.begin()){
Serial.println("SD Card Mount Failed");
return;
}
uint8_t cardType = SD_MMC.cardType();
if(cardType == CARD_NONE){
Serial.println("No SD Card attached");
return;
}
camera_fb_t * fb = NULL;
// Take Picture with Camera
fb = esp_camera_fb_get();
if(!fb) {
Serial.println("Camera capture failed");
return;
}
// initialize EEPROM with predefined size
EEPROM.begin(EEPROM_SIZE);
pictureNumber = EEPROM.read(0) + 1;
// Path where new picture will be saved in SD Card
String path = "/picture" + String(pictureNumber) +".jpg";
fs::FS &fs = SD_MMC;
Serial.printf("Picture file name: %s\n", path.c_str());
File file = fs.open(path.c_str(), FILE_WRITE);
if(!file){
Serial.println("Failed to open file in writing mode");
}
else {
file.write(fb->buf, fb->len); // payload (image), payload length
Serial.printf("Saved file to path: %s\n", path.c_str());
EEPROM.write(0, pictureNumber);
EEPROM.commit();
}
file.close();
esp_camera_fb_return(fb);
// Turns off the ESP32-CAM white on-board LED (flash) connected to GPIO 4
pinMode(4, INPUT);
digitalWrite(4, LOW);
rtc_gpio_hold_dis(GPIO_NUM_4);
delay(2000);
Serial.println("Going to sleep now");
delay(2000);
esp_deep_sleep_start();
Serial.println("This will never be printed");
}
void loop() {
}
This code works for me. I would suggest that you use a different memory card and if it still doesn't work then erase the flash memory of ESP32 cam.

arduino + serialport communication + ruby

I'm trying to turn on an LED connected to an Arduino from a ruby file, as well as sending a string from that Arduino to my ruby file / terminal. (I'm doing these two things separately, to avoid potential problems).
I'm using a USB port to connect Arduino - Computer, and the serialport gem. I'm working in Linux.
To receive the string on my computer:
I have followed several tutorials that all recommend to run this ruby file:
require 'serialport'
port_str = '/dev/ttyACM0'
baud_rate = 9600
data_bits = 8
stop_bits = 1
parity = SerialPort::NONE
sp = SerialPort.new(port_str, baud_rate, data_bits, stop_bits, parity)
while(true) do
message = sp.gets.chomp
puts message
end
And uploaded this to the Arduino:
uint8_t c;
int i;
void setup() {
Serial.begin(9600); // set baud rate
pinMode(13, OUTPUT); // set pin 13 to be output for LED
digitalWrite(13, LOW); // start with LED off
}
void loop() {
while (Serial.available() > 0) { // check if serial port has data
// writes to computer
Serial.println("Hello world");
}
}
I got this error message: undefined method `chomp' for nil:NilClass (NoMethodError). When getting rid of .chomp, at some point I could get some parts of "Hello world" being printed on my terminal, such as "Hell", and then far later "o w", etc.
Now I don't even get anything anymore.
To turn the led on:
In the ruby file, I replaced the message part by
sp.write('a')
And in the arduino file,
void loop() {
while (Serial.available() > 0) { // check if serial port has data
c = Serial.read(); // read the byte
if (c == 'a'){
digitalWrite(13, HIGH); // turn on LED
delay(500); // wait 500 milliseconds
digitalWrite(13, LOW); // turn off LED
delay(500); // wait 500 milliseconds
}
}
}
The led turns on if I remove the condition (c == 'a'), but obviously I need it if I want the arduino to perform different actions.
I'm new to Serial communication, so I'm not quite sure where the error comes from, since I feel somehow some data seems to be transmitted.

Not able to send a .bin file from computer to the micro-controller via UART, STM32F415

I am trying to send a file(.hex file) from my computer to the micro-controller's internal flash. For time being I am using Hercules terminal to send file. My UART responds to the data sent.
My internal flash memory sector is 128Kbytes and my file is about 50Kbytes , so space is not a problem.
While sending .hex file upto a certain point in file the data gets transferred but after a while it stops . I don't understand why.
To slow it down , I have tried my UART baud rate form 115200 to 2400.
Below is the code:
while(1)
{
i = 0;
int c;
char str[256];
printf("\n> ");
do
{
c = fgetc(stdin);
if(c=='\n')
break;
if(c!=-1)
{
str[i++] = c;
delay(10);
}
}while(1);
//str[i]='\0';
//printf("Got..%s\n",str);
int j = 0;
while(j < i-1)
{
uint64_t data;
uint64_t *pData = (uint64_t*)(str + j); //
//data = *((uint64_t*)&str[i]);
//++pData;
data = *pData;
if (HAL_FLASH_Program(TYPEPROGRAM_BYTE, start_address, data) != HAL_OK) {
HAL_FLASH_Lock();
}else
{
//printf("\nSuccess: Writing a byte at (%x) ==> %c ",start_address,*((char*)&data));
}
delay(10);
//data++;
start_address=start_address+1;
j++;
}
}
Below I am attaching my Hercules terminal image :

Eliminating noise from sensor readings on CC3200?

step by step what I did so far
1) micro controller used CC3200 from Texas instruments ( wifi builted micro controller)
2) Conductive rubber cord stretch sensor - connected to Microcontroller's analog pin
**Sensor's behaviour = ( resistance increases upon stretching the conductive rubber)
So now, following is the code fo reference which I debugged in to the microcontroller to run the sensor(Using energia tool-IDE).
The code is nothing but written for the web server( which I gave- available wifi ssid and password-which you can see in the following programm "iPhone and the passowrd"), where the sensor's code is also wrote in to,
And this webserver reads and generates IP address of the microcontroller and also values of the stretch sensor.
Webserver code :
#include <SPI.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiServer.h>
// which analog pin to connect
#define RUBBERPIN A3
// how many samples to take and average, more takes longer
// but is more 'smooth'
#define NUMSAMPLES 5
// the value of the 'other' resistor
#define SERIESRESISTOR 10000
int samples[NUMSAMPLES];
// your network name also called SSID
char ssid[] = "iPhone";
// your network password
char password[] = "123456789";
// your network key Index number (needed only for WEP)
int keyIndex = 0;
WiFiServer server(3000);
void setup() {
Serial.begin(115200); // initialize serial communication
analogReference(EXTERNAL);
pinMode(RED_LED, OUTPUT); // set the LED pin mode
// attempt to connect to Wifi network:
Serial.print("Attempting to connect to Network named: ");
// print the network name (SSID);
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP
network:
WiFi.begin(ssid, password);
while ( WiFi.status() != WL_CONNECTED) {
// print dots while we wait to connect
Serial.print(".");
delay(300);
}
Serial.println("\nYou're connected to the network");
Serial.println("Waiting for an ip address");
while (WiFi.localIP() == INADDR_NONE) {
// print dots while we wait for an ip addresss
Serial.print(".");
delay(300);
}
// you're connected now, so print out the status
printWifiStatus();
Serial.println("Starting dataerver on port 3000");
server.begin(); // start the web server on port
80
Serial.println("Dataserver started!");
}
void loop() {
// listen for incoming clients
WiFiClient client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
uint8_t i;
float average;
// take N samples in a row, with a slight delay
for (i=0; i< NUMSAMPLES; i++) {
samples[i] = analogRead(RUBBERPIN);
delay(10);
}
// average all the samples out
average = 0;
for (i=0; i< NUMSAMPLES; i++) {
average += samples[i];
}
average /= NUMSAMPLES;
Serial.println(average);
client.println(average);
delay(10);
}
delay(1);
// close the connection:
client.stop();
Serial.println("client disonnected");
}
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("Network Name: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
After running this programm it generates IP address and sensor's values(
221.40
221.20
221.20
*********here = value missing were a noise is visible on graph
221.00
221.20
221.40
221.00
221.20
221.40
221.00
221.40
221.20
221.40
221.20
221.00
221.00
221.60
221.00
221.20
*********here = value missing were a noise is visible on graph
221.20
221.00
Now,
I wrote the generated IP address in to a client programm (In the tool- named processing.org )
Here is my client code
import processing.net.*;
Client c;
String input;
int data[];
int posx;
void setup()
{
size(1000, 500);
background(204);
stroke(0);
frameRate(5); // Slow it down a little
// Connect to the server's IP address and port
c = new Client(this, "192.168.23.2", 3000); // Replace with your server's IP
and port
posx =2;
data = new int[3];
}
void draw()
{
posx++;
// Receive data from server
if (c.available() > 0) {
input = c.readString();
input = input.substring(0, input.indexOf("\n")); // Only up to the newline
println(input);
data[0]=data[1];
data[1] = int(input); // Split values into an array
// Draw line using received coords
stroke(0);
line(posx-1, data[0]+10, posx, data[1]+10);
}
}
My results after running the following programm:
Server sending the data and client receiving the data wirelessly- its all fine
I am able to see the output signal which I am expecting. i.e., when my sensor is in rest position the output must be straight line and if I stretch my sensor the voltage signal must increase- Iam able to see all these. But,
here is a small problem
There is a noise coming out from the output signal (please have a look in to the following picture.)
Noise
So my problem is even when the sensor is in rest position- with out any stretch- there is peak coming out.
please help me

Resources