Search code examples
stringcharserial-portesp8266esp32

Serial communicate data between an ESP-12e and ESP32- CAM


I have an ESP8266-12e and an ESP32-CAM. I am trying to send data from the 12e to the ESP32 through serial connection. I have connected TX of 12e to RX of ESP32 and RX of 12e to TX of ESP32. They are both powered by the same source 3.3v for 12E and 5v for ESP32. A common ground.

I have both set to a baud rate of 115200.

This is the code for the ESP-12e

include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>  


  char ssid[] = "XXXXXXXX";
  char pass[] = "OOOOOOOO";   

  WiFiClient client;

  String f;
void setup() {
  Serial.begin(115200);
  WiFiManager wifiManager;
WiFi.begin(ssid, pass);
delay(5000);
 if (WiFi.status() == WL_CONNECTED) {  Serial.println("Connected");
delay(1000);}
  Serial.printf("SSID: %s\n", WiFi.SSID().c_str());
Serial.printf("PSK: %s\n", WiFi.psk().c_str());
String ssidString = WiFi.SSID().c_str();
String pskString = WiFi.psk().c_str();
    f = String('<')+String("Hi")+String(',')+String(ssidString)+String(',')+String(pskString)+String('>');
    delay (1000);
 Serial.print(f);

delay(500);

Serial.print(f);
delay(500);
Serial.print(f);
delay(500);
Serial.print(f);
delay(500);
Serial.print(f);
delay(500);
Serial.print(f);
delay(500);
Serial.print(f);
delay(500);
Serial.print(f);
delay(500);
Serial.print(f);
delay(500);
Serial.print(f);
delay(500);
Serial.print(f);
delay(500);
Serial.print(f);
delay(500);
Serial.print(f);
delay(500);
Serial.print(f);
delay(500);
Serial.print(f);
delay(500);
Serial.print(f);
delay(500);
Serial.print(f);
delay(500);
Serial.print(f);
delay(500);
Serial.print(f);
delay(500);
Serial.print(f);
delay(500);
Serial.print(f);
delay(500);
Serial.print(f);
delay(500);
Serial.print(f);
delay(500);
Serial.print(f);
delay(500);
Serial.print(f);
delay(500);
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

And this is the code for the ESP32-CAM

#include <WiFi.h>



const byte numChars = 32; //COULD THIS HAVE SOMETHING TO DO WITH IT
char receivedChars[numChars];
char tempChars[numChars];        // temporary array for use when parsing
// variables to hold the parsed data
char messageFromPC[numChars] = {0};

//CHANGED THIS char ssidString[] = "";  TO THIS
char ssidString[numChars] = {0};
//CHANGED THIS char ssidString[] = "";  TO THIS
char pskString[numChars] = {0};
char myChar[]= "XXXXXXXX";
char myPsk[]= "OOOOOOOO";
boolean newData = false;
//end stuff ti bring in string
String f;


void setup() {
   Serial.begin(115200);

  pinMode(33,OUTPUT);
 digitalWrite(33,HIGH);
  //while (!Serial); 
  //wait for serial connection.
delay(5000);
if(Serial.available()){

  digitalWrite(33,LOW);
  delay(2000);
  
  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;
    }

    Serial.print("network");
    Serial.print(ssidString);
    Serial.print("pass");
    Serial.print(pskString);
 
}

    delay(5000);
    
    if(pskString == "OOOOOOOO"){
digitalWrite(33,HIGH);
}
delay(1000);
if(ssidString == "Gary's Wi-Fi Network"){
digitalWrite(33,LOW);
}
delay(1000);

/*if(ssidString=="");{
  digitalWrite(33,LOW);
  delay(2000);
  digitalWrite(33,HIGH);
}*/

#define SSID1 ssidString//"XXXXXXXX"
#define PWD1 pskString//"Homenetwork"
  // put your main code here, to run repeatedly:
   WiFi.begin(ssidString, pskString);
   delay(5000);
 if (WiFi.status() == WL_CONNECTED) {  digitalWrite(33,HIGH);
delay(1000);}

  // put your setup code here, to run once:

}

void loop() {

  

}


//more new stuff for string
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
    strcpy(ssidString,strtokIndx);
    

    strtokIndx = strtok(NULL, ",");
    strcpy(pskString,strtokIndx);
    

  

}

//============

void showParsedData() {
    Serial.print("Message ");
    Serial.println(messageFromPC);
    Serial.print("ssid");
    
    Serial.println(ssidString);
    Serial.print("psk");
    
    Serial.println(pskString);
  
}

Like I said I tried putting the conditions code in different places. I tried using direct coding of ssidString and pskString. With no improvement. I hope this helps and someone can help me figure this out. Thanks


Solution

  • So I finally got this working by changing the following part to my last posted code:

    const byte numChars = 100;
    char ssidString[numChars] = {0};
    char pskString[numChars] = {0};
    

    Works great now.