0

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.

  • Why do you think UART doesn't work? UART is what I would suggest... – Sim Son Apr 17 at 16:41
  • How do I do it. I want to use Arduino IDE to upload but language I found didn't work. Do I need a separate board. Sorry – user1114881 Apr 17 at 16:43
  • You just have to connect rx->tx/tx->rx and look for an example. There are many tutorials about serial communication between arduino and esp8266. – Sim Son Apr 17 at 16:48
  • @user1114881 are you using End of Line and Carraige Return characters at the end of string you are sending? – Vaibhav Apr 17 at 17:53
  • 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 – Sim Son Apr 18 at 4:21
0

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 Nov 8 at 0:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.