1 | #include <ESP8266WiFi.h>
|
2 | #include <PubSubClient.h>
|
3 |
|
4 | // Update these with values suitable for your network.
|
5 |
|
6 | //const char* ssid = "........";
|
7 | //const char* password = "........";
|
8 |
|
9 | //const char* ssid = ".......";
|
10 | //const char* password = "........";
|
11 |
|
12 | //const char* ssid = "....."; //replace this with your WiFi network name
|
13 | //const char* password = "......"; //replace this with your WiFi network password
|
14 |
|
15 | const char* ssid = "......";
|
16 | const char* password = "......";
|
17 |
|
18 |
|
19 | const char* mqtt_server = "192.xxx.xxx.xxx";
|
20 |
|
21 | WiFiClient espClient;
|
22 | PubSubClient client(espClient);
|
23 |
|
24 | long lastMsg = 0;
|
25 | char msg[50];
|
26 | int value = 0;
|
27 |
|
28 | void setup_wifi()
|
29 | {
|
30 | delay(10);
|
31 | // We start by connecting to a WiFi network
|
32 | Serial.println();
|
33 | Serial.print("Connecting to ");
|
34 | Serial.println(ssid);
|
35 |
|
36 | WiFi.begin(ssid, password);
|
37 |
|
38 | while (WiFi.status() != WL_CONNECTED)
|
39 | {
|
40 | delay(500);
|
41 | Serial.print(".");
|
42 | }
|
43 |
|
44 | randomSeed(micros());
|
45 |
|
46 | Serial.println("");
|
47 |
|
48 | /***********************************************/
|
49 | Serial.println("Connected.");
|
50 | Serial.print("MAC: ");
|
51 | Serial.println(WiFi.macAddress());
|
52 | Serial.print("IP: ");
|
53 | Serial.println(WiFi.localIP());
|
54 | Serial.print("Subnet: ");
|
55 | Serial.println(WiFi.subnetMask());
|
56 | Serial.print("Gateway :");
|
57 | Serial.println(WiFi.gatewayIP());
|
58 | Serial.print("DNS: ");
|
59 | Serial.println(WiFi.dnsIP());
|
60 | Serial.print("Channel: ");
|
61 | Serial.println(WiFi.channel());
|
62 | Serial.print("Status: ");
|
63 | Serial.println(WiFi.status());
|
64 | /***********************************************/
|
65 |
|
66 | }
|
67 |
|
68 | void callback(char* topic, byte* payload, unsigned int length)
|
69 | {
|
70 | Serial.print("Message arrived [");
|
71 | Serial.print(topic);
|
72 | Serial.print("] ");
|
73 | for (int i = 0; i < length; i++)
|
74 | {
|
75 | Serial.print((char)payload[i]);
|
76 | }
|
77 | Serial.println();
|
78 |
|
79 | // Switch on the LED if an 1 was received as first character
|
80 | if ((char)payload[0] == '1')
|
81 | {
|
82 | digitalWrite(2, LOW); // Turn the LED on (Note that LOW is the voltage level
|
83 | // but actually the LED is on; this is because
|
84 | // it is acive low on the ESP-01)
|
85 | } else
|
86 | {
|
87 | digitalWrite(2, HIGH); // Turn the LED off by making the voltage HIGH
|
88 | }
|
89 | }
|
90 |
|
91 | void reconnect()
|
92 | {
|
93 | // Loop until we're reconnected
|
94 | while (!client.connected())
|
95 | {
|
96 | Serial.print("Attempting MQTT connection...");
|
97 | // Create a random client ID
|
98 | String clientId = "ESP8266Client-";
|
99 | clientId += String(random(0xffff), HEX);
|
100 | // Attempt to connect
|
101 | if (client.connect(clientId.c_str(),"openhabian","mqtt"))
|
102 | {
|
103 | Serial.println("connected");
|
104 | // Once connected, publish an announcement...
|
105 | client.publish("outTopic", "hello world");
|
106 | // ... and resubscribe
|
107 | client.subscribe("inTopic");
|
108 | }
|
109 | else
|
110 | {
|
111 | Serial.print("failed, rc=");
|
112 | Serial.print(client.state());
|
113 | Serial.println(" try again in 5 seconds");
|
114 | // Wait 5 seconds before retrying
|
115 | delay(5000);
|
116 | }
|
117 | }
|
118 | }
|
119 |
|
120 | void setup()
|
121 | {
|
122 | pinMode(2, OUTPUT); // Initialize the BUILTIN_LED pin as an output
|
123 | Serial.begin(115200);
|
124 | setup_wifi();
|
125 |
|
126 | client.setServer(mqtt_server, 1883);
|
127 | client.setCallback(callback);
|
128 | }
|
129 |
|
130 | void loop()
|
131 | {
|
132 | if (!client.connected())
|
133 | {
|
134 | reconnect();
|
135 | }
|
136 | client.loop();
|
137 |
|
138 | long now = millis();
|
139 | if (now - lastMsg > 2000)
|
140 | {
|
141 | lastMsg = now;
|
142 | ++value;
|
143 | snprintf (msg, 75, "hello world #%ld", value);
|
144 | Serial.print("Publish message: ");
|
145 | Serial.println(msg);
|
146 | client.publish("outTopic", msg);
|
147 | }
|
148 | }
|