Coding issue while trying to connect my ESP8266 to Wifi

Hi, my code below seems to continuously attempt to connect my "ESP8266" to Wi-Fi and I can't figure out which part of my code is giving me this issue. The code is listed below

#include <SoftwareSerial.h>
#include <TinyGPS++.h>
#include <ESP8266WiFi.h>
#include <ThingsBoard.h>
#include <Seeed_mbedtls.h>

#define THINGSBOARD_ENABLE_PROGRAM 0
#define THINGSBOARD_ENABLE_PSRAM 0
#define THINGSBOARD_ENABLE_DYNAMIC 1

constexpr char WIFI_SSID[]="Fios-Rdq3F";
constexpr char WIFI_PASSWORD[]="sun59sum25mouse";
constexpr char TOKEN[]="TKm4MA82jKZKBRDidGlX";
constexpr char THINGSBOARD_SERVER[]="demo.thingsboard.io";
constexpr uint16_t THINGSBOARD_PORT=1883U;
constexpr uint32_t MAX_MESSAGE_SIZE=256U;
constexpr uint32_t SERIAL_DEBUG_BAUD= 115200U;
constexpr int16_t telemetrySendInterval = 2000U;
uint32_t previousDataSend;

WiFiClient wifiClient;
ThingsBoard tb(wifiClient, MAX_MESSAGE_SIZE);

int MQ2=A0;//(A0)
int GAS;
int Latitude;
int Longitude;
int Speed;
int Altitude;

TinyGPSPlus gps;
SoftwareSerial GPS(2,0);//Blue(D4),purple(D3)

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

const bool reconnect() {
// Check to ensure we aren't connected yet
const wl_status_t status = WiFi.status();
if (status == WL_CONNECTED) {
return true;
}
InitWiFi();
return true;
}

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
GPS.begin(9600);
Serial.begin(115200);
InitWiFi();
pinMode(MQ2,INPUT);
}

void loop() {
if (!reconnect()) {
return;
}

if (!tb.connected()) {
Serial.print("Connecting to: ");
Serial.print(THINGSBOARD_SERVER);
Serial.print(" with token ");
Serial.println(TOKEN);
}
if (!tb.connect(THINGSBOARD_SERVER, TOKEN, THINGSBOARD_PORT)) {
Serial.println("Failed to connect");
return;
}
GPS.listen();
while (GPS.available() > 0) {
gps.encode(GPS.read());
if (gps.location.isUpdated()) {
Serial.print("Longitude=");
Serial.println(gps.location.lng(), 6);
Serial.print("Latitude=");
Serial.println(gps.location.lat(), 6);
Serial.print("Speed=");
Serial.println(gps.speed.kmph());
Serial.print("Altitude in meters=");
Serial.println(gps.altitude.meters());

  GAS= analogRead(MQ2);
  Serial.print("Gas=");
  Serial.println(GAS);




  Serial.println("\t");
  delay(1000);


  tb.sendTelemetryInt("Gas",GAS);
  tb.sendTelemetryInt("Longitude",gps.location.lng());
  tb.sendTelemetryInt("Latitude",gps.location.lat());
  tb.sendTelemetryInt("Speed",gps.speed.kmph());
  tb.sendTelemetryInt("Altitude",gps.altitude.meters());
}

}
}

This is the message that I receive after I compile the code. The message is listed below...

ets Jan 8 2013,rst cause:2, boot mode:(3,6)

load 0x4010f000, len 3424, room 16

tail 0

chksum 0x2e

load 0x3fff20b8, len 40, room 8

tail 0

chksum 0x2b

csum 0x2b

v0007c3e0

~ld

����o�r��N|�$�d b�2�|r�$�o��o�$`��r�l�l��Connecting to AP ...

.............Connected to AP

Connecting to: demo.thingsboard.io with token TKm4MA82jKZKBRDidGlX

—-----------------CUT HERE FOR EXCEPTION DECODER—------------------

Exception (3):

epc1=0x40221d1a epc2=0x00000000 epc3=0x00000000 excvaddr=0x4026e6e7 depc=0x00000000

stack>>>

ctx: cont

sp: 3ffffde0 end: 3fffffd0 offset: 0150

3fffff30: 514d0400 3f045454 000001f4 000001f4

3fffff40: 00000000 4026e6e7 00000000 00000000

3fffff50: 00000001 00000100 00000000 4020ee6c

3fffff60: 3ffe91a2 fffffffc 00000001 3ffe8c00

3fffff70: 3ffe8c14 3fff0778 3fff05d0 4020c228

3fffff80: 00000000 00000000 00000001 4020c7d0

3fffff90: 3ffe8c14 3fff0778 3fff05d0 4020824c

3fffffa0: feefeffe feefeffe feefeffe 3fff0900

3fffffb0: 3fffdad0 00000000 3fff08d4 4020dd14

3fffffc0: feefeffe feefeffe 3fffdab0 4010140d

<<<stack<<<

First: Edit your post and insert code tags for the code and for the serial output!

From that output I would say the ESP connected to your WiFi. It seems that the thingsboard code fails afterwards.

Once you inserted the code tags I try to get the stack trace decoded to give you a hint where your code failed.

When I see that, I want to know how things are powered.
Gas sensors draw a significant amount of current, and could overheat voltage regulators.
That could periodically reboot the processor.
Please upload a connection diagram.
Leo..

Im in the same group as Kabam96, our powered components are the Neo-6M and the MQ2 sensor which are both powered by external power supplies.

#include <SoftwareSerial.h>
#include <TinyGPS++.h>
#include <ESP8266WiFi.h>
#include <ThingsBoard.h>
#include <Seeed_mbedtls.h>

#define THINGSBOARD_ENABLE_PROGRAM 0
#define THINGSBOARD_ENABLE_PSRAM 0
#define THINGSBOARD_ENABLE_DYNAMIC 1

constexpr char WIFI_SSID[]="Fios-Rdq3F";
constexpr char WIFI_PASSWORD[]="sun59sum25mouse";
constexpr char TOKEN[]="TKm4MA82jKZKBRDidGlX";
constexpr char THINGSBOARD_SERVER[]="demo.thingsboard.io";
constexpr uint16_t THINGSBOARD_PORT=1883U;
constexpr uint32_t MAX_MESSAGE_SIZE=256U;
constexpr uint32_t SERIAL_DEBUG_BAUD= 115200U;
constexpr int16_t telemetrySendInterval = 2000U;
uint32_t previousDataSend;

WiFiClient wifiClient;
ThingsBoard tb(wifiClient, MAX_MESSAGE_SIZE);

int MQ2=A0;//(A0)
int GAS;
int Latitude;
int Longitude;
int Speed;
int Altitude;

TinyGPSPlus gps;
SoftwareSerial GPS(2,0);//Blue(D4),purple(D3)

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

const bool reconnect() {
// Check to ensure we aren't connected yet
const wl_status_t status = WiFi.status();
if (status == WL_CONNECTED) {
return true;
}
InitWiFi();
return true;
}

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
GPS.begin(9600);
Serial.begin(115200);
InitWiFi();
pinMode(MQ2,INPUT);
}

void loop() {
if (!reconnect()) {
return;
}

if (!tb.connected()) {
Serial.print("Connecting to: ");
Serial.print(THINGSBOARD_SERVER);
Serial.print(" with token ");
Serial.println(TOKEN);
}
if (!tb.connect(THINGSBOARD_SERVER, TOKEN, THINGSBOARD_PORT)) {
Serial.println("Failed to connect");
return;
}
GPS.listen();
while (GPS.available() > 0) {
gps.encode(GPS.read());
if (gps.location.isUpdated()) {
Serial.print("Longitude=");
Serial.println(gps.location.lng(), 6);
Serial.print("Latitude=");
Serial.println(gps.location.lat(), 6);
Serial.print("Speed=");
Serial.println(gps.speed.kmph());
Serial.print("Altitude in meters=");
Serial.println(gps.altitude.meters());

  GAS= analogRead(MQ2);
  `Serial.print`("Gas=");
  `Serial.println`(GAS);

Serial.println("\t");
delay(1000);

 ` tb.sendTelemetryInt`("Gas",GAS);
 ` tb.sendTelemetryInt`("Longitude",gps.location.lng());
  `tb.sendTelemetryInt`("Latitude",gps.location.lat());
  `tb.sendTelemetryInt`("Speed",gps.speed.kmph());
  `tb.sendTelemetryInt`("Altitude",gps.altitude.meters());
}

}
}
@pylon is this what you mean by code tags? (Kabam96 and I are very new to this)

Type 3 backward apostrophes, press ENTER
Paste your code
Start a new line
Type 3 backward apostrophes, press ENTER

(the "backward apostrophe" key is to the left of the '1' on the keyboard)

Hi Newbie welcome to the community

Kindly go through it
Guidelines

2 Likes

I would appreciate it if you provided some useful information. Thanks

Are you talking to yourself?
Leo..

I installed the sketch on my D1 mini clone and was able to connect to my WiFi but the " if (!tb.connect(THINGSBOARD_SERVER, TOKEN, THINGSBOARD_PORT))" crashed with an "exception (3)". I got a stack trace from the ESP Exception Decoder but I can't figure out what exactly is going wrong:

*Decoding stack results* 
0x4020ebfc: **uart_write(uart_t*, char const*, size_t)** at /Users/john/Library/Arduino15/packages/esp8266/hardware/esp8266/3.1.2/cores/esp8266/**uart.cpp** line **547** 
0x4020bf90: **PubSubClient::connect(char const*, char const*, char const*)** at /Users/john/Documents/Arduino/libraries/TBPubSubClient/src/**TBPubSubClient.cpp** line **170** 
0x4020c534: **Print::println(char const*)** at /Users/john/Library/Arduino15/packages/esp8266/hardware/esp8266/3.1.2/cores/esp8266/**Print.cpp** line **238** 0x40207f44: **loop()** at /Users/john/Documents/Arduino/libraries/ThingsBoard/src/**ThingsBoard.h** line **1753** 
0x4020c534: **Print::println(char const*)** at /Users/john/Library/Arduino15/packages/esp8266/hardware/esp8266/3.1.2/cores/esp8266/**Print.cpp** line **238** 
0x4020daa0: **loop_wrapper()** at /Users/john/Library/Arduino15/packages/esp8266/hardware/esp8266/3.1.2/cores/esp8266/**core_esp8266_main.cpp** line **258**

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