1
+ // 导入相关的库
2
+ #include < ArduinoJson.h>
3
+ #include < SoftwareSerial.h>
4
+ #include < ESP8266WiFi.h>
5
+ #include < Adafruit_MQTT.h>
6
+ #include < Adafruit_MQTT_Client.h>
7
+
8
+ /* ************************ WiFi Access Point *********************************/
9
+
10
+ #define WLAN_SSID " iQOO 7" // 你的WI-FI的SSID,注意把你的 WIFI 的 AP 设置成 2.4GHz 频段。
11
+ #define WLAN_PASS " 1234567899" // 你的WI-FI的密码
12
+
13
+ /* ************************ Adafruit.io Setup *********************************/
14
+
15
+ #define AIO_SERVER " io.adafruit.com"
16
+ #define AIO_SERVERPORT 1883 // use 8883 for SSL
17
+ #define AIO_USERNAME " coderzzx" // 你在io.adafruit.com上注册的用户名
18
+ #define AIO_KEY " aio_FVAz59rsIgM6vRHWNTbX3h6ceAch" // 你在io.adafruit.com所获得的AIO
19
+
20
+ /*
21
+ 定义nodeMCU引脚编号
22
+ */
23
+ #define D1 5
24
+ #define D2 4
25
+ #define D3 0
26
+ #define D4 2
27
+ #define D5 14
28
+ #define D6 12
29
+ #define D7 13
30
+
31
+ #define LED_PIN D4
32
+
33
+ /* ***********************************************实例化串口通信的对象****************************************************/
34
+ SoftwareSerial nodemcuSerial (D6, D5);
35
+
36
+ /* *********************************************用于将数据发布到服务器上***************************************************/
37
+ // Create an ESP8266 WiFiClient class to connect to the MQTT server.
38
+ WiFiClient client;
39
+
40
+ // Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
41
+ Adafruit_MQTT_Client mqtt (&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
42
+
43
+ /* ***************************** Feeds ***************************************/
44
+
45
+ // Setup a feed called 'photocell' for publishing.
46
+ // Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
47
+ Adafruit_MQTT_Publish t_rel = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME " /feeds/Temperature" );
48
+ Adafruit_MQTT_Publish h_rel = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME " /feeds/Humidity" );
49
+ Adafruit_MQTT_Publish c_rel = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME " /feeds/CO Density" );
50
+ Adafruit_MQTT_Publish p_rel = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME " /feeds/PM2.5" );
51
+
52
+ // Setup a feed called 'onoff' for subscribing to changes.
53
+ Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME " /feeds/Switch" );
54
+
55
+ // Function to connect and reconnect as necessary to the MQTT server.
56
+ // Should be called in the loop function and it will take care if connecting.
57
+ void MQTT_connect () {
58
+ int8_t ret;
59
+ int8_t retries = 10 ;
60
+
61
+ // Stop if already connected.
62
+ if ( mqtt.connected () )
63
+ {
64
+ return ;
65
+ }
66
+
67
+ Serial.println (" Connecting to MQTT... " );
68
+
69
+ while ( (ret = mqtt.connect ()) != 0 ) // connect will return 0 for connected
70
+ {
71
+ Serial.println ( mqtt.connectErrorString (ret) );
72
+ Serial.println (" Retrying MQTT connection in 5 seconds..." );
73
+
74
+ mqtt.disconnect ();
75
+ delay (5000 );
76
+
77
+ retries--;
78
+ if (retries == 0 ) {
79
+ // basically die and wait for WDT to reset me
80
+ while (1 );
81
+ }
82
+ }
83
+
84
+ Serial.println (" MQTT Connected!" );
85
+ }
86
+
87
+ /* **************************************************数据发布函数*********************************************************/
88
+ void infoRelea (float x[])
89
+ {
90
+ // this is our 'wait for incoming subscription packets' busy subloop
91
+ // try to spend your time here
92
+ Adafruit_MQTT_Subscribe *subscription;
93
+
94
+ // Ensure the connection to the MQTT server is alive (this will make the first
95
+ // connection and automatically reconnect when disconnected). See the MQTT_connect
96
+ // function definition further below.
97
+ MQTT_connect ();
98
+
99
+ int flag;
100
+ while ((subscription = mqtt.readSubscription (5000 )))
101
+ {
102
+ if (subscription == &onoffbutton)
103
+ {
104
+ Serial.print (F (" Got: " ));
105
+ String value = (char *)onoffbutton.lastread ;
106
+
107
+ Serial.println (value);
108
+ if (!value.compareTo (" 1" ))
109
+ {
110
+ digitalWrite (LED_PIN, HIGH);
111
+ flag = 1 ;
112
+ }
113
+ if (!value.compareTo (" 0" ))
114
+ {
115
+ digitalWrite (LED_PIN, LOW);
116
+ flag = 0 ;
117
+ return ;
118
+ }
119
+ }
120
+ }
121
+
122
+ // Now we can publish stuff!
123
+ int i;
124
+ for (i = 0 ; i < 4 ; i++)
125
+ {
126
+ switch (i)
127
+ {
128
+ case 0 :
129
+ if (! t_rel.publish (x[0 ]))
130
+ {
131
+ Serial.println (F (" Failed" ));
132
+ }
133
+ else
134
+ {
135
+ Serial.println (F (" OK!" ));
136
+ }
137
+ break ;
138
+
139
+ case 1 :
140
+ if (! h_rel.publish (x[1 ]))
141
+ {
142
+ Serial.println (F (" Failed" ));
143
+ }
144
+ else
145
+ {
146
+ Serial.println (F (" OK!" ));
147
+ }
148
+ break ;
149
+
150
+ case 2 :
151
+ if (! c_rel.publish (x[2 ]))
152
+ {
153
+ Serial.println (F (" Failed" ));
154
+ }
155
+ else
156
+ {
157
+ Serial.println (F (" OK!" ));
158
+ }
159
+ break ;
160
+
161
+ case 3 :
162
+ if (! p_rel.publish (x[3 ]))
163
+ {
164
+ Serial.println (F (" Failed" ));
165
+ }
166
+ else
167
+ {
168
+ Serial.println (F (" OK!" ));
169
+ }
170
+ }
171
+ }
172
+ }
173
+
174
+ // 各个初始化操作
175
+ void setup ()
176
+ {
177
+ Serial.begin (115200 );
178
+ while (!Serial) continue ;
179
+
180
+ nodemcuSerial.begin (9600 );
181
+
182
+ Serial.println (F (" Adafruit MQTT demo" ));
183
+
184
+ // Connect to WiFi access point.
185
+ Serial.print (" Connecting to " );
186
+ Serial.println (WLAN_SSID);
187
+
188
+ WiFi.begin (WLAN_SSID, WLAN_PASS);
189
+
190
+ while (WiFi.status () != WL_CONNECTED)
191
+ {
192
+ delay (1000 );
193
+ Serial.print (" ." );
194
+ }
195
+
196
+ Serial.println ();
197
+ Serial.println (" WiFi connected" );
198
+ Serial.print (" IP address: " );
199
+ Serial.println (WiFi.localIP ());
200
+
201
+ // Setup MQTT subscription for onoff feed.
202
+ mqtt.subscribe (&onoffbutton);
203
+
204
+ pinMode (LED_PIN, OUTPUT);
205
+ }
206
+
207
+ void loop ()
208
+ {
209
+ /* ***********************************************用于从串口中获取数据****************************************************/
210
+ const size_t CAPACITY = JSON_OBJECT_SIZE (20 );
211
+ StaticJsonDocument<CAPACITY> doc;
212
+
213
+ DeserializationError error = deserializeJson (doc, nodemcuSerial);
214
+
215
+ // if (error)
216
+ // {
217
+ // Serial.print(F("deserializeJson() failed: "));
218
+ // Serial.println(error.f_str());
219
+ // return;
220
+ // }
221
+
222
+ JsonObject root = doc.as <JsonObject>();
223
+ // 获取数据
224
+ /*
225
+ t:温度
226
+ h:湿度
227
+ val:一氧化碳浓度
228
+ d:PM2.5浓度
229
+ */
230
+ float temp = (float )root[" temp" ];
231
+ float humi = (float )root[" humi" ];
232
+ float val = (float )root[" val" ];
233
+ float dens = (float )root[" dens" ];
234
+
235
+ float data[4 ] = {temp, humi, val, dens};
236
+ // for (int i = 0; i < 4; i++)
237
+ // Serial.println(data[i]);
238
+ /* **********************************************************************************************************************/
239
+ infoRelea (data);
240
+ }
0 commit comments