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
}
