Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tb.connect crashes on Lolin(Wemos) D1 mini pro board #145

Closed
hertell opened this issue Aug 20, 2023 · 9 comments
Closed

tb.connect crashes on Lolin(Wemos) D1 mini pro board #145

hertell opened this issue Aug 20, 2023 · 9 comments

Comments

@hertell
Copy link

hertell commented Aug 20, 2023

Hi,

I having trouble with using the 0.10.2 library with a Lolin (Wemos) D1 mini Pro (https://iotappstory.com/hardware/ESP8266/boards/lolinwemos-d1-mini-pro). The following line crashes the board

tb.connect(thingsboardServer, TOKEN)

with the following dump

16:23:33.425 -> ......Connected to AP
16:23:35.916 -> Connecting to ThingsBoard node ...
16:23:35.948 -> --------------- CUT HERE FOR EXCEPTION DECODER ---------------
16:23:35.948 -> 
16:23:35.948 -> Exception (3):
16:23:35.948 -> epc1=0x40222856 epc2=0x00000000 epc3=0x00000000 excvaddr=0x4027063b depc=0x00000000
16:23:35.948 -> 
16:23:35.948 -> >>>stack>>>
16:23:35.948 -> 
16:23:35.948 -> ctx: cont
16:23:35.980 -> sp: 3ffffd60 end: 3fffffc0 offset: 0190
16:23:35.980 -> 3ffffef0:  00000000 3ffe8b18 00000000 4020c39c  
16:23:35.980 -> 3fffff00:  514d0400 73045454 00647773 40209c40  
16:23:35.980 -> 3fffff10:  00000000 4027063b 00000000 00000000  
16:23:35.980 -> 3fffff20:  00000001 00000040 3fff07e4 4020c7b8  
16:23:35.980 -> 3fffff30:  4020c7ac 3ffe8af5 3fff07e4 00000000  
16:23:35.980 -> 3fffff40:  3fff07a6 3fff07e4 3fff04c8 4020c584  
16:23:36.013 -> 3fffff50:  00000000 00000000 00000001 4020cad0  
16:23:36.013 -> 3fffff60:  3fffdad0 00000000 3fff04c8 402061c2  
16:23:36.013 -> 3fffff70:  3fff23e4 4020af0c 3fff0924 4020cb24  
16:23:36.013 -> 3fffff80:  4020eb91 3fff04c8 3fff0910 3fff0924  
16:23:36.013 -> 3fffff90:  3fffdad0 00000000 3fff0910 40208648  
16:23:36.013 -> 3fffffa0:  feefeffe feefeffe 3fff0910 4020ddbc  
16:23:36.013 -> 3fffffb0:  feefeffe feefeffe 3ffe870c 40100f9d  
16:23:36.013 -> <<<stack<<<
16:23:36.013 -> 
16:23:36.046 -> --------------- CUT HERE FOR EXCEPTION DECODER ---------------
16:23:36.046 -> 
16:23:36.046 ->  ets Jan  8 2013,rst cause:2, boot mode:(3,6)
16:23:36.046 -> 
16:23:36.079 -> load 0x4010f000, len 3460, room 16 
16:23:36.079 -> tail 4
16:23:36.079 -> chksum 0xcc
16:23:36.079 -> load 0x3fff20b8, len 40, room 4 
16:23:36.079 -> tail 4
16:23:36.079 -> chksum 0xc9
16:23:36.079 -> csum 0xc9
16:23:36.079 -> v0007e910
16:23:36.079 -> ~ld
16:23:36.145 -> Connecting to AP ...
16:23:36.664 -> ..........

I'm not that good in debugging such issues, but if someone can give some hits, then i'll dig up some more :-) Here is the complete sketch that i'm using

#include <ESP8266WiFi.h>
#include <ThingsBoard.h>

#define WIFI_AP "MyWifiNetwork"
#define WIFI_PASSWORD "MyWifiPasswd"
#define TOKEN "1234567890abcdefg"


char thingsboardServer[] = "host.domain.tld";

WiFiClient wifiClient;
ThingsBoard tb(wifiClient);

int status = WL_IDLE_STATUS;
unsigned long lastSend;

void setup() {
  Serial.begin(115200);
  delay(10);
  InitWiFi();
  lastSend = 0;
}

void loop() {
  if ( !tb.connected() ) {
    reconnect();
  }
  if ( millis() - lastSend > 1000 ) { // Update and send only after 1 seconds
    getAndSendTemperatureAndHumidityData();
    lastSend = millis();
  }
  tb.loop();
}

void getAndSendTemperatureAndHumidityData() {
  Serial.println("Collecting temperature data.");

  float humidity = 100.0;
  float temperature = 30.00;

  Serial.println("Sending data to ThingsBoard:");
  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.println(" *C ");

  tb.sendTelemetryFloat("temperature", temperature);
  tb.sendTelemetryFloat("humidity", humidity);
}

void InitWiFi() {
  Serial.println("Connecting to AP ...");
  // attempt to connect to WiFi network

  WiFi.begin(WIFI_AP, WIFI_PASSWORD);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("Connected to AP");
}


void reconnect() {
  // Loop until we're reconnected
  while (!tb.connected()) {
    status = WiFi.status();
    if ( status != WL_CONNECTED) {
      Serial.println("Wifi not connected");
      WiFi.begin(WIFI_AP, WIFI_PASSWORD);
      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
      }
      Serial.println("Connected to AP");
    }
    Serial.print("Connecting to ThingsBoard node ...");
    if ( tb.connect(thingsboardServer, TOKEN) ) {
      Serial.println( "[DONE]" );
    } else {
      Serial.print( "[FAILED]" );
      Serial.println( " : retrying in 5 seconds]" );
      // Wait 5 seconds before retrying
      delay( 5000 );
    }
  }
}
@MathewHDYT
Copy link
Contributor

This is a typo and your part and not an issue with the library. If you see this question on StackOverflow, the person asking has the exact same problem.

What is occuring is that you are trying to save "host.domain.tld" into the thingsboardServer char array, which will be compiled to an array of the size 1. Causing the exception 3 to occur.

Making your array constexpr or const should resolve this issue. So instead of this line.

char thingsboardServer[] = "host.domain.tld";

Simply write this instead.

constexpr char thingsboardServer[] = "host.domain.tld";

@hertell
Copy link
Author

hertell commented Aug 20, 2023

I tested that already, but the crash-result is the same :-(

@MathewHDYT
Copy link
Contributor

MathewHDYT commented Aug 20, 2023

Does it also not work when you add the size.

constexpr const char thingsboardServer[16] = "host.domain.tld";

@hertell
Copy link
Author

hertell commented Aug 20, 2023

The same. Even tb.connect("host.domain.tld", "1234567890abcdefg") makes it crash.

@MathewHDYT
Copy link
Contributor

Interesting is it the same exception code so 3 or is it another number now?

The exception causes and their number are described in this table

@hertell
Copy link
Author

hertell commented Aug 20, 2023

Yes, it's the same

Exception (3):
epc1=0x402203e2 epc2=0x00000000 epc3=0x00000000 excvaddr=0x4026e1c7 depc=0x00000000

>>>stack>>>

@MathewHDYT
Copy link
Contributor

Ah after thinking about it for a while I think I might have remembered what the issue could be. ESP8266 seems to have problems with writing PROGMEM strings to serial, all internal debug messages are using PROGMEM per default because the strings are saved in flash memory not clogging up DRAM, which can then be used for other variables.

But some configurations or boards that use the ESP8266 do not actually support PROGMEM, therefore this has to be disabled beforehand.

This is done over a #define before the #include <Thingsboard.h> call should be called. Simply adjust your code to look like this and the issue should be hopefully fixed.

#include <ESP8266WiFi.h>
// Disable PROGMEM because the ESP8266WiFi library,
// does not support flash strings.
#define THINGSBOARD_ENABLE_PROGMEM 0
#include <ThingsBoard.h>

@hertell
Copy link
Author

hertell commented Aug 20, 2023

Bingo! That fixed it! :-) Thanks for your help figuring this out! Is this something that could be detected by the library?

@MathewHDYT
Copy link
Contributor

MathewHDYT commented Aug 20, 2023

The above mentioned snippet is included in the examples and it is the first segment in the README.md file under the Troubleshooting section.

The only thing I could do is always print a warning to the screen when using PROGMEM, something like this if PROGMEM is enabled

"If LoadStoreException occur and the device crashes, when using ThingsBoard try to disable PROGMEM (#define THINGSBOARD_ENABLE_PROGMEM 0), which might cause problems with devices that do not support using constant strings in flash memory"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants