Closed
Description
----------------------------- Delete below -----------------------------
If your issue is a general question, starts similar to "How do I..", is related to 3rd party libs, or is related to hardware, please discuss at a community forum like esp8266.com.
INSTRUCTIONS
If you do not follow these instructions, your issue may be dismissed.
- Follow the checklist under Basic Infos and fill in the [ ] spaces with an X.
- Fill in all the fields under Platform and Settings in IDE marked with [ ] (pick the correct option for you in each case, delete the others).
- If you haven't already done so, test your issue against current master branch (aka latest git), because it may have been already fixed.
- Describe your problem.
- If you have a STACK DUMP decode it:
https://arduino-esp8266.readthedocs.io/en/latest/Troubleshooting/stack_dump.html
- Include a Minimal Complete Reproducible Example sketch that shows your issue. Do not include your entire project, or a huge piece of code.
- Include debug messages:
https://arduino-esp8266.readthedocs.io/en/latest/Troubleshooting/debugging.html
- Use markup (buttons above) and the Preview tab to check what the issue will look like.
- Delete these instructions from the above to the below marker lines before submitting this issue.
----------------------------- Delete above -----------------------------
Basic Infos
- This issue complies with the issue POLICY doc.
- I have read the documentation at readthedocs and the issue is not addressed there.
- I have tested that the issue is present in current master branch (aka latest git).
- I have searched the issue tracker for a similar issue.
- If there is a stack dump, I have decoded it.
- I have filled out all fields below.
Platform
- Hardware: [ESP-12|ESP-01|ESP-07|ESP8285 device|other]
- Core Version: [latest git hash or date]
- Development Env: [Arduino IDE|Platformio|Make|other]
- Operating System: [Windows|Ubuntu|MacOS]
Settings in IDE
- Module: [Generic ESP8266 Module|Wemos D1 mini r2|Nodemcu|other]
- Flash Mode: [qio|dio|other]
- Flash Size: [4MB/1MB]
- lwip Variant: [v1.4|v2 Lower Memory|Higher Bandwidth]
- Reset Method: [ck|nodemcu]
- Flash Frequency: [40Mhz]
- CPU Frequency: [80Mhz|160MHz]
- Upload Using: [OTA|SERIAL]
- Upload Speed: [115200|other] (serial upload only)
Problem Description
Detailed problem description goes here.
MCVE Sketch
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#define BUTTON D1
// WiFi
const char *ssid = "mousse"; // Enter your WiFi name
const char *password = "qweqweqwe"; // Enter WiFi password
// MQTT Broker
const char *mqtt_broker = "broker.emqx.io";
const char *topic = "esp8266/test";
const char *mqtt_username = "emqx";
const char *mqtt_password = "public";
const int mqtt_port = 1883;
bool status_internet = true ;
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
// Set software serial baud to 115200;
Serial.begin(115200);
pinMode(BUTTON, INPUT); // Button 1 as input
// connecting to a WiFi network
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the WiFi network");
//connecting to a mqtt broker
client.setServer(mqtt_broker, mqtt_port);
client.setCallback(callback);
while (!client.connected()) {
status_internet = fail;
String client_id = "esp8266-client-";
client_id += String(WiFi.macAddress());
Serial.printf("The client %s connects to the public mqtt broker\n", client_id.c_str());
if (client.connect(client_id.c_str(), mqtt_username, mqtt_password)) {
status_internet = true;
Serial.println("Public emqx mqtt broker connected");
} else {
Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);
}
}
// publish and subscribe
client.publish(topic, "hello emqx");
client.subscribe(topic);
}
void callback(char *topic, byte *payload, unsigned int length) {
Serial.print("Message arrived in topic: ");
Serial.println(topic);
Serial.print("Message:");
for (int i = 0; i < length; i++) {
Serial.print((char) payload[i]);
}
Serial.println();
Serial.println("-----------------------");
}
void check_input()
{
if(digitalRead(BUTTON_1) client.publish("state button", 1);
else client.publish("state button", 0);
}
void loop() {
client.loop();
check_input();
}
Debug Messages
Debug messages go here
When internet lost connection client.connected() return true so I continue to publish -> Error
Metadata
Metadata
Assignees
Labels
No labels