]> git.piffa.net Git - sketchbook_andrea/blob - iot/mqtt_esp8266/mqtt_esp8266.ino
PWM correzione
[sketchbook_andrea] / iot / mqtt_esp8266 / mqtt_esp8266.ino
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 #include <psw.h>
29 // Update these with values suitable for your network.
30
31 //const char* ssid = "........";
32 //const char* password = "........";
33 const char* mqtt_server = "chrome";
34 const char* input = "inTopic" ;
35 const char* output = "outTopic" ;
36
37 WiFiClient espClient;
38 PubSubClient client(espClient);
39 long lastMsg = 0;
40 char msg[50];
41 int value = 0;
42
43 void setup() {
44   pinMode(BUILTIN_LED, OUTPUT);     // Initialize the BUILTIN_LED pin as an output
45   Serial.begin(115200);
46   setup_wifi();
47   client.setServer(mqtt_server, 1883);
48   client.setCallback(callback);
49 }
50
51 void 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     delay(500);
63     Serial.print(".");
64   }
65
66   Serial.println("");
67   Serial.println("WiFi connected");
68   Serial.println("IP address: ");
69   Serial.println(WiFi.localIP());
70 }
71
72 void callback(char* topic, byte* payload, unsigned int length) {
73   Serial.print("Message arrived [");
74   Serial.print(topic);
75   Serial.print("] ");
76   for (int i = 0; i < length; i++) {
77     Serial.print((char)payload[i]);
78   }
79   Serial.println();
80
81   // Switch on the LED if an 1 was received as first character
82   if ((char)payload[0] == '1') {
83     digitalWrite(BUILTIN_LED, LOW);   // Turn the LED on (Note that LOW is the voltage level
84     // but actually the LED is on; this is because
85     // it is acive low on the ESP-01)
86   } else {
87     digitalWrite(BUILTIN_LED, HIGH);  // Turn the LED off by making the voltage HIGH
88   }
89
90 }
91
92 void reconnect() {
93   // Loop until we're reconnected
94   while (!client.connected()) {
95     Serial.print("Attempting MQTT connection...");
96     // Attempt to connect
97     if (client.connect("ESP8266Client")) {
98       Serial.println("connected");
99       // Once connected, publish an announcement...
100       client.publish(output, "hello world");
101       // ... and resubscribe
102       client.subscribe(input);
103     } else {
104       Serial.print("failed, rc=");
105       Serial.print(client.state());
106       Serial.println(" try again in 5 seconds");
107       // Wait 5 seconds before retrying
108       delay(5000);
109     }
110   }
111 }
112 void loop() {
113
114   if (!client.connected()) {
115     reconnect();
116   }
117   client.loop();
118
119   long now = millis();
120   if (now - lastMsg > 2000) {
121     lastMsg = now;
122     ++value;
123     snprintf (msg, 75, "hello world #%ld", value);
124     Serial.print("Publish message: ");
125     Serial.println(msg);
126     client.publish(output, msg);
127   }
128 }