123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- #include <ESP8266WiFi.h>
- #include <PubSubClient.h>
-
- const char* SSID = "smartroom";
- const char* PSK = "smarthome";
- const char* MQTT_BROKER = "192.168.4.1";
-
- WiFiClient espClient;
- PubSubClient client(espClient);
- long lastMsg = 0;
- char msg[50];
- int value = 0;
-
- void setup() {
- Serial.begin(115200);
- setup_wifi();
- client.setServer(MQTT_BROKER, 1883);
- }
-
- void setup_wifi() {
- delay(10);
- Serial.println();
- Serial.print("Connecting to ");
- Serial.println(SSID);
- WiFi.mode(WIFI_STA);
- WiFi.begin(SSID, PSK);
-
- 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() {
- while (!client.connected()) {
- Serial.print("Reconnecting...");
- if (!client.connect("ESP8266Client")) {
- Serial.print("failed, rc=");
- Serial.print(client.state());
- Serial.println(" retrying in 5 seconds");
- delay(5000);
- }
- }
- }
- void loop() {
-
- if (!client.connected()) {
- reconnect();
- }
- client.loop();
-
- snprintf (msg, 50, "Alive since %ld milliseconds", millis());
- Serial.print("Publish message: ");
- Serial.println(msg);
- client.publish("/home/data",msg);
- delay(5000);
- }
|