[英]ESP8266 repeatedly throws Exception(0)
我正在制作一個帶有連接到 Arduino 的稱重傳感器的物聯網秤。 然后 Arduino 將數據發送到通過串行連接的 ESP8266,該 ESP8266 隨后將更新我在 Google Firebase 上的數據庫。 但是,我的 ESP8266 在啟動時拋出異常。 一半的時間它工作得很好。 當我連接串行電纜時,另一半會引發以下錯誤。 這個錯誤反復運行,所以我認為它可能是 void loop() 內部的錯誤,但我似乎無法弄清楚到底是什么問題。 此外,我的電纜質量不是最好的,但我不確定這是否會影響任何事情
Exception (0):
epc1=0x4025803e epc2=0x00000000 epc3=0x00000000 excvaddr=0x0000000a depc=0x00000000
>>>stack>>>
ctx: sys
sp: 3fffe0c0 end: 3fffffb0 offset: 01a0
3fffe260: 00000000 00000000 00000000 00000000
3fffe270: 00000000 00000000 00000000 40104b72
3fffe280: 40104b54 3fffc100 0000001a 00000000
3fffe290: 00000000 4025803e 00000000 00000000
3fffe2a0: 400005e1 00000000 00000000 00000000
3fffe2b0: 4025803e 00000033 00000010 00000000
3fffe2c0: 4025803e 40266fd0 00000000 00000001
3fffe2d0: fbf8ffff 04000002 3feffe00 00000100
3fffe2e0: 0000001a 00000018 04000102 40104b54
3fffe2f0: 3fffc100 0000001a 00000000 00000000
3fffe300: 00000000 00000000 00000000 00000000
3fffe310: 00000000 00000000 00000000 00000000
3fffe320: 00000000 00000000 00000000 00000000
3fffe330: 00000000 00000000 00000000 00000000
3fffe340: 00000000 00000000 00000000 00000000
3fffe350: 00000000 00000000 00000000 00000000
3fffe360: 00000000 00000000 00000000 00000000
3fffe370: 00000000 00000000 00000000 00000000
3fffe380: 00000000 00000000 00000000 00000000
3fffe390: 00000000 00000000 00000000 00000000
3fffe3a0: 00000000 00000000 00000000 00000000
3fffe3b0: 00000000 00000000 00000000 00000000
3fffe3c0: 00000000 00000000 00000000 40104b72
3fffe3d0: 40104b54 3fffc100 0000001a 00000000
3fffe3e0: 00000000 4025803e 00000000 00000000
3fffe3f0: 400005e1 00000000 00000000 00000000
3fffe400: 4025803e 00000033 00000010 00000000
3fffe410: 4025803e 40266fd0 00000000 00000001
3fffe420: fbf8ffff 04000002 3feffe00 00000100
3fffe430: 0000001a 00000018 04000102 40104b54
3fffe440: 3fffc100 0000001a 00000000 00000000
3fffe450: 00000000 00000000 00000000 00000000
3fffe460: 00000000 00000000 00000000 00000000
3fffe470: 00000000 00000000 00000000 00000000
3fffe480: 00000000 00000000 00000000 00000000
3fffe490: 00000000 00000000 00000000 00000000
3fffe4a0: 00000000 00000000 00000000 00000000
3fffe4b0: 00000000 00000000 00000000 00000000
3fffe4c0: 00000000 00000000 00000000 00000000
3fffe4d0: 00000000 00000000 00000000 00000000
3fffe4e0: 00000000 00000000 00000000 00000000
3fffe4f0: 00000000 00000000 00000000 00000000
3fffe500: 00000000 00000000 00000000 00000000
3fffe510: 00000000 00000000 00000000 40104b72
3fffe520: 40104b54 3fffc100 0000001a 00000000
3fffe530: 00000000 4025803e 00000000 00000000
3fffe540: 400005e1 00000000 00000000 00000000
3fffe550: 4025803e 00000033 00000010 00000000
3fffe560: 4025803e 40266fd0 00000000 00000001
3fffe570: fbf8ffff 04000002 3feffe00 00000100
3fffe580: 0000001a 00000018 04000102 40104b54
3fffe590: 3fffc100 0000001a 00000000 00000000
3fffe5a0: 00000000 00000000 00000000 00000000
3fffe5b0: 00000000 00000000 00000000 00000000
3fffe5c0: 00000000 00000000 00000000 00000000
3fffe5d0: 00000000 00000000 00000000 00000000
3fffe5e0: 00000000 00000000 00000000 00000000
3fffe5f0: 00000000 00000000 00000000 00000000
3fffe600: 00000000 00000000 00000000 00000000
3fffe610: 00000000 00000000 00000000 00000000
3fffe620: 00000000 00000000 00000000 00000000
3fffe630: 00000000 00000000 00000000 00000000
3fffe640: 00000000 00000000 00000000 00000000
3fffe650: 00000000 00000000 00000000 00000000
3fffe660: 00000000 00000000 00000000 40104b72
3fffe670: 40104b54 3fffc100 0000001a 00000000
3fffe680: 00000000 4025803e 00000000 00000000
3fffe690: 400005e1 00000000 00000000 00000000
3fffe6a0: 4025803e 00000033 00000010 00000000
3fffe6b0: 4025803e 40266fd0 00000000 00000001
3fffe6c0: fbf8ffff 04000002 3feffe00 00000100
3fffe6d0: 0000001a 00000018 04000102 40104b54
3fffe6e0: 3fffc100 0000001a 00000000 00000000
3fffe6f0: 00000000 00000000 00000000 00000000
3fffe700: 00000000 00000000 00000000 00000000
3fffe710: 00000000 00000000 00000000 00000000
3fffe720: 0af8a400 00000001 647a4f3a 00000000
3fffe730: 0af8a400 00000001 647a4f3a 00000000
3fffe740: 00000000 00000000 00000000 00000000
3fffe750: 00000000 00000000 00000000 00000000
3fffe760: 00000000 00000000 00000000 00000000
3fffe770: 00000000 15f14800 000015f1 00004800
3fffe780: 0000c8f4 000215f1 00000000 00000000
3fffe790: c8f49e74 00009e74 00000000 00000000
3fffe7a0: 00000000 00000000 00000000 00000000
3fffe7b0: 00000000 00000000 00000000 40104b72
3fffe7c0: 40104b54 3fffc100 0000001a 00000000
3fffe7d0: 00000000 4025803e 647a4f3a 00000000
3fffe7e0: 400005e1 00000002 000000ad 00000000
3fffe7f0: 4025803e 00000033 00000010 004e11a5
3fffe800: 4025803e 40266fd0 00000000 00000001
3fffe810: fbf8ffff 04000002 3feffe00 00000100
3fffe820: 0000001a 00000018 04000102 40104b54
3fffe830: 3fffc100 0000001a 00000000 00000000
3fffe840: ffffffff 00000000 00000001 00000000
3fffe850: 00005201 00000000 4026492a ffffffff
3fffe860: 00000000 fffffffd 00000000 00000000
3fffe870: 00000000 00000000 00000001 40006545
3fffe880: 00000000 00000000 00000000 40006545
3fffe890: 00000001 00000001 00000000 40006589
3fffe8a0: 00000000 3fffe922 00000000 40264b1e
3fffe8b0: 40264b32 00000001 00000000 00000000
3fffe8c0: 3fffe8d3 00000000 00000000 40006545
3fffe8d0: 000005e0 3fffe918 3ffef288 40226a30
3fffe8e0: 00000608 00000000 00000000 00000000
3fffe8f0: 00000000 00000020 402532c3 00000001
3fffe900: ffffffff 00000000 3ffe8e91 40104b72
3fffe910: 40104b54 3fffc100 0000001a 00000000
3fffe920: 00000000 4025803e 00000020 401001a4
3fffe930: 400005e1 00000000 00000000 401015bc
3fffe940: 4025803e 00000033 00000010 3ffed11c
3fffe950: 4025803e 40266fd0 00000000 00000001
3fffe960: fbf8ffff 04000002 3feffe00 00000100
3fffe970: 0000001a 00000018 04000102 40104b54
3fffe980: 3fffc100 0000001a 00000000 00000000
3fffe990: 00000000 00000002 3ffee8a0 402660a3
3fffe9a0: c0266089 7fffffff 00000002 40211114
3fffe9b0: 3ffe8765 00000002 3ffee898 4026608a
3fffe9c0: 00000000 7fffffff 3fff17ec 3ffed29c
3fffe9d0: 00000000 3ffeea48 3ffe8765 40222cde
3fffe9e0: 3fffea00 3ffeea1c 3ffe8765 3ffeea78
3fffe9f0: 3ffe8765 3ffeea9c 3ffe8765 40222cde
3fffea00: 3fffea20 3ffeebb4 3ffeeb24 3ffed29c
3fffea10: 3ffe8765 00000000 3ffee678 402063e8
3fffea20: 40247cf1 00000000 3ffeeaa8 402037d8
3fffea30: ffffffff 00000000 3ffeeaa8 402109af
3fffea40: 00000000 00000000 00000001 401001a4
3fffea50: 3ffedffc 3ffee028 3ffe8abc 40104b72
3fffea60: 40104b54 3fffc100 0000001a 00000000
3fffea70: 00000000 4025803e 00000001 402587be
3fffea80: 400005e1 00001000 40104e0d 000003fd
3fffea90: 4025803e 00000033 00000010 00000000
3fffeaa0: 4025803e 40266fd0 00000000 00000001
3fffeab0: fbf8ffff 04000002 3feffe00 00000100
3fffeac0: c_⸮⸮rS⸮
這是我在 ESP8266 上的代碼。 對於任何不良的編程習慣,我深表歉意。
#include <ESP8266WiFi.h>
#include <FirebaseESP8266.h>
//Hidden for security
#define WIFINAME
#define WIFIPASSWORD
#define BAUTRATE 9600
#define FIREBASE_HOST
#define FIREBASE_COMMAND_ADDRESS
#define FIREBASE_AUTH
WiFiClientSecure client;
FirebaseData firebaseData;
unsigned long previousTime;
String firebaseWriteAddress;
String firebaseReadAddress;
String weight; // Stored in a string to accomodate the "loading" text
bool maxWRead;
bool minWRead;
bool displayPrefRead;
bool instructedToConnectWifi;
void readWeights(){
// function to read the min and max weights, then send to Arduino
if (!maxWRead){
String maxWAddress = firebaseReadAddress + "/Product/MaxW";
if (Firebase.get(firebaseData,maxWAddress)){
if (firebaseData.dataType() == "string"){
String maxWMessage = "max:"+ firebaseData.stringData();
Serial.println(maxWMessage);
maxWRead = true;
}
}
}
if (!minWRead){
String minWAddress = firebaseReadAddress + "/Product/MinW";
if (Firebase.get(firebaseData,minWAddress)){
if (firebaseData.dataType() == "string"){
String minWMessage = "min:"+ firebaseData.stringData();
Serial.println(minWMessage);
minWRead = true;
}
}
}
}
void readDisplayPref(){
String displayAddress = firebaseReadAddress + "/Display";
if (Firebase.get(firebaseData,displayAddress)){
if (firebaseData.dataType() == "string"){
String displayMessage = "dsp:"+ firebaseData.stringData();
Serial.println(displayMessage);
displayPrefRead = true;
}
}
}
void sendFireBase(){
// function to send data to Firebase Real-time Database
Firebase.setString(firebaseData, firebaseWriteAddress,weight);
}
void readFireBase(){
Firebase.begin(FIREBASE_HOST,FIREBASE_AUTH);
if(Firebase.get(firebaseData, FIREBASE_COMMAND_ADDRESS)){
if (firebaseData.dataType() == "string"){
String data = firebaseData.stringData();
String command = data.substring(0,4);
if (command == "read"){
firebaseReadAddress = "/UserData/" + data.substring(4);
readWeights();
firebaseWriteAddress = "/UserData/" + data.substring(4)
+ "/Weight";
}
}
}
}
void connectWifi(){
WiFi.begin(WIFINAME, WIFIPASSWORD);
byte wifiCounter = 0;
// Allow some buffer time to connect to WiFi
while (WiFi.status() != WL_CONNECTED) {
delay(100);
wifiCounter ++;
if (wifiCounter > 50){
// tries for 5 sec.
break;
}
}
if (WiFi.status() == WL_CONNECTED){
Serial.print("WiFi Connected!\n");
instructedToConnectWifi = true;
}
else {
Serial.print("Can't Connect. Trying Again\n");
connectWifi();
}
}
void readArd(){
// function to read Serial inputs from arduino
String message = Serial.readStringUntil('\n');
if (message == "ConnectWifi"){
connectWifi();
}
else{
weight = message;
// stores weight without the percentage sign. Only stores if its within 0-100
sendFireBase();
}
}
void setup() {
Serial.begin(BAUTRATE);
previousTime = millis();
maxWRead = false;
minWRead = false;
displayPrefRead = false;
instructedToConnectWifi = false;
}
void loop() {
if (Serial.available()){
readArd();
}
if (instructedToConnectWifi){
// if wifi disconnects halfway, auto connect back
if (WiFi.status() != WL_CONNECTED){
connectWifi();
}
}
unsigned long currentTime = millis();
if (currentTime - previousTime >2000){
// cycles every 2 sec to prevent spam
// TODO: fix overflow problem (49 days) if it becomes product
if (instructedToConnectWifi){
if (!minWRead || !maxWRead){
// stop once max and min has been read
readFireBase();
previousTime = currentTime;
}
else {
readDisplayPref();
previousTime = currentTime;
}
}
}
}
這些例外分析給了我以下結果:
Exception 0: Illegal instruction
深入研究錯誤發現:
std::basic_string , std::allocator >::basic_string(char const*, std::allocator const&)
此錯誤模式對於 String 類是典型的。
用固定字符數組替換所有字符串(不是動態創建的(!))
原因:String 類傾向於破壞堆,導致 ESP 崩潰(8266 以及 ESP32)。
為什么是那些? 因為我們使用它們(與 WiFi 場景(IoT)中 99% 的“普通”Arduinos 相反)。所以在內存中發生了很多事情。
該怎么辦? 擺脫 String 類(和 delay() 如果使用)
String firebaseWriteAddress;
String firebaseReadAddress;
String weight; // Stored in a string to accomodate the "loading" text
以上這些與函數(動態定義的字符串變量)結合使用
String data = firebaseData.stringData();
String command = data.substring(0,4);
if (command == "read"){
firebaseReadAddress = "/UserData/" + data.substring(4);
readWeights();
firebaseWriteAddress = "/UserData/" + data.substring(4)
+ "/Weight";
將確保 ESP 非常快速地崩潰。 由於沒有垃圾收集(並且無論如何都沒有足夠的內存),您在某個時候寫入被占用的內存 - 請參閱我的錯誤分析的第一行 - 崩潰。
為什么要使用固定字符? - 它們被編譯到閃存中,並且您大部分時間都使用指針進行操作。 所以所有“固定”的短信進入閃存(示例)
char firebaseReadAddress[64] = {'\0'}; //Takes 63 chars
用過
strcpy(firebaseReadAddress, "/UserData/");
strcat(firebaseReadAddress, dataContent);
要檢查字符數組是否以“read”開頭,請使用以下方法:
if (strncmp( command, "read", 4) == 0) { /** Compare the first 4 characters - returns 0 if they are equal */
處理和 String 類一樣簡單,但它可以讓您的應用程序永遠運行(至少只要有電源)
檢查還使用了大量字符串使用的庫(特別是如果它們不是專用於 ESP 的)。 如果他們這樣做 - 放棄它們或重寫它們;-)
firebase 庫是一個積極的例子,沒有使用動態字符串和大量使用字符數組。
關於這些壞消息我很遺憾
如果您想要一個穩定的應用程序,請重寫您的程序,如果有特殊問題,我可以幫助您 - 只需在此處發表評論。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.