ESP-12e 和 ESP32-CAM 之间的串行通信数据
Serial communicate data between an ESP-12e and ESP32- CAM
我有一个 ESP8266-12e 和一个 ESP32-CAM。我正在尝试通过串行连接将数据从 12e 发送到 ESP32。我已将 12e 的 TX 连接到 ESP32 的 RX,将 12e 的 RX 连接到 ESP32 的 TX。它们都由相同的 3.3v 电源为 12E 和 5v 为 ESP32 供电。共同点。
我都设置了115200的波特率。
这是 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:
}
这是 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 [=11=]
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] = '[=11=]'; // 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);
}
正如我所说,我尝试将条件代码放在不同的地方。我尝试使用 ssidString 和 pskString 的直接编码。没有改善。
我希望这会有所帮助,有人可以帮助我解决这个问题。
谢谢
所以我终于通过将以下部分更改为我上次发布的代码来完成此工作:
const byte numChars = 100;
char ssidString[numChars] = {0};
char pskString[numChars] = {0};
现在效果很好。
我有一个 ESP8266-12e 和一个 ESP32-CAM。我正在尝试通过串行连接将数据从 12e 发送到 ESP32。我已将 12e 的 TX 连接到 ESP32 的 RX,将 12e 的 RX 连接到 ESP32 的 TX。它们都由相同的 3.3v 电源为 12E 和 5v 为 ESP32 供电。共同点。
我都设置了115200的波特率。
这是 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:
}
这是 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 [=11=]
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] = '[=11=]'; // 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);
}
正如我所说,我尝试将条件代码放在不同的地方。我尝试使用 ssidString 和 pskString 的直接编码。没有改善。 我希望这会有所帮助,有人可以帮助我解决这个问题。 谢谢
所以我终于通过将以下部分更改为我上次发布的代码来完成此工作:
const byte numChars = 100;
char ssidString[numChars] = {0};
char pskString[numChars] = {0};
现在效果很好。