esp8266 Use PubSubClient to push (publish) the solution to the problem of untimely push

The power-meter project was updated two days ago, and I plan to use the PubSubClient library to access homeassistant

But when running the project, it is found that the esp8266 mqtt push is extremely unstable

I checked the mqtt server and the client receiving the message, and found no problems

I wrote a circular push with esp8266, which is also very stable, eliminating the problem of the board itself

for(int i = 1; i <= 100; i++){ sprintf(message, "%d", i); client.publish(topic, message); yield(); delay(500); }

code segment:

void loop(){ while(1){ light1 = analogRead(A0); if(light1 > powerledPin_average){ // led on timeA = micros(); delay(300); break; } yield(); client.loop( ); } time_light = timeA - timeB; power = 1. / powerledPin_rate * 1000 * 3600 * 1000000 / time_light; Serial.println(power); sprintf(message, "%f", power); client.publish(topic, message) ; delay(100); // add a delay }

Guessing that the last line of publish code is too late to process and continues to loop,Therefore, a delay is added after the code is pushed, and the problem is solved.


Leave a Reply

Your Email address will not be published. The required items are marked with *