I'm making a digital light dimmer controlled by mqtt messages, using Openhab. It works ok before some few changes of the dimming value, but then esp crashes or has a watchdog reset.
I used the same code without PubSubClient and It worked perfectly.
It's my code
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
int AC_LOAD = 5; // Output to Opto Triac pin
int dimming = 90; // Dimming level (0-128) 0 = ON, 128 = OFF
unsigned long last_time=0;
WiFiClient wclient;
IPAddress server( 192,0,0,0);
PubSubClient client(wclient, server);
const char *ssid = "***"; // cannot be longer than 32 characters!
const char *pass = "***"; //
String topic = "ludique/#";
void changeColor(uint32_t c) {
if (updateLights) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
updateLights = false;
}
}
}
void callback(const MQTT::Publish& pub) {
detachInterrupt(12);
if(pub.topic()=="ludique/dimmer")
{Serial.print("Dimmer");
int dim=pub.payload_string().toInt();
dimming=dim;
Serial.println(dim);}
attachInterrupt(12, zero_crosss_int, RISING);
}
void zero_crosss_int() //function to be fired at the zero crossing to dim the light
{
int dimtime = (100*dimming); // For 60Hz =>65
delayMicroseconds(dimtime); // Wait till firing the TRIAC
digitalWrite(AC_LOAD, HIGH); // Fire the TRIAC
delayMicroseconds(100); // triac On propogation delay (for 60Hz use 8.33)
digitalWrite(AC_LOAD, LOW); // No longer trigger the TRIAC (the next zero crossing will swith it off) TRIAC
}
float n=0;
void setup() {
pinMode(AC_LOAD, OUTPUT);// Set AC Load pin as output
pinMode(12, INPUT_PULLUP);
pinMode(13,INPUT);
Serial.begin(9600);
delay(10);
Serial.println("holi2");
Serial.println();
Serial.println();
client.set_callback(callback);
attachInterrupt(12, zero_crosss_int, RISING); // Choose the zero cross interrupt # from the table above
}
void loop() {
if (WiFi.status() != WL_CONNECTED) {
Serial.print("Connecting to ");
Serial.print(ssid);
Serial.println("...");
WiFi.begin(ssid, pass);
if (WiFi.waitForConnectResult() != WL_CONNECTED)
return;
Serial.println("WiFi connected");
return;
}
if (!client.connected()) {
if (client.connect("arduinoClient")) {
client.set_callback(callback);
client.subscribe(topic);
}}
if (client.connected())
client.loop();
}
I hope someone could help me