Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By Marie
#41830 Hi!
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
Code: Select all#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 :roll:
User avatar
By Lenbok
#48253
Marie wrote:Hi!
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.


Did you ever get this problem sorted out? I am trying to make a bubble counter (incremented in an interrupt) that submits the counts periodically via mqtt, and am getting esp crashes, constant mqtt reconnect attempts, etc.
User avatar
By dkumar
#48256 this is lua code for communication between two esp8266, i copied from www.esp8266.com post,

i able to send data from client to server, but i don't know :( how to send data from server to client, because i don't know lua.... :(

i want to serial-wifi-wifi-serial from both side data communication using esp8266

Please anybody help me, thanks

SERVER:
Code: Select allprint("configuring ap")

wifi.setmode(wifi.STATIONAP)
cfg={}
cfg.ssid="WS"
cfg.pwd="12345678"
wifi.ap.config(cfg)

print("config TCP server")
server = net.createServer(net.TCP,28800)
server:listen(80, function(conn)
     conn:on("receive",function(conn,data)
          print(data)
         
     end)
     conn:on("sent",function(conn)
          collectgarbage()
     end)
          conn:on("connection",function(conn)
          print("Connected")
     end)
end)
print("done")



CLIENT:
Code: Select all--------- SETTINGS ---------
SSID="WS"
PWD="12345678"
----------------------------
---------- VARIABLES --------
conn_flag = 0
--------- FUNCTIONS --------
function connect_wifi(ssid,pwd)
if (conn_flag == 0) then
     print("Waiting for connection...")
     print("#1")
else
     print("Reconnecting...")
     print("#1")
end
timer1=0
wifi.setmode(wifi.STATION)
wifi.sta.config(ssid,pwd)
wifi.sta.connect()
tmr.alarm(1,1000,1,function()
     if(wifi.sta.getip()~=nil) then
          tmr.stop(1)
          print("Connected to:", SSID)
          print("IP:",wifi.sta.getip())
          print("MAC:",wifi.sta.getmac())
          conn_flag = 1
     elseif(timer1==10) then
          print("Unable to connect to:",SSID)
          tmr.stop(1)
     end
end)
end

function connect_tcp()
tmr.alarm(2,1000,1,function() 
        sk = net.createConnection(net.TCP, 0) 
        sk:on("receive", function(sk, receivedData)
            print("received: " .. receivedData)
        end)
        sk:on("connection", function(sk)
               print("Connected!")
               print("#2")
               tmr.stop(2)
        end )
        sk:on("disconnection", function(sk)
               print("Disconnected!")
               print("#0")
               connect_wifi(SSID,PWD)
               connect_tcp()
        end )
        sk:connect(80, "192.168.4.1") -- server ESP IP
    collectgarbage()
end)
end
function send(data)
sk:send(data)
end

----------------------------

connect_wifi(SSID,PWD)
connect_tcp()




To send data to server I'm using atmega8 like this:

Code: Select allvoid send_wifis(char *s){
   uart_puts("send(\"");
   uart_puts(s);
   uart_puts("\")\n\r");

}

void send_wifilong(uint32_t liczba){
   uart_puts("send(\"");
   uart_putlong(liczba,10);
   uart_puts("\")\n\r");
}