Is there anyway to send a string from an Arduino UNO to a ESP8266-01 (one with only 8 pins RX, TX, CH-PD, vcc, ground, reset, GPIO0, GPIO2) without using without using AT commands. I've looked everywhere and from what I can see UART doesn't work and SoftwareSerial uses only AT commands. I am at my wits end. Can anyone help here? please.
I have tried Serial.write but only writes String and Esp8266 only receives ints.
I found some code I thought might work but so far I can see that the arduino is sending <100.00:200:00>, but on the esp8266 all I get is four backwards question marks
this is the code on the Arduino UNO
#include <SoftwareSerial.h>
SoftwareSerial ESPserial(2, 3); // TX | RX
float water = 100.00;
float battery = 200.00;
void setup()
{
Serial.begin(9600); // communication with the host computer
//while (!Serial) { ; }
// Start the software serial for communication with the ESP8266
ESPserial.begin(57600);
}
void loop()
{
ESPserial.print("<");
ESPserial.print(water,2);
ESPserial.print(":");
ESPserial.print(battery,2);
ESPserial.print(">");
Serial.print("<");
Serial.print(water,2);
Serial.print(":");
Serial.print(battery,2);
Serial.print(">");
delay(10000);
}
and here is the code on the ESP8266
#include <SoftwareSerial.h>
#include <string.h>
SoftwareSerial ESPSerial(2,0);
const byte numChars = 32;
char receivedChars[numChars];
boolean newData = false;
String water;
String battery;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
ESPSerial.begin(57600);
delay(10);
}
void loop() {
// put your main code here, to run repeatedly:
recvWithStartEndMarkers();
showNewData();
water= getValue(receivedChars, ':', 0);
battery = getValue(receivedChars, ':', 1);
Serial.println(water);
}
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (ESPSerial.available() > 0 && newData == false) {
rc = ESPSerial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '0円'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
The Arduino sends writes every 10 seconds and the esp prints the ???? every 10 seconds but with a lag.
I am not sure what the pin out to pin in should be. Right now I have the Arduino 2(TX) attached to the GPIO0 and the Arduino 3(RX) attached to the GPIO2.
Can someone look at my code and see what I am doing wrong. I thought I would send over two values because eventually I want to show water level and battery level.
2 Answers 2
Sending data between Arduino and NodeMcu (Serial Communication)
youtube link (in French):
-
1When you make a video that is entirely in french, please don't write the title entirely in english. Use some french, or write "[french]" in the title or something.timemage– timemage2021年02月27日 17:29:20 +00:00Commented Feb 27, 2021 at 17:29
-
hi .. i make that video to help people to solve the problem.. i forget to ad the language in title .. but the important thing it's the methode (arduino code) using to send the data .. i'm gonna change the title and maybe i will make a video in english .. thank youmohamed daouad– mohamed daouad2021年03月01日 12:24:06 +00:00Commented Mar 1, 2021 at 12:24
-
1I appreciate that there are non-english videos out there =). It's just good that people know what they're going to get, particularly if they're already frustrated by a problem they're having. Voted you up.timemage– timemage2021年03月01日 12:35:51 +00:00Commented Mar 1, 2021 at 12:35
I figured this out. send code is
Arduino Uno side
f = String('<')+String("Hi")+String(',')+String(waterSensorOutValue)+String(',')+String(sensorBatValue)+String('>');
Serial.print(f);
ESP side
const byte numChars = 32; char receivedChars[numChars]; char tempChars[numChars]; // temporary array for use when parsing // variables to hold the parsed data char messageFromPC[numChars] = {0}; float floatFromPC = 0; float floatFromPC2 = 0; boolean newData = false; //end stuff ti bring in string String f; long itt = 500;
long itt2 = 500;
void loop() {
//new stuff string recvWithStartEndMarkers();
if (newData == true) {
strcpy(tempChars, receivedChars);
// this temporary copy is necessary to protect the original data
// because strtok() used in parseData() replaces the commas with 0円
parseData();
showParsedData();
newData = false;
} }
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '0円'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
} }
//============
void parseData() { // split the data into its parts
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(tempChars,","); // get the first part - the string
strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
floatFromPC = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ",");
floatFromPC2 = atoi(strtokIndx); // convert this part to a float
}
//============
void showParsedData() {
Serial.print("Message ");
Serial.println(messageFromPC);
Serial.print("Float ");
Serial.println(floatFromPC);
Serial.print("Float ");
Serial.println(floatFromPC2); } //end new stuff string
Then connect to wifi and upload to ThingSpeak.
I had to get rid of Softwareserial and connect the RX on the Arduino Uno to the TX on the ESP8266-01 and the TX on the Arduino Uno to the RX on the ESP8266-01.
Everything worked great till I changed from an Arduino UNO R3 to an Arduino Pro Mini for size and energy conservation.
With the wiring the same (I think) and the code the same the string (f) doesn't get sent over to the ESP. I don't see the TX led flashing on the Pro Mini when it should be transferring string(f) and nothing gets uploaded to ThingSpeak. Not sure why but I will post another question for that one. Thanks for everyone's help.
-
It is OK to accept your own answer as the solution.VE7JRO– VE7JRO2019年11月08日 00:25:27 +00:00Commented Nov 8, 2019 at 0:25
Arduino 2(TX) attached to the GPIO0 and the Arduino 3(RX) attached to the GPIO2
nope, you have to connect Arduino-TX to Esp-RX and Arduino-RX to Esp-TX. At least the esp8266 has very limited possibilities on gpio0 and gpio2