I'm using NodeMCU 0.9 My sketch crashes if the following 2 include are present in the sketch:
#include <PubSubClient.h>
#include <HX711.h>
I obtain a Soft WDT reset
If I do not use HX711 I can send message using mqtt. If I do not use PubSubClient I can read weight values from the load cell connected to the hx711 board without problem
The code crashes at the following line:
mqtt_client.connect("ESP8266Client", mqtt_user, mqtt_password)
Actually using this debugger suggested here the line that crashes is the following and it is inside the PubSubClient class:
WiFiClient::available()
It is a function already existing at ~/.arduino15/packages/esp8266/hardware/esp8266/2.4.1/libraries/ESP8266WiFi/src/WiFiClient.cpp line 21
Here I paste my code so you can reproduce it and tell me if you have the same problem:
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include "HX711.h"
#define wifi_ssid "mqtt"
#define wifi_password "testmqtt"
#define mqtt_server "THE.IP.OF.MQTT"
#define mqtt_user "USER"
#define mqtt_password "PSW"
#define humidity_topic "sensor/humidity"
#define temperature_topic "sensor/temperature"
#define weight_topic "sensor/weight"
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
Serial.begin(9600);
setup_wifi();
client.setServer(mqtt_server, 1883);
}
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(wifi_ssid);
WiFi.begin(wifi_ssid, wifi_password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
// If you do not want to use a username and password, change next line to
// if (client.connect("ESP8266Client")) {
if (client.connect("ESP8266Client", mqtt_user, mqtt_password)) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
long lastMsg = 0;
float temp = 0.0;
float hum = 0.0;
float diff = 1.0;
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
long now = millis();
if (now - lastMsg > 1000) {
lastMsg = now;
float newTemp = 456;
float newHum = 123;
temp = newTemp+1;
Serial.print("New temperature:");
Serial.println(String(temp).c_str());
client.publish(weight_topic, String(temp).c_str(), true);
}
}
It works if you comment the initial include
PS: Nothing change if HX711 is include as <...>
or "..."
HX711_ADC
. I'll try to adapt my code to this new library and update if it gives me problem