Problem about data from uno to nodemcu

Hi guys,
I want the data received by uno to be sent to nodemcu and finally uploaded to aws dynamo db for storage. But in serial monitor show:

dhcp client start...
ip:172.20.10.3,mask:255.255.255.240,gw:172.20.10.1
.
WiFi connected
IP address: 
172.20.10.3
Heap: 40008
Success to open cert file
cert loaded
Success to open private cert file
private key loaded
Success to open ca
ca loaded
Heap: 36416
pm open,type:2 0

Soft WDT reset

>>>stack>>>

ctx: cont
sp: 3ffffc90 end: 3fffffc0 offset: 01b0
3ffffe40:  004fafef 3fff15e4 3ffeeea4 00000043  
3ffffe50:  00000000 00000000 3ffeeef0 00000043  
3ffffe60:  00000000 3ffeeea4 00000000 4020bff7  
3ffffe70:  514d0400 00045454 000006dc 3ffef20c  
3ffffe80:  00000000 00000000 00000000 00000001  
3ffffe90:  3fff2f1c 00000000 3ffe8514 3ffef330  
3ffffea0:  3fffdad0 00000000 3ffeeea4 4020c098  
3ffffeb0:  00000000 00000000 00000001 4020d8c6  
3ffffec0:  00000000 00000000 00000010 402077cc  
3ffffed0:  3fff1aa4 3fff27dc 33000000 3ffe874d  
3ffffee0:  3ffef1e8 000003ff 000003ff 40100814  
3ffffef0:  3ffeeef0 3ffef354 3fff186c 40100c5c  
3fffff00:  3ffe8ad4 00000345 3fff2ba4 402170d5  
3fffff10:  3ffeeef0 3ffef354 3fff1914 4020da77  
3fffff20:  4020ca04 000003e8 3fff186c 40207554  
3fffff30:  3ffeeef0 3ffef354 3ffef20c 40207767  
3fffff40:  4020f398 00000000 000003e8 feefeffe  
3fffff50:  3fff27dc 3fff2b8c 4020f398 00000000  
3fffff60:  000003e8 feefeffe 3fff24bc 3fff1bdc  
3fffff70:  4020f398 00000000 000003e8 feefeffe  
3fffff80:  3fff1914 3fff186c feefeffe feefeffe  
3fffff90:  feefeffe feefeffe feefeffe 3ffef330  
3fffffa0:  3fffdad0 00000000 3ffef300 4020d894  
3fffffb0:  feefeffe feefeffe 3ffe8514 40100cf1  
<<<stack<<<

I tried to adjust flash size and add yield(); to my function
But still error.
How do I deal with it?

#include "FS.h"
#include <ESP8266WiFi.h>;
#include <PubSubClient.h>;
#include <NTPClient.h>;
#include <WiFiUdp.h>;
#include <SoftwareSerial.h> //軟體序列輔助檔

unsigned long previousMillis = 0;        // will store last temp was read
const long interval = 2000;              // interval at which to read sensor

// Update these with values suitable for your network.

const char* ssid = "webduino.io"; //填入網路id
const char* password = "webduino"; //填入網路密碼
SoftwareSerial NodeMCU(D2,D3); //定義和UNO版連線的軟體序列位置
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org");

const char* AWS_endpoint = "endpoint.amazonaws.com"; //填入 aws endpoint

void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
  Serial.print((char)payload[i]);
}
  Serial.println();
}

WiFiClientSecure espClient;
PubSubClient client(AWS_endpoint, 8883, callback, espClient); //set MQTT port number to 8883 as per //standard
long lastMsg = 0;
char msg[50];
int value = 0;

void setup_wifi() {

delay(10);
// We start by connecting to a WiFi network
espClient.setBufferSizes(512, 512);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
  delay(500);
  Serial.print(".");
}

Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());

timeClient.begin();
while(!timeClient.update()){
timeClient.forceUpdate();
}

espClient.setX509Time(timeClient.getEpochTime());
}

void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESPthing")) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("outTopic", "hello world");
// ... and resubscribe
client.subscribe("inTopic");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");

char buf[256];
espClient.getLastSSLError(buf,256);
Serial.print("WiFiClientSecure SSL error: ");
Serial.println(buf);

// Wait 5 seconds before retrying
delay(5000);
}
}
}

void setup() {
  NodeMCU.begin(9600);      //軟體序列的鮑率
  pinMode(D2,INPUT); //D2為RX,D3為TX
  pinMode(D3,OUTPUT);
  Serial.begin(9600);
  Serial.setDebugOutput(true);
// initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
  setup_wifi();
  delay(1000);
  if (!SPIFFS.begin()) {
    Serial.println("Failed to mount file system");
    return;
  }

  Serial.print("Heap: "); Serial.println(ESP.getFreeHeap());

// Load certificate file
File cert = SPIFFS.open("/cert.der", "r"); //replace cert.crt eith your uploaded file name
if (!cert) {
Serial.println("Failed to open cert file");
}
else
Serial.println("Success to open cert file");

delay(1000);

if (espClient.loadCertificate(cert))
Serial.println("cert loaded");
else
Serial.println("cert not loaded");

// Load private key file
File private_key = SPIFFS.open("/private.der", "r"); //replace private eith your uploaded file name
if (!private_key) {
Serial.println("Failed to open private cert file");
}
else
Serial.println("Success to open private cert file");

delay(1000);

if (espClient.loadPrivateKey(private_key))
Serial.println("private key loaded");
else
Serial.println("private key not loaded");

// Load CA file
File ca = SPIFFS.open("/ca.der", "r"); //replace ca eith your uploaded file name
if (!ca) {
Serial.println("Failed to open ca ");
}
else
Serial.println("Success to open ca");

delay(1000);

if(espClient.loadCACert(ca))
Serial.println("ca loaded");
else
Serial.println("ca failed");

Serial.print("Heap: "); Serial.println(ESP.getFreeHeap());
}

void loop() {
yield();
if (client.connect("CHANGE_THIS_IT_HAS_TO_BE_DIFFERENT_FOR_EACH_DEVICE")){
reconnect();
}
client.loop();

long now = millis();
if (now - lastMsg > 2000) {
  lastMsg = now;
  ++value;
  yield();
//gettemperature();       // 讀取感測器資料
       
  char Nums[50]; //定義通訊讀資料的矩陣
  while(NodeMCU.available()){ //如果有資料從UNO來
       NodeMCU.readBytes(Nums,30);  } //就讀23個字元
       String my_string;
       my_string= String(Nums);   //將矩陣整個轉成字串
       Serial.println(my_string); //印出字串    
       //以下是將字串分割的程式,利用","割字元
               int delimiter, delimiter_1, delimiter_2, delimiter_3, delimiter_4,delimiter_5;
               delimiter = my_string.indexOf(","); //my_string第一次出現","的位址
               delimiter_1 = my_string.indexOf(",", delimiter + 1);//my_string第二次出現","的位址
               delimiter_2 = my_string.indexOf(",", delimiter_1 +1);//my_string第三次出現","的位
               delimiter_3 = my_string.indexOf(",", delimiter_2 +1);//my_string第三次出現","的位
               delimiter_4 = my_string.indexOf(",", delimiter_3 +1);//my_string第三次出現","的位
                delimiter_5 = my_string.indexOf(",", delimiter_4 +1);//my_string第三次出現","的位
// Define variables to be executed on the code later by collecting information from the my_string as substrings
               String pm25 = my_string.substring(delimiter + 1, delimiter_1);
               String temperature = my_string.substring(delimiter + 1, delimiter_1);//第二個子字串pm25就是在第二及第三個","之間的字元
               String humidity = my_string.substring(delimiter_1 + 1, delimiter_2);
               String lux = my_string.substring(delimiter_3 + 1, delimiter_4);
               String hcho = my_string.substring(delimiter_4 + 1, delimiter_5);
snprintf (msg,75, "{\"pm25\":%04d,\"temp\": %.2lf,\"humid\":%.2lf,\"lux\":%05d,\"hcho\":%.3lf}",pm25.toInt(),temperature.toFloat(),humidity.toFloat(),lux.toInt(),hcho.toFloat());

//sizeof(msg)
Serial.print("Publish message: ");
Serial.println(msg);
client.publish("device/302/data", msg);

Serial.print("Heap: "); Serial.println(ESP.getFreeHeap()); //Low heap can cause problems
 }
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(100); // wait for a second
}

Was there a message before the stack dump telling you why you were getting a stack dump?

Hi @1j_0000,

you should re-edit your first post to have your code as a code-section.
This can be done with a few mous-clicks.

Below your posting you find a pencil-icon that will activate editing-mode

Click on this icon.
Then mark all the code. Then delete the marked code.

You can post code by using this method that adds the code-tags

There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

best regards Stefan

1 Like

This picture from the same serial monitor is before the stack message.

By the way
When I use the same code with MQ137,dht11 to be ok
but when I use pms5003 with this code to pop up this serial monitor message
and I also try this function about waiting minutes to data reset ,still the same situation.

You should post the content of the serial monitor as a code-section

simply click into the window of the serial monitor
press Ctrl-A, to mark all content in the serial monitor
press Ctrl-C on your keyboard to copy to clipboard

click the insert code-icon </> in the forum
which will create these lines

press Ctrl-V to paste clipboard-content.

As the Soft Watch Dog Timer WDT is biting
you should look into your code what does your code below printing
"pm open, type 2 0"

there your code is busy with something that endures too long

You should add a few hardcoded and unique characters to your serial printing
to make it easy to find the place inside your code where this printed line comes from

best regards Stefan

1 Like

Can you tell me more details about " add a few hardcoded and unique characters"? I don't understand these words. I tried yield(); in my loop functions or delay(); but still can't work to upload to aws. T_T
By the way, when I take notes about client code , nodemcu can receive the data from uno.

Publish message: {"pm25":0009,"temp": 24.40,"humid":32.40,"lux":00078,"hcho":0.017}
,0009,24.40,32.40,00078,0.017,!@⸮⸮⸮?⸮⸮⸮?⸮⸮?{⸮ @p⸮ @
Publish message: {"pm25":0009,"temp": 24.40,"humid":32.40,"lux":00078,"hcho":0.017}
,0009,24.40,32.40,00078,0.017,!@⸮⸮⸮?⸮⸮⸮?⸮⸮?{⸮ @p⸮ @
Publish message: {"pm25":0009,"temp": 24.40,"humid":32.40,"lux":00078,"hcho":0.017}
,0009,24.40,32.40,00078,0.017,!@⸮⸮⸮?⸮⸮⸮?⸮⸮?{⸮ @p⸮ @
Publish message: {"pm25":0009,"temp": 24.40,"humid":32.40,"lux":00078,"hcho":0.017}
,0009,24.40,32.40,00078,0.017,!@⸮⸮⸮?⸮⸮⸮?⸮⸮?{⸮ @p⸮ @
Publish message: {"pm25":0009,"temp": 24.40,"humid":32.40,"lux":00078,"hcho":0.017}

This is an example

The screenshot of the serial monitor shows a line

heap: ....

this comes from this line of code

Serial.print("Heap: "); 

next line in the serial monitor is
pm open, type: 2 0

If I try to search for the characters "pm open, type: 2 0"

like "pm open"
or
"type:"

they are not found in your code

adding hardocded serial.prints like

Serial.print("my marker1"); 

a few lines of code further down

Serial.print("my marker2"); 

etc. etc. etc.

will help to find the place where in your code the Soft WDT reset occurs

best regards Stefan

Sorry, You means that I will add "Serial.print("pm open,type2 0);" in my code and try the different line of code to know where the Soft WDT reset occurs, right?

sigh !
no!

at this point I recommend that you lay away your actual project and do either

  • buy a ready to use product that does the same thing
  • learn the basics about programming in general and learn what libraries are and how the work.

As I see it: The gaps in your knowledge are too big to go on with this project directly. You should go back to smaller exercises until you at least understand what Serial.print() in detail does.

best regards Stefan

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.