· 7 years ago · Jan 23, 2018, 11:36 PM
1/*
2 Basic ESP8266 MQTT example
3
4 This sketch demonstrates the capabilities of the pubsub library in combination
5 with the ESP8266 board/library.
6
7 It connects to an MQTT server then:
8 - publishes "hello world" to the topic "outTopic" every two seconds
9 - subscribes to the topic "inTopic", printing out any messages
10 it receives. NB - it assumes the received payloads are strings not binary
11 - If the first character of the topic "inTopic" is an 1, switch ON the ESP Led,
12 else switch it off
13
14 It will reconnect to the server if the connection is lost using a blocking
15 reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to
16 achieve the same result without blocking the main loop.
17
18 To install the ESP8266 board, (using Arduino 1.6.4+):
19 - Add the following 3rd party board manager under "File -> Preferences -> Additional Boards Manager URLs":
20 http://arduino.esp8266.com/stable/package_esp8266com_index.json
21 - Open the "Tools -> Board -> Board Manager" and click install for the ESP8266"
22 - Select your ESP8266 in "Tools -> Board"
23
24*/
25
26#include <ESP8266WiFi.h>
27#include <PubSubClient.h>
28
29// Update these with values suitable for your network.
30
31const char* ssid = "Livebox-DE38";
32const char* password = "Damianades";
33const char* mqtt_server = "192.168.1.12";
34
35WiFiClient espClient;
36PubSubClient client(espClient);
37long lastMsg = 0;
38char msg[50];
39int value = 0;
40int ledPin = 2;
41
42void setup() {
43 pinMode(ledPin, OUTPUT); // Initialize the BUILTIN_LED pin as an output
44 pinMode(4,INPUT);
45 Serial.begin(115200);
46 setup_wifi();
47 client.setServer(mqtt_server, 1883);
48 client.setCallback(callback);
49}
50
51void setup_wifi() {
52
53 delay(10);
54 // We start by connecting to a WiFi network
55 Serial.println();
56 Serial.print("Connecting to ");
57 Serial.println(ssid);
58
59 WiFi.begin(ssid, password);
60
61 while (WiFi.status() != WL_CONNECTED) {
62 onoff();
63 delay(500);
64 Serial.print(".");
65 }
66
67 Serial.println("");
68 Serial.println("WiFi connected");
69 Serial.println("IP address: ");
70 Serial.println(WiFi.localIP());
71}
72
73void callback(char* topic, byte* payload, unsigned int length) {
74 Serial.print("Message arrived [");
75 Serial.print(topic);
76 Serial.print("] ");
77 for (int i = 0; i < length; i++) {
78 Serial.print((char)payload[i]);
79 }
80 Serial.println();
81
82 // Switch on the LED if an N was received as first character
83 if ((char)payload[1] == 'N') {
84 digitalWrite(ledPin, HIGH); // Turn the LED on (Note that LOW is the voltage level
85 // but actually the LED is on; this is because
86 // it is acive low on the ESP-01)
87 } else {
88 digitalWrite(ledPin, LOW); // Turn the LED off by making the voltage HIGH
89 }
90
91}
92
93void reconnect() {
94 // Loop until we're reconnected
95 while (!client.connected()) {
96 onoff();
97 Serial.print("Attempting MQTT connection...");
98 // Attempt to connect
99 if (client.connect("ESP8266Client")) {
100 Serial.println("connected");
101 // Once connected, publish an announcement...
102 client.publish("test", "hello world");
103 // ... and resubscribe
104 client.subscribe("test");
105 } else {
106 Serial.print("failed, rc=");
107 Serial.print(client.state());
108 Serial.println(" try again in 5 seconds");
109 // Wait 3 seconds before retrying
110 delay(80);
111 }
112 }
113}
114void loop() {
115
116onoff();
117 if (!client.connected()) {
118 reconnect();
119 }
120 client.loop();
121
122 long now = millis();
123 if (now - lastMsg > 10000) {
124 lastMsg = now;
125 ++value;
126 snprintf (msg, 75, "hello world #%ld", value);
127 Serial.print("Publish message: ");
128 Serial.println(msg);
129 client.publish("outTopic", msg);
130 }
131}
132
133void onoff(){
134 if(digitalRead(4)==0 ){
135
136 if(digitalRead(ledPin) == LOW){
137 digitalWrite(ledPin,HIGH);
138 client.publish("test","ON");
139 Serial.print("1: ");
140 Serial.println(digitalRead(4));
141 delay(600);
142 }
143 else if(digitalRead(ledPin) == HIGH){
144 digitalWrite(ledPin,LOW);
145 client.publish("test","OFF");
146 Serial.print("2: ");
147 Serial.println(digitalRead(4));
148 delay(600);
149 }
150 }
151}