You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: tutorials/configure-nodemcu-iot-hub/index.mdx
+50-47Lines changed: 50 additions & 47 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,10 @@ products:
6
6
dates:
7
7
validation: 2025-05-27
8
8
validation_frequency: 12
9
-
9
+
usecase:
10
+
- iot
11
+
ecosystem:
12
+
- third-party
10
13
tags: iot iot-hub NodeMCU Arduino-IDE
11
14
hero: assets/scaleway_nodemcu.webp
12
15
---
@@ -60,9 +63,9 @@ Scaleway's IoT Hub lets your connected devices share messages. In this tutorial,
60
63
</Message>
61
64
62
65
1. Start the Arduino IDE application on your local computer.
63
-
2. Go to **Arduino** menu and click **Preferences**:
66
+
2. Go to **Arduino** menu and click **Preferences**:
64
67
<Lightboximage={image}alt="" />
65
-
3. Add the following link in the Additional Boards Manager URLs: `http://arduino.esp8266.com/stable/package_esp8266com_index.json` and click **OK** to validate:
68
+
3. Add the following link in the Additional Boards Manager URLs: `http://arduino.esp8266.com/stable/package_esp8266com_index.json` and click **OK** to validate:
66
69
<Lightboximage={image2}alt="" />
67
70
4. Go to **Tools** > **Board** > **Board Manager**
68
71
<Lightboximage={image3}alt="" />
@@ -78,14 +81,14 @@ Your Arduino IDE is now ready to communicate with the NodeMCU device.
78
81
1. Log into your [Scaleway console](https://conole.scaleway.com/) and click on **IoT Hub** in the side menu:
79
82
<Lightboximage={image6}alt="" />
80
83
2. Click **Create a Hub** and create your new IoT Hub.
81
-
3. Enter a name for your hub (here we use iot-hub-esp8266) and choose a plan. For this tutorial, we use the Free plan. If your Project needs more resources you can choose one of the larger hubs. Click **Create a hub** to deploy it.
84
+
3. Enter a name for your hub (here we use iot-hub-esp8266) and choose a plan. For this tutorial, we use the Free plan. If your Project needs more resources you can choose one of the larger hubs. Click **Create a hub** to deploy it.
82
85
4. Once your hub is ready, click on it. You will see the following page:
83
86
<Lightboximage={image7}alt="" />
84
-
5. Navigate to the **Devices** tab and click **Add device** to add a new device to the hub:
87
+
5. Navigate to the **Devices** tab and click **Add device** to add a new device to the hub:
85
88
<Lightboximage={image8}alt="" />
86
89
87
90
<Messagetype="note">
88
-
Make sure to enable insecure connections to be able to communicate with the IoT Hub, as we are not using certificates in this tutorial.
91
+
Make sure to enable insecure connections to be able to communicate with the IoT Hub, as we are not using certificates in this tutorial.
89
92
</Message>
90
93
6. Click **Add device to your hub** to add the device.
91
94
7. Once added, click on the device name to display some statistics about the device.
@@ -107,14 +110,14 @@ MQTT Explorer is a MQTT client that provides a structured overview of your MQTT
107
110
</Message>
108
111
109
112
<Lightboximage={image10}alt="" />
110
-
3. Click **Connect** to establish the connection to your IoT Hub. The following screen displays:
113
+
3. Click **Connect** to establish the connection to your IoT Hub. The following screen displays:
111
114
<Lightboximage={image11}alt="" />
112
115
113
116
Your MQTT Explorer is now successfully connected to your IoT Hub.
114
117
115
118
## Connecting your ESP8266 to Scaleway IoT Hub
116
119
117
-
We now connect the ESP8266 device to our IoT Hub. To do so, some additional libraries are required. Download them on your local computer and add them to the Arduino IDE by clicking on **Sketch** > **Include Library** > **Add .ZIP library**. We use the following libraries:
120
+
We now connect the ESP8266 device to our IoT Hub. To do so, some additional libraries are required. Download them on your local computer and add them to the Arduino IDE by clicking on **Sketch** > **Include Library** > **Add .ZIP library**. We use the following libraries:
118
121
119
122
*[PubSubClient library](https://github.com/knolleary/pubsubclient/archive/master.zip): The PubSubClient library provides a client for doing simple publish/subscribe messaging with a server that supports MQTT (basically allows your ESP8266 to talk with your MQTT Broker).
120
123
@@ -141,7 +144,7 @@ We now connect the ESP8266 device to our IoT Hub. To do so, some additional libr
141
144
WiFiClient espClient;
142
145
PubSubClient client(espClient);
143
146
```
144
-
4. We use the setup function to check the connection to our WiFi network and print a message in the Serial Monitor to confirm whether the device is connected or not:
147
+
4. We use the setup function to check the connection to our WiFi network and print a message in the Serial Monitor to confirm whether the device is connected or not:
145
148
```
146
149
void setup() {
147
150
Serial.begin(115200);
@@ -151,7 +154,7 @@ We now connect the ESP8266 device to our IoT Hub. To do so, some additional libr
151
154
Serial.println("Connecting to WiFi..");
152
155
}
153
156
Serial.println("Connected to the WiFi network");
154
-
157
+
155
158
client.setServer(mqttServer, mqttPort);
156
159
client.setCallback(callback);
157
160
```
@@ -160,7 +163,7 @@ We now connect the ESP8266 device to our IoT Hub. To do so, some additional libr
160
163
while (!client.connected()) {
161
164
Serial.println("Connecting to MQTT...");
162
165
if (client.connect("ESP8266Client", mqttUser, mqttPassword )) {
163
-
Serial.println("connected");
166
+
Serial.println("connected");
164
167
} else {
165
168
166
169
Serial.print("failed with state ");
@@ -169,29 +172,29 @@ We now connect the ESP8266 device to our IoT Hub. To do so, some additional libr
169
172
}
170
173
}
171
174
```
172
-
6. We test the publish/subscribe function by publishing a test message and subscribing to the topic:
175
+
6. We test the publish/subscribe function by publishing a test message and subscribing to the topic:
173
176
```
174
177
Serial.println("Sending payload hello-world...");
175
178
client.publish("esp/test", "hello-world"); //Topic name
176
179
Serial.println("Payload sent!");
177
180
client.subscribe("esp/test");
178
-
}
181
+
}
179
182
```
180
-
7. To print out the message we specify a call-back function and print the topic name and the received message on the Serial Monitor:
183
+
7. To print out the message we specify a call-back function and print the topic name and the received message on the Serial Monitor:
181
184
```
182
185
void callback(char* topic, byte* payload, unsigned int length) {
183
-
186
+
184
187
Serial.print("Message arrived in topic: ");
185
188
Serial.println(topic);
186
-
189
+
187
190
Serial.print("Message:");
188
191
for (int i = 0; i < length; i++) {
189
192
Serial.print((char)payload[i]);
190
193
}
191
-
194
+
192
195
Serial.println();
193
196
Serial.println("-----------------------");
194
-
197
+
195
198
}
196
199
```
197
200
8. Finish by adding the following lines in the loop:
@@ -201,21 +204,21 @@ We now connect the ESP8266 device to our IoT Hub. To do so, some additional libr
201
204
}
202
205
```
203
206
204
-
You should now have a sketch that begins with the following lines:
207
+
You should now have a sketch that begins with the following lines:
205
208
206
209
```
207
210
#include <ESP8266WiFi.h>
208
211
#include <PubSubClient.h>
209
212
```
210
213
211
-
and includes all the aforementioned code, ending with:
214
+
and includes all the aforementioned code, ending with:
212
215
213
216
```
214
217
void loop() {
215
218
client.loop();
216
219
}
217
220
```
218
-
9. Upload your code to your ESP8266 microcontroller. An output as in the following example displays in the Serial Monitor:
221
+
9. Upload your code to your ESP8266 microcontroller. An output as in the following example displays in the Serial Monitor:
219
222
<Lightboximage={image12}alt="" />
220
223
221
224
<Messagetype="note">
@@ -227,7 +230,7 @@ We now connect the ESP8266 device to our IoT Hub. To do so, some additional libr
227
230
228
231
## Controlling an LED and measuring temperature and humidity
229
232
230
-
Next, we will use our NodeMCU device to measure temperature and humidity and publish these values in a topic. Besides the NodeMCU device, you need the following hardware for this step:
233
+
Next, we will use our NodeMCU device to measure temperature and humidity and publish these values in a topic. Besides the NodeMCU device, you need the following hardware for this step:
231
234
232
235
* A [Breadboard](https://en.wikipedia.org/wiki/Breadboard)
233
236
* An [LED](https://en.wikipedia.org/wiki/Light-emitting_diode)
@@ -238,10 +241,10 @@ Next, we will use our NodeMCU device to measure temperature and humidity and pub
238
241
1. Perform the following wiring on the breadboard:
239
242
<Lightboximage={image14}alt="" />
240
243
241
-
This is the pinout table of the NodeMCU device:
244
+
This is the pinout table of the NodeMCU device:
242
245
243
246
| Pin Names on NodeMCU Development Kit| ESP8266 Internal GPIO Pin number|
244
-
| ------------- |:-------------:|
247
+
| ------------- |:-------------:|
245
248
|D0|GPIO16|
246
249
|D1|GPIO5|
247
250
|D2|GPIO4|
@@ -308,14 +311,14 @@ Next, we will use our NodeMCU device to measure temperature and humidity and pub
308
311
}
309
312
310
313
// This function is executed when some device publishes a message to a topic that your ESP8266 is subscribed to
311
-
// Change the function below to add logic to your program, so when a device publishes a message to a topic that
314
+
// Change the function below to add logic to your program, so when a device publishes a message to a topic that
312
315
// your ESP8266 is subscribed you can actually do something
313
316
void callback(String topic, byte* message, unsigned int length) {
314
317
Serial.print("Message arrived on topic: ");
315
318
Serial.print(topic);
316
319
Serial.print(". Message: ");
317
320
String messageTemp;
318
-
321
+
319
322
for (int i = 0; i < length; i++) {
320
323
Serial.print((char)message[i]);
321
324
messageTemp += (char)message[i];
@@ -340,14 +343,14 @@ Next, we will use our NodeMCU device to measure temperature and humidity and pub
340
343
}
341
344
342
345
// This functions reconnects your ESP8266 to your MQTT broker
343
-
// Change the function below if you want to subscribe to more topics with your ESP8266
346
+
// Change the function below if you want to subscribe to more topics with your ESP8266
344
347
void reconnect() {
345
348
// Loop until we're reconnected
346
349
while (!client.connected()) {
347
350
Serial.print("Attempting MQTT connection...");
348
351
// Attempt to connect
349
352
if (client.connect("ESP8266Client", mqttUser, mqttPassword )) {
350
-
Serial.println("connected");
353
+
Serial.println("connected");
351
354
// Subscribe or resubscribe to a topic
352
355
// You can subscribe to more topics (to control more LEDs in this example)
353
356
client.subscribe("room/lamp");
@@ -366,9 +369,9 @@ Next, we will use our NodeMCU device to measure temperature and humidity and pub
366
369
// The callback function is what receives messages and actually controls the LEDs
367
370
void setup() {
368
371
pinMode(lamp, OUTPUT);
369
-
372
+
370
373
dht.begin();
371
-
374
+
372
375
Serial.begin(115200);
373
376
setup_wifi();
374
377
client.setServer(mqttServer, mqttPort);
@@ -406,19 +409,19 @@ Next, we will use our NodeMCU device to measure temperature and humidity and pub
406
409
float hic=dht.computeHeatIndex(t, h, false);
407
410
static char temperatureTemp[7];
408
411
dtostrf(hic, 6, 2, temperatureTemp);
409
-
410
-
// Uncomment to compute temperature values in Fahrenheit
412
+
413
+
// Uncomment to compute temperature values in Fahrenheit
@@ -431,35 +434,35 @@ Next, we will use our NodeMCU device to measure temperature and humidity and pub
431
434
// Serial.print(hif);
432
435
// Serial.println(" *F");
433
436
}
434
-
}
437
+
}
435
438
```
436
439
3. Transfer the Arduino Sketch to the NodeMCU device and open the Serial Monitor.
437
440
4. The following output displays, confirming the successful capture and transmission of temperature and humidity data:
438
441
<Lightboximage={image15}alt="" />
439
442
440
443
## Flow Programming with Node-RED
441
444
442
-
In the following steps, we deploy a [Node-RED](https://nodered.org/) application using the IoT-Hub Kickstart feature and use it for flow programming.
445
+
In the following steps, we deploy a [Node-RED](https://nodered.org/) application using the IoT-Hub Kickstart feature and use it for flow programming.
443
446
444
-
1. Go back to your IoT Hub in the Scaleway console and click on the [Kickstart](https://console.scaleway.com/iot-hub/kickstarts/create) tab. Click **Create a Kickstart**:
447
+
1. Go back to your IoT Hub in the Scaleway console and click on the [Kickstart](https://console.scaleway.com/iot-hub/kickstarts/create) tab. Click **Create a Kickstart**:
445
448
<Lightboximage={image16}alt="" />
446
449
2. Select the **Flow Programming** Kickstart and choose your Hub and Device from the drop-down lists. Then select a region for your Kickstart to deploy. Click **Create** to deploy the Node-RED application on an [Instance](https://www.scaleway.com/en/virtual-instances/)
447
450
<Lightboximage={image17}alt="" />
448
-
3. Your IoT Kickstart application is deployed. The deployment may take some seconds, once it is ready, a green dot appears next to it. Click **Dashboard** to access the Node-RED dashboard:
451
+
3. Your IoT Kickstart application is deployed. The deployment may take some seconds, once it is ready, a green dot appears next to it. Click **Dashboard** to access the Node-RED dashboard:
449
452
<Lightboximage={image18}alt="" />
450
-
4. Log into Node Red using the credentials you have set during the deployment:
453
+
4. Log into Node Red using the credentials you have set during the deployment:
451
454
* Username: `admin`
452
455
* Password: `your-password`
453
-
5. Open the menu and click on **Manage Palette**:
456
+
5. Open the menu and click on **Manage Palette**:
454
457
<Lightboximage={image19}alt="" />
455
-
6. Find the `node-red-dashboard` and install it:
458
+
6. Find the `node-red-dashboard` and install it:
456
459
<Lightboximage={image20}alt="" />
457
-
7. Create a layout as follows:
460
+
7. Create a layout as follows:
458
461
<Lightboximage={image21}alt="" />
459
-
8. Then add the following nodes to a new flow:
462
+
8. Then add the following nodes to a new flow:
460
463
<Lightboximage={image22}alt="" />
461
464
462
-
The following elements are used:
465
+
The following elements are used:
463
466
464
467
*`switch` – this will control the ESP8266 output
465
468
*`mqtt output node` – this will publish a message to the ESP8266 according to the switch state
@@ -472,14 +475,14 @@ In the following steps, we deploy a [Node-RED](https://nodered.org/) application
472
475
<Lightboximage={image24}alt="" />
473
476
11. Configure the **MQTT input nodes** for `temperature` and `humidity`:
474
477
<Lightboximage={image25}alt="" />
475
-
12. Edit the **chart node** as follows:
478
+
12. Edit the **chart node** as follows:
476
479
<Lightboximage={image26}alt="" />
477
480
13. Edit the **gauge node** as follows:
478
481
<Lightboximage={image27}alt="" />
479
-
14. Link the nodes as follows:
482
+
14. Link the nodes as follows:
480
483
<Lightboximage={image28}alt="" />
481
484
15. Click **Deploy** to deploy the configuration.
482
-
16. Open a new browser tab and go to: `http://your-ip:1880/ui/`. A Dashboard showing temperature and humidity displays. You can use the toggle switch for `Light`. If toggled, a notification displays in the Serial Monitor of the NodeMCU device:
485
+
16. Open a new browser tab and go to: `http://your-ip:1880/ui/`. A Dashboard showing temperature and humidity displays. You can use the toggle switch for `Light`. If toggled, a notification displays in the Serial Monitor of the NodeMCU device:
0 commit comments