0

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

3
  • 1
    Here's a thought: write two small programs, one which sends the data the way you described, one which receives it and tries to get on wifi using it, the way you described (but without all the rest of the ESP32 cam code). If that still fails then share those here instead of taking us on a tour of code fragments. If that doesn't fail you may get a clue as to what's going on. I appreciate that you're trying to help us help you by explaining your code fragments, but two simple complete programs the demonstrate the issue are ultimately a lot more debuggable. Commented Nov 25, 2020 at 4:47
  • 1
    OK so I tried your suggestion. Still hooked up the same way. Tried different baud rates but 115200 works. I will change original post to reflect simplified code. I tried different placements for if(){} code but with no results. However the LED does turn on when the serial is available. Commented Nov 25, 2020 at 22:16
  • So I thought there would be more response after changing things. Maybe later. I have connected up tx of 12e to rx of ESP32-CAM and then the tx of ESP32 to the FTDI and am reading the received data from the 12e to the ESP32. I have changed the code as shown and I get ssidString as the sent XXXXXXXX but the passString is missing the last 4 letters. I'm not sure why this is and how to change it unless the numchar of 32 has something to do with it. Can someone figure this out? Commented Nov 27, 2020 at 18:51

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.