Closed yedhink closed 4 years ago
The wdt reset error caused by the blocking code (http request/response) and delay used in your code which the wdt does not feed or clear in time.
The error still be happened even no Firebase library used.
The below code send https request to github and wait for response (the code is taken from Arduino example)
#include <ESP8266WiFi.h>
#include <espnow.h>
const char* host = "api.github.com";
const int httpsPort = 443;
const char fingerprint[] PROGMEM = "5F F1 60 31 09 04 3E F2 90 D2 B0 8A 50 38 04 E8 37 9F BC 76";
#define NO_OF_LIGHTS 2
#define NO_OF_FANS 0
const int totalApp = NO_OF_LIGHTS + NO_OF_FANS;
String appliances[totalApp];
String stat[2] = {"0", "0"};
volatile int receivedDataFromEspCallback = 0, dataSentToEsp = 0;
uint8_t receiverAddress[] = {0x40, 0xf5, 0x20, 0x17, 0x71, 0xba}; //add reciver mac ids
struct __attribute__((packed)) dataPacket
{
int8_t index;
int8_t status;
};
volatile dataPacket pac;
dataPacket packet;
void transmissionComplete(uint8_t *receiver_mac, uint8_t transmissionStatus)
{
if (transmissionStatus == 0)
{
Serial.println("Data sent successfully from Master to Slave");
dataSentToEsp = 1;
}
else
{
Serial.print("Error code in transmission complete: ");
Serial.println(transmissionStatus);
}
}
void dataRecived(uint8_t *senderMac, uint8_t *data, uint8_t dataLength)
{
sendRequest();
}
void wifiConnect()
{
WiFi.begin("ssid", "password");
Serial.println("Connecting..");
while (WiFi.status() != WL_CONNECTED)
{
Serial.println('.');
delay(1000);
}
}
void setup()
{
Serial.begin(74880);
Serial.print("/n/nInitializing...");
Serial.print("My MAC address is: ");
Serial.println(WiFi.macAddress());
WiFi.mode(WIFI_AP_STA);
WiFi.disconnect();
WiFi.softAP("ssid", "password", 1);
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
wifiConnect();
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// const int WIFI_CHANNEL = WiFi.channel();
Serial.println(WiFi.channel());
if (esp_now_init() != 0)
{
Serial.println("ESP-NOW initialization failed");
return;
}
// TODO: move into a sep function called initEspNow()
esp_now_set_self_role(MY_ROLE);
esp_now_register_send_cb(transmissionComplete);
// REGISTER OUR CUSTOM CALLBACK FUNCTION-----------------------------------------> Register callback
esp_now_register_recv_cb(dataRecived);
esp_now_add_peer(receiverAddress, RECEIVER_ROLE, 1, NULL, 0);
Serial.println("Initialized.");
for (int i = 0; i < totalApp; ++i)
{
const String indexOfAppliance = String(i + 1);
const String currentAppliance = i < NO_OF_LIGHTS ? "LED" : "FAN";
appliances[i] = PARENT_PATH + currentAppliance + indexOfAppliance + "_Status";
Serial.println(appliances[i]);
}
}
void sendRealTimeDataToEspSlave(String realTimeValueReceived, int i)
{
Serial.println("GPIO 4, on");
dataPacket packet;
int Status = realTimeValueReceived == "1" ? 1 : 0;
packet.index = i;
packet.status = Status;
Serial.println(packet.index);
Serial.println(packet.status);
while (dataSentToEsp == 0)
{
esp_now_send(receiverAddress, (uint8_t *)&packet, sizeof(packet));
delay(100);
}
dataSentToEsp = 0;
}
void loop()
{
for (int i = 0; i < totalApp; ++i)
{
if (WiFi.status() != WL_CONNECTED)
{
wifiConnect();
}
sendRequest();
sendRealTimeDataToEspSlave(stat[i], i);
}
delay(500);
}
void sendRequest()
{
//The code was taken from Arduino example
// Use WiFiClientSecure class to create TLS connection
WiFiClientSecure client;
Serial.print("connecting to ");
Serial.println(host);
Serial.printf("Using fingerprint '%s'\n", fingerprint);
client.setFingerprint(fingerprint);
if (!client.connect(host, httpsPort))
{
Serial.println("connection failed");
return;
}
String url = "/repos/esp8266/Arduino/commits/master/status";
Serial.print("requesting URL: ");
Serial.println(url);
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"User-Agent: BuildFailureDetectorESP8266\r\n" +
"Connection: close\r\n\r\n");
Serial.println("request sent");
while (client.connected())
{
String line = client.readStringUntil('\n');
if (line == "\r")
{
Serial.println("headers received");
break;
}
}
String line = client.readStringUntil('\n');
if (line.startsWith("{\"state\":\"success\""))
{
Serial.println("esp8266/Arduino CI successfull!");
}
else
{
Serial.println("esp8266/Arduino CI has failed");
}
Serial.println("reply was:");
Serial.println("==========");
Serial.println(line);
Serial.println("==========");
Serial.println("closing connection");
}
The internet access is not available when making any http request in ESP-Now send/receive callback and the waiting time for server response was defined by the default tcp timeout in WiFiClient class.
When calling functions from this library within the registered esp callback, it leads to
wdt reset
. Using Arduino Uno with esp8266(nodemcu)volatile
variables to which I add data from the registered callback and call firebasesetJson
,getJson
etc fromloop()
. But it seems like a very big hack. Any idea how to actually callFirebase.setJson
etc from a callback?This is the failing code:-
@mobizt Any ideas how to use the REGISTERED_CALLBACK
dataRecived
to set firebase data and also why this error occurs in the first place? Thanks.