1. Introduction
This is my first real project with an Arduino board where I will use the Arduino Nano 33 IoT to measure outdoor air temperatures and my fish pond temperatures. This remote outdoor temperature measurement device should provide the following functionality:
- temperatures available via the Homey Pro and the Arduino IoT cloud;
- using WiFi connectivity of the Arduino board;
- OLED display showing measured temperatures;
- power provided by an battery or accu;
- the board, OLED display and battery/accu will be placed inside a waterproof housing.
I am a beginner w.r.t. to Arduino boards and so to help others I provide my journey towards my end goal. It is not complicated if you have some basic understanding of one or more programming languages and interest in electronics.
2. Requirements
The following materials are required:
- Arduino Nano 33 IoT
- DS18B20 waterproof sensors
- Waterproof casing
- USB cable USB-A to USB mini to connect the computer with the Arduino board, while at same time providing the power for the board
- Accu
- More to come
3. Gathering knowledge
Thinking about the goal I have with this project a lot of questions about measuring temperatures with the Arduino Nano 33 IoT popup:
- What functionality does the Arduino board provide?
- How to connect the temperature sensor to the Arduino board and read the temperatures?
- How to connect the OLED display and get measured temperatures displayed?
- How to power the board with a battery or accu & which ones works the best at low ambient temperatures (below zero)?
- How to connect the board via WiFi?
- How to get the board connected to Arduino IoT cloud?
- How to make the measured temperatures available to Homey Pro?
- What is the maximum amount of DS18B20 temperature sensors I can connect?
- How do I know the accu is empty?
- How to get the device in a waterproof casing, including visible OLED display and wires going in/out?
The answers to these questions are provided in the next sections.
Additional information can be found on the Arduino website in the article Connecting the Nano 33 IoT to a Wi-Fi Network.
3.1 Functionality of the Arduino Nano 33 IoT
When looking around for the smallest Arduino board available and having interest in the Internet of Things (IoT) I decided to go for the Arduino Nano 33 IoT board for this project. I bought the book “Beginning Arduino Nano 33 IoT: Step-By-Step Internet of Things Projects” written by Agus Kurniawan. This book of approximately 180 pages does not have lots of technical details, but contains essential information in a snappy manner to get you on the road to build projects with this Arduino board. It provides also a summary of the Arduino development language.
For more technical details I went to the official Arduino website for this board.
3.2 How to connect the DS18B20 temperature sensor and read it out?
A half year ago I played already a little bit with the Arduino Uno R3 to measure temperatures with the DS18B20 sensor. This involved gathering knowledge about this sensor, which I published in my article “The DS18B20 digital temperature sensor“. To check for differences with the Arduino Nano 33 IoT I searched the web for others using this board with DS18B20 sensors and found a blog describing setting up a pool water monitoring system. Part 3 of this blog describes using the Arduino Nano 33 IoT with a DS18B20 sensor to measure temperature.
This article convinced me that my goal is possible and gave information how to make the correct connections. Based on the information collected so far I used the software Fritzing to create a schematic how to position and connect the parts on a breadboard. See the images below.
My DS18B20 sensor has 3 wires instead of 4 as shown in the schematic of the breadboard. The yellow wire (data) from my sensor is the same as the white wire in the schematic. The orange one in the schematic is not applicable. I connected the Arduino board to my computer via USB and started the Arduino IDE. I used the following code to test that temperatures are being measured.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
/* Measure temperature with one DS18B20 sensor on Arduino Nano 33 IoT (or other Arduino boards) Created by: Jan-Willem Kruse Date : 17-Dec-'21 Version : 1.00 Article : How to measure temperature with Arduino Nano 33 IoT and DS18B20 https://smarthome.familykruse.eu/temperature-arduino-nano-33-iot-ds18b20/ Note : Based on DS18B20 1-Wire digital temperature sensor with Arduino example code. More info: https://www.makerguides.com https://www.makerguides.com/ds18b20-arduino-tutorial/ */ // ### LOAD REQUIRED ARDUINO LIBRARIES ### #include <OneWire.h> // Library by Jim Studt etc. #include <DallasTemperature.h> // Library by Miles Burton etc. // ### INITIALISE ### // ## INITIALISE ONE WIRE BUS ## // Define pin D5 of the Arduino as the pin to which the 1-Wire bus is connected (the DQ data line of the DS18B20 temperature sensor) #define ONE_WIRE_BUS 5 // Create a new instance of the oneWire class to communicate with any OneWire device: OneWire oneWire(ONE_WIRE_BUS); // Pass the oneWire reference to DallasTemperature library: DallasTemperature sensors(&oneWire); // ## INITIALISE VARIABLES ## int deviceCount = 0; float tempC; float tempF; void setup() { Serial.begin(9600); // Begin serial communication at a baud rate of 9600 while (!Serial) { ; // wait for serial port to connect. Needed for native USB } sensors.begin(); // Start up the library } void loop() { // Send the command for all devices on the bus to perform a temperature conversion sensors.requestTemperatures(); // Fetch the temperature in degrees Celsius & Fahrenheit for device index, in this case 0 for the first and only device float tempC = sensors.getTempCByIndex(0); float tempF = sensors.getTempFByIndex(0); // Print the temperature in Celsius in the Serial Monitor Serial.print("Temperature: "); Serial.print(tempC); Serial.print(" °"); // shows degree symbol, if it does not work try Serial.print(" \xC2\xB0"); Serial.print("C | "); // Print the temperature in Fahrenheit Serial.print(tempF); Serial.print(" °"); // shows degree symbol, if it does not work try Serial.print(" \xC2\xB0"); Serial.println("F"); // Wait 1 second, as the temperature conversion in 12-bit mode can take up to 750 ms delay(1000); } |
An example of the output shown in the Arduino IDE serial monitor is shown in the next image.
3.3 Displaying temperature measurements on an OLED display
The final device should have a display to see measured temperatures locally next to seeing them via Homey Pro or the Arduino IoT cloud. I had already an OLED display with the following specifications:
- OLED 128×128 pixels, white text
- controller SSD1327
- works via I2C protocol
- required power 3.3V or 5V
- dimensions 45.5 x 34.3mm
I bought this display from the Dutch online store tinytronics.nl, where it is listed as “1.5 inch OLED Display 128*128 pixels wit – I2C” and costs €10 (2021 price). It is one of the few 128×128 OLED displays currently available.
3.3.1 Connecting
This OLED display has the following connections (I2C protocol):
- GND Ground
- VCC 3.3V or 5V power in
- SCL I2C SCL (clock signal)
- SDA I2C SDA (data signal)
The arduino has these connections in the signal lines indicated within a blue box in the schematic below.
So I connected the OLED display with the Arduino board:
OLED display | Arduino Nano 33 IoT | Remark |
GND | GND | Ground |
VCC | 3.3V out | VCC can be 3.3V or 5V power input |
SCL | A5 | I2C clock signal |
SDA | A4 | I2C data signal |
When I connected the wires, the OLED display remained dark (I had no worries) when the board was powered up. Time to get some test code up and running.
3.3.2 Displaying text
The supplier of the OLED display indicated that the Arduino library U8G2 library from olikraus should be used. So I installed this library via the Arduino IDE library manager (just search for U8G2). Additional information provided by the supplier of the OLED display:
- The Arduino Uno/Nano/Pro Mini do not have enough memory (RAM), hence it cannot be used with the “full_buffer” examples of the U8G2 library. Select instead the “page_buffer” examples
- Use the following initialisation under the section “U8g2 Constructor List” in the example code to use page-buffer:
U8G2_SSD1327_MIDAS_128X128_1_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
I started with a simple example showing “Hello World!” on the OLED display to check everything is working fine. The example code HelloWorld from the u8g2 library can be loaded in the Arduino IDE via File > Examples > U8g2 > page_buffer > HelloWorld. I modified the example code and cleaned it up to the bare minimum:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
/* Modified 'Hello World' example code from the u8g2 library to display 'Hello World!' text on OLED 128x128 I2C display Created by: Jan-Willem Kruse Date : 18-Dec-'21 Version : 1.00 Article : How to measure temperature with Arduino Nano 33 IoT and DS18B20 https://smarthome.familykruse.eu/temperature-arduino-nano-33-iot-ds18b20/ Note : - Based on 'HelloWorld.ino' example code from the Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/), - OLED display connected to Arduino Nano 33 IoT, but should work on other similar Arduino boards. - Compared with original sample code it has been modified to get it working with the OLED 128x128 display and not required code has been removed. */ // ### LOAD REQUIRED ARDUINO LIBRARIES ### #include <U8g2lib.h> // The Universal 8bit Graphics Library by olikraus #include <Wire.h> // Required for I2C protocol // ### SETUP U8G2 library ### // U8g2 Contructor List (Picture Loop Page Buffer) // Using page buffer as uses less RAM, so it works with all Arduino boards. In this case the Arduino Nano 33 IoT. U8G2_SSD1327_MIDAS_128X128_1_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE); void setup(void) { // Speedup display u8g2.setBusClock(400000); // Start u8g2.begin(); } void loop(void) { u8g2.firstPage(); do { u8g2.setFont(u8g2_font_ncenB10_tr); u8g2.drawStr(0,24,"Hello World!"); } while ( u8g2.nextPage() ); } |
3.3.3 Display temperatures continuously in terminal
To have a continuously ongoing display of measured temperatures I loaded the example code ‘Terminal’ from the u8g2 library via File > Examples > U8g2 > page_buffer > Terminal. This code was modified to include learnings from the previous sections Arduino code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
/* Measure temperature with one DS18B20 sensor on Arduino Nano 33 IoT and display results on OLED display terminal Created by: Jan-Willem Kruse Date : 18-Dec-'21 Version : 1.00 Article : How to measure temperature with Arduino Nano 33 IoT and DS18B20 https://smarthome.familykruse.eu/temperature-arduino-nano-33-iot-ds18b20/ Parts : - Arduino Nano 33 IoT - DS18B20 temperature sensor - 1.5" OLED display 128x128 I2C Note : - Based on DS18B20 1-Wire digital temperature sensor with Arduino example code. More info: https://www.makerguides.com https://www.makerguides.com/ds18b20-arduino-tutorial/ - Based on example code Terminal.ino from the Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/) */ // ### LOAD REQUIRED ARDUINO LIBRARIES ### #include <U8g2lib.h> #include <OneWire.h> // Library by Jim Studt etc. #include <DallasTemperature.h> // Library by Miles Burton etc. #include "RTCZero.h" // ### INITIALISE ### // ## SETUP ONE WIRE BUS ## // Define pin D5 of the Arduino as the pin to which the 1-Wire bus is connected (the DQ data line of the DS18B20 temperature sensor) #define ONE_WIRE_BUS 5 // Create a new instance of the oneWire class to communicate with any OneWire device: OneWire oneWire(ONE_WIRE_BUS); // Pass the oneWire reference to DallasTemperature library: DallasTemperature sensors(&oneWire); // ## SETUP OLED DISPLAY ## // U8g2lib using page buffer U8G2_SSD1327_MIDAS_128X128_1_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE); // setup the terminal (U8G2LOG) and connect to u8g2 for automatic refresh of the display // The size (width * height) depends on the selected font and the display U8G2LOG u8g2log; // assume 4x6 font #define U8LOG_WIDTH 32 #define U8LOG_HEIGHT 10 uint8_t u8log_buffer[U8LOG_WIDTH*U8LOG_HEIGHT]; // ## RTC CLOCK ## RTCZero alarm; // ## INITIALISE VARIABLES ## int deviceCount = 0; float tempC; float tempF; void setup() { // ### CONFIG OLED ### u8g2.setBusClock(900000); //speeds up OLED display. Started with 400000 and increased too 900000 u8g2.begin(); u8g2.setContrast(0); //Slightly les bright than max value of 255 form this OLED display u8g2.setFont(u8g2_font_helvR12_tf); // set the font for the terminal window u8g2log.begin(u8g2, U8LOG_WIDTH, U8LOG_HEIGHT, u8log_buffer); u8g2log.setLineHeightOffset(0); // set extra space between lines in pixel, this can be negative u8g2log.setRedrawMode(0); // 0: Update screen with newline, 1: Update screen for every char // ### CONFIG ONEWIRE ### sensors.begin(); // Start up the library // ### RTC CLOCK CONFIG ### alarm.begin(); } void loop() { char tm[16]; // ### GET TEMPERATURE ### // Send the command for all devices on the bus to perform a temperature conversion sensors.requestTemperatures(); // Fetch the temperature in degrees Celsius & Fahrenheit for device index, in this case 0 for the first and only device float tempC = sensors.getTempCByIndex(0); // the index 0 refers to the first device float tempF = sensors.getTempFByIndex(0); // ### DISPLAY TEMPERATURE ON OLED DISPLAY ### // Print the time since start of the Arduino board sprintf(tm, "%02d:%02d:%02d ", alarm.getHours(),alarm.getMinutes(),alarm.getSeconds()); u8g2log.print(tm); // Print the temperature in Celsius, modify if you want results in Fahrenheit u8g2log.print(tempC); u8g2log.print(" \xB0"); // shows degree symbol ° u8g2log.print("C\n"); // Wait 1 second, as the temperature conversion in 12-bit mode can take up to 750 ms delay(1000); } |
3.4 Battery powering the Arduino Nano 33 IoT?
Now we have a working device it is time to get it working standalone with its own power source. Looking on the web for ways how to power the Arduino Nano 33 IoT board with a battery and specially which voltage range is accepted before damaging the board was a daunting task. A lot of people seem to be confused by info w.r.t. max acceptable 3.3V on the analog and digital lines versus the power to supply to the board. It turns out that the power supply can be in the range of 4.5 – 21V DC as stated on the packaging of the Arduino board.
See also the Arduino forum article “Nano 33 IoT Vin Voltage range“.
3.4.1 Using a 9V battery
I started with a 9V battery and connected it as shown below.
The device works now without an USB connection providing the power. I left it running to check how fast the battery would drain. It turned out to be approximately 24 hours, so not useful in this setup. This does not worry me at this moment as it is likely caused by a combination of:
- continuously having on & updating the OLED display
- reading the sensor and updating the OLED display every second
This can be solved later and worst-case adding a button to press before something is shown on the OLED display.
3.4.1 Using a lithium ion accu
Another is the wish to use rechargeable accus. I have two 18650 Lithium ion rechargeable accus of each 3.7 Volt, which should roughly double when placed in series. I have a holder for these accus and measured the voltage, which is about 8.3 Volt in series when fully charged. So within the required power bandwidth of the Arduino Nano 33 IoT.
3.5 How to connect the board via WiFi?
3.5.1 Scan for available WiFi networks
Before the WiFi connection of the Arduino Nano 33 IoT can be used the WiFiNINA library needs to be installed in the Arduino IDE via the library manager. Search for “WiFiNINA” from Arduino.
With this library also new code examples are installed, which can be loaded in the Arduino IDE via File > Examples > WiFiNINA. There are 2 example codes to scan 802.11b/g WiFi networks available and list them in the Arduino IDE serial monitor:
- ScanNetworks
Displays all WiFi networks in range - ScanNetworksAdvanced
Displays all WiFi networks, including the encrypted ones.
Both example codes will only list available networks and their signal strengths, not connect to a network. Load the file ‘ScanNetworksAdvanced’. No modifications are required to the code, so upload it to the Arduino board and open the serial monitor in the Arduino IDE.
Note: In case the warning is displayed to update the WiFi controller firmware, see the Arduino website for the article “Check and update the firmware for WiFiNINA and WiFi101“.
3.5.2 Connect to a WiFi network
To connect to a WiFi network and obtain an IP address I started with the example code ‘ConnectWithWPA’ as my WiFi network uses WPA/WPA2 to encrypt. This script will also open the file ‘arduino_secrets.h’, which needs to be modified contain your WiFi network’s credentials. This file will be included in the main code when compiled before uploading to the Arduino board. The correct SSID can be fetched from the output created by the earlier used scan networks routine.
I modified this example code extensively based on information found in the WiFiNINA reference guide and info found elsewhere on the web to add:
- Extensive and nicely formatted WiFi information for the WiFi access point and the Arduino board within the Arduino IDE serial monitor;
- Set a network host name for the Arduino board;
- Every 5 seconds flash the internal orange LED of the Arduino Nano 33 IoT;
- Reconnect to the WiFi access point if the connection gets lost.
Download/copy the next two pieces of Arduino code and upload it to your Arduino Nano 33 IoT.
3.5.2.1 Arduino code part 1/2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 |
/* Connect Arduino Nano 33 IoT to a WiFi network and provide info via the serial monitor PART 1/2: Connect to WiFi.ino - main code Created by: Jan-Willem Kruse Date : 20-Dec-'21 Version : 1.00 Article : How to measure temperature with Arduino Nano 33 IoT and DS18B20 https://smarthome.familykruse.eu/temperature-arduino-nano-33-iot-ds18b20/ Parts : - Arduino Nano 33 IoT Note : - Based on example code ConnectWithWPA from the WiFiNINA library https://www.arduino.cc/en/Reference/WiFiNINA */ // ### LOAD REQUIRED ARDUINO LIBRARIES ### #include <SPI.h> #include <WiFiNINA.h> // ### INITIALISE ### // ## WIFI CONNECTION SETUP ## #include "arduino_secrets.h" // NOTE: enter your sensitive WiFi connection data in arduino_secrets.h char ssid[] = SECRET_SSID; // your network SSID (name) char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) int status = WL_IDLE_STATUS; // the WiFi radio's status char boardhostname[] = "Arduino-Nano-33-IoT-01"; byte LED=13; //Internal orange LED of the Ardduino Nano 33 IoT int discon=0; //Counter for nr of WiFi disconnects // ## DECLARE VARIABLES ## IPAddress ip; IPAddress subnet; IPAddress gateway; // ### START ## void setup() { // Initialize serial and wait for port to open: Serial.begin(9600); while (!Serial) { ; // wait for serial port to connect. Needed for native USB port only } // Setup internal orange LED pinMode(LED,OUTPUT); digitalWrite(LED,LOW); //LED is off if no WiFi connection // Connect to WiFi network CheckWiFiModule(); //Check WiFi module is available & firmware WiFi.setHostname(boardhostname); // Set host name of this Arduino board ConnectToWiFiNetwork(); // Connect to WiFi network and provide WiFi info in serial monitor } void loop() { // check the network status connection once every 10 seconds, flash the LED every 5 seconds delay(5000); heartBeat(); // Shortly flash the internal orange LED delay(5000); heartBeat(); // Check WiFi status status=WiFi.status(); printWiFiStatus(status); Serial.println(); // Reconnect if WiFi connection lost if (status != WL_CONNECTED) { discon=discon+1; // count disconnections digitalWrite(LED,LOW); ConnectToWiFiNetwork(); } } // --- SUB ROUTINES --- void CheckWiFiModule() { // ## CHECK IF WIFI MODULE IS AVAILABLE ### if (WiFi.status() == WL_NO_MODULE) { Serial.println("Communication with WiFi module failed!"); // don't continue while (true); } // ## CHECK WIFI MODULE FIRMWARE ## Serial.println(); Serial.println("WIFI MODULE FIRMWARE CHECK"); Serial.println("--------------------------"); String fv = WiFi.firmwareVersion(); Serial.print("Current firmware version: "); Serial.println(fv); Serial.print("Latest firmware version: "); Serial.println(WIFI_FIRMWARE_LATEST_VERSION); if (fv < WIFI_FIRMWARE_LATEST_VERSION) { Serial.println("Please upgrade the firmware"); } } void ConnectToWiFiNetwork() { Serial.println(); if (discon==0) { Serial.println("STARTING CONNECTING TO WIFI NETWORK"); Serial.println("-----------------------------------"); } else { Serial.println("STARTING RECONNECTING TO WIFI NETWORK"); Serial.println("-------------------------------------"); Serial.print("Reconnection attempt number: "); Serial.print(discon); Serial.println(); } while (status != WL_CONNECTED) { Serial.print("Attempting to connect to SSID with WPA/WPA2 encryption: "); Serial.println(ssid); // Connect to WPA/WPA2 network: status = WiFi.begin(ssid, pass); if (status != WL_CONNECTED) { int reason = int(WiFi.reasonCode()); Serial.print("Connection attempt failed, reason code: "); Serial.print(reason); Serial.print(" --> "); printReasonCode(reason); Serial.println(); Serial.println(); } // wait 10 seconds for connection: delay(10000); } // ## CONNECTION SUCCESFUL, PRINT INFO ## Serial.println("Connection attempt succesful, you're connected to the network."); digitalWrite(LED,HIGH); printWiFiNetworkInfo(); printArduinoBoardInfo(); // Header for the loop routine Serial.println("CHECKING WIFI CONNECTION EVERY 10 SECONDS"); Serial.println("-----------------------------------------"); } void printWiFiNetworkInfo() { Serial.println(""); Serial.println("WIFI NETWORK INFO"); Serial.println("-----------------"); // print the SSID of the network you're attached to: Serial.print("SSID: "); Serial.println(WiFi.SSID()); // print the MAC address of the router you're attached to: byte bssid[6]; WiFi.BSSID(bssid); Serial.print("MAC address: "); printMacAddress(bssid); Serial.print(" (BSSID)"); Serial.println(); // print your gateway address: gateway = WiFi.gatewayIP(); Serial.print("Gateway: "); Serial.println(gateway); // print the received signal strength: long rssi = WiFi.RSSI(); Serial.print("Signal strength: "); Serial.print(rssi); Serial.print(" dBm (RSSI)"); Serial.println(); // print the encryption type: byte encryption = WiFi.encryptionType(); Serial.print("Encryption: "); printEncryptionType(encryption); Serial.println(); Serial.println(); } void printArduinoBoardInfo() { Serial.println("ARDUINO BOARD WIFI INFO"); Serial.println("-----------------------"); // print your board's IP address: IPAddress ip = WiFi.localIP(); Serial.print("IP Address: "); Serial.println(ip); // print your subnet mask: subnet = WiFi.subnetMask(); Serial.print("Subnet: "); Serial.println(subnet); // print your MAC address: byte mac[6]; WiFi.macAddress(mac); Serial.print("MAC address: "); printMacAddress(mac); Serial.println(); // Print hostname of the board Serial.print("Host name: "); Serial.println(boardhostname); Serial.println(); } void heartBeat(){ // Flashes the internal orange LED shortly 3 times for (int t=0;t<3;t++){ digitalWrite(LED,LOW); delay(80); digitalWrite(LED,HIGH); delay(80); } } void printEncryptionType(int thisType) { // read the encryption type and print out the name: switch (thisType) { case ENC_TYPE_WEP: Serial.print("WEP"); break; case ENC_TYPE_TKIP: Serial.print("WPA"); break; case ENC_TYPE_CCMP: Serial.print("WPA2"); break; case ENC_TYPE_NONE: Serial.print("None"); break; case ENC_TYPE_AUTO: Serial.print("Auto"); break; case ENC_TYPE_UNKNOWN: default: Serial.print("Unknown"); break; } } void printReasonCode(int thisType){ // Reason codes from: https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/wifi.html switch (thisType) { case WIFI_REASON_UNSPECIFIED: //1 Serial.print("WIFI_REASON_UNSPECIFIED; "); Serial.print("Generally, it means an internal failure, e.g., the memory runs out, the internal TX fails, or the reason is received from the remote side, etc."); break; case WIFI_REASON_AUTH_EXPIRE: //2 Serial.print("WIFI_REASON_AUTH_EXPIRE; "); Serial.print("The previous authentication is no longer valid."); break; case WIFI_REASON_AUTH_LEAVE: //3 Serial.print("WIFI_REASON_AUTH_LEAVE; "); Serial.print("De-authenticated, because the sending station is leaving (or has left)."); break; case WIFI_REASON_ASSOC_EXPIRE: //4 Serial.print("WIFI_REASON_ASSOC_EXPIRE; "); Serial.print("Disassociated due to inactivity."); break; case WIFI_REASON_ASSOC_TOOMANY: //5 Serial.print("WIFI_REASON_ASSOC_TOOMANY; "); Serial.print("Disassociated, because the AP is unable to handle all currently associated STAs at the same time."); break; case WIFI_REASON_NOT_AUTHED: //6 Serial.print("WIFI_REASON_NOT_AUTHED; "); Serial.print("Class-2 frame received from a non-authenticated STA"); break; case WIFI_REASON_NOT_ASSOCED: //7 Serial.print("WIFI_REASON_NOT_ASSOCED; "); Serial.print("Class-3 frame received from a non-associated STA."); break; case WIFI_REASON_ASSOC_LEAVE: //8 Serial.print("WIFI_REASON_ASSOC_LEAVE; "); Serial.print("Disassociated, because the sending station is leaving (or has left) BSS."); break; case WIFI_REASON_ASSOC_NOT_AUTHED: //9 Serial.print("WIFI_REASON_ASSOC_NOT_AUTHED; "); Serial.print("station requesting (re)association is not authenticated by the responding STA."); break; case WIFI_REASON_DISASSOC_PWRCAP_BAD: //10 Serial.print("WIFI_REASON_DISASSOC_PWRCAP_BAD; "); Serial.print("Disassociated, because the information in the Power Capability element is unacceptable."); break; case WIFI_REASON_DISASSOC_SUPCHAN_BAD: //11 Serial.print("WIFI_REASON_DISASSOC_SUPCHAN_BAD; "); Serial.print("Disassociated, because the information in the Supported Channels element is unacceptable."); break; //code 12 not in use case WIFI_REASON_IE_INVALID: //13 Serial.print("WIFI_REASON_IE_INVALID; "); Serial.print("Invalid element, i.e. an element whose content does not meet the specifications of the Standard in frame formats clause."); break; case WIFI_REASON_MIC_FAILURE: //14 Serial.print("WIFI_REASON_MIC_FAILURE; "); Serial.print("Message integrity code (MIC) failure."); break; case WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT: //15 Serial.print("WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT; "); Serial.print("Four-way handshake times out. For legacy reasons, in ESP this reason-code is replaced with WIFI_REASON_HANDSHAKE_TIMEOUT."); break; case WIFI_REASON_GROUP_KEY_UPDATE_TIMEOUT: //16 Serial.print("WIFI_REASON_GROUP_KEY_UPDATE_TIMEOUT; "); Serial.print("Group-Key Handshake times out."); break; case WIFI_REASON_IE_IN_4WAY_DIFFERS: //17 Serial.print("WIFI_REASON_IE_IN_4WAY_DIFFERS; "); Serial.print("The element in the four-way handshake is different from the (Re-)Association Request/Probe and Response/Beacon frame."); break; case WIFI_REASON_GROUP_CIPHER_INVALID: //18 Serial.print("WIFI_REASON_GROUP_CIPHER_INVALID; "); Serial.print("Invalid group cipher."); break; case WIFI_REASON_PAIRWISE_CIPHER_INVALID: //19 Serial.print("WIFI_REASON_PAIRWISE_CIPHER_INVALID; "); Serial.print("Invalid pairwise cipher."); break; case WIFI_REASON_AKMP_INVALID: //20 Serial.print("WIFI_REASON_AKMP_INVALID; "); Serial.print("Invalid AKMP."); break; case WIFI_REASON_UNSUPP_RSN_IE_VERSION: //21 Serial.print("WIFI_REASON_UNSUPP_RSN_IE_VERSION; "); Serial.print("Unsupported RSNE version."); break; case WIFI_REASON_INVALID_RSN_IE_CAP: //22 Serial.print("WIFI_REASON_INVALID_RSN_IE_CAP; "); Serial.print("Invalid RSNE capabilities."); break; case WIFI_REASON_802_1X_AUTH_FAILED: //23 Serial.print("WIFI_REASON_802_1X_AUTH_FAILED; "); Serial.print("IEEE 802.1X. authentication failed."); break; case WIFI_REASON_CIPHER_SUITE_REJECTED: //24 Serial.print("WIFI_REASON_CIPHER_SUITE_REJECTED; "); Serial.print("Cipher suite rejected due to security policies."); break; case WIFI_REASON_BEACON_TIMEOUT: //200 Serial.print("WIFI_REASON_BEACON_TIMEOUT; "); Serial.print("When the station loses N beacons continuously, it will disrupt the connection and report this reason."); break; case WIFI_REASON_NO_AP_FOUND: //201 Serial.print("WIFI_REASON_NO_AP_FOUND; "); Serial.print("Target WiFi access point "); Serial.print(ssid); Serial.print(" is not available (yet)."); break; case WIFI_REASON_AUTH_FAIL: //202 Serial.print("WIFI_REASON_AUTH_FAIL; "); Serial.print("The authentication fails, but not because of a timeout."); break; case WIFI_REASON_ASSOC_FAIL: //203 Serial.print("WIFI_REASON_ASSOC_FAIL"); Serial.print("The association fails, but not because of ASSOC_EXPIRE or ASSOC_TOOMANY."); break; case WIFI_REASON_HANDSHAKE_TIMEOUT: //204 Serial.print("WIFI_REASON_HANDSHAKE_TIMEOUT"); Serial.print("The handshake fails for the same reason as that in WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT."); break; case WIFI_REASON_CONNECTION_FAIL: //205 Serial.print("WIFI_REASON_CONNECTION_FAIL"); Serial.print("The connection to the AP has failed."); break; default: Serial.print("Unknown code: "); Serial.print(thisType); break; } } void printWiFiStatus(int thisType){ switch (thisType) { case WL_CONNECTED: Serial.print("Connected to WiFi network"); break; case WL_AP_CONNECTED: Serial.print("Device is connected in Access Point mode"); break; case WL_AP_LISTENING: Serial.print("Listening for connections in Access Point mode"); break; /* case WL_NO_SHIELD: Serial.print("assigned when no WiFi shield is present"); break; */ case WL_NO_MODULE: Serial.print("Communication with integrated WiFi module failed"); break; case WL_IDLE_STATUS: Serial.print("Temporary status assigned when WiFi.begin() is called and remains active until the number of attempts expires (resulting in WL_CONNECT_FAILED) or a connection is established (resulting in WL_CONNECTED)"); break; case WL_NO_SSID_AVAIL: Serial.print("No SSID are available"); break; case WL_SCAN_COMPLETED: Serial.print("Scan networks is completed"); break; case WL_CONNECT_FAILED: Serial.print("Connection failed for all the attempts"); break; case WL_CONNECTION_LOST: Serial.print("Connection is lost"); break; case WL_DISCONNECTED: Serial.print("Disconnected from WiFi network"); break; default: Serial.print("Unknown WiFi status code: "); Serial.print(thisType); break; } } void printMacAddress(byte mac[]) { for (int i = 5; i >= 0; i--) { if (mac[i] < 16) { Serial.print("0"); } Serial.print(mac[i], HEX); if (i > 0) { Serial.print(":"); } } } |
3.5.2.2 Arduino code part 2/2
Do not forget to update the following two lines in the code below
1 2 |
#define SECRET_SSID "<YOUR WIFI ACCESS POINT SSID>" #define SECRET_PASS "<YOUR WIFI PASSWORD>" |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
/* Connect Arduino Nano 33 IoT to a WiFi network and provide info via the serial monitor PART 2/2: arduino_secrets.h - Include file Created by: Jan-Willem Kruse Date : 20-Dec-'21 Version : 1.00 Article : How to measure temperature with Arduino Nano 33 IoT and DS18B20 https://smarthome.familykruse.eu/temperature-arduino-nano-33-iot-ds18b20/ Parts : - Arduino Nano 33 IoT Note : - Based on example code ConnectWithWPA from the WiFiNINA library https://www.arduino.cc/en/Reference/WiFiNINA */ #define SECRET_SSID "<YOUR WIFI ACCESS POINT SSID>" #define SECRET_PASS "<YOUR WIFI PASSWORD>" /* source: https://www.esp32.com/viewtopic.php?t=349 https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/wifi.html#wi-fi-reason-code */ #define WIFI_REASON_UNSPECIFIED 1 #define WIFI_REASON_AUTH_EXPIRE 2 #define WIFI_REASON_AUTH_LEAVE 3 #define WIFI_REASON_ASSOC_EXPIRE 4 #define WIFI_REASON_ASSOC_TOOMANY 5 #define WIFI_REASON_NOT_AUTHED 6 #define WIFI_REASON_NOT_ASSOCED 7 #define WIFI_REASON_ASSOC_LEAVE 8 #define WIFI_REASON_ASSOC_NOT_AUTHED 9 #define WIFI_REASON_DISASSOC_PWRCAP_BAD 10 #define WIFI_REASON_DISASSOC_SUPCHAN_BAD 11 #define WIFI_REASON_IE_INVALID 13 #define WIFI_REASON_MIC_FAILURE 14 #define WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT 15 #define WIFI_REASON_GROUP_KEY_UPDATE_TIMEOUT 16 #define WIFI_REASON_IE_IN_4WAY_DIFFERS 17 #define WIFI_REASON_GROUP_CIPHER_INVALID 18 #define WIFI_REASON_PAIRWISE_CIPHER_INVALID 19 #define WIFI_REASON_AKMP_INVALID 20 #define WIFI_REASON_UNSUPP_RSN_IE_VERSION 21 #define WIFI_REASON_INVALID_RSN_IE_CAP 22 #define WIFI_REASON_802_1X_AUTH_FAILED 23 #define WIFI_REASON_CIPHER_SUITE_REJECTED 24 #define WIFI_REASON_BEACON_TIMEOUT 200 #define WIFI_REASON_NO_AP_FOUND 201 #define WIFI_REASON_AUTH_FAIL 202 #define WIFI_REASON_ASSOC_FAIL 203 #define WIFI_REASON_HANDSHAKE_TIMEOUT 204 #define WIFI_REASON_CONNECTION_FAIL 205 |
3.5.2.3 Serial monitor example output
Examples of the output in the serial monitor is shown below.
So far the code to read out the DS18B20 temperature sensor and connecting to the WiFi network has been created. It is now time to get some temperature measurements online via the Arduino IoT cloud.
3.5.2.4 Arduino code split-up
As the main code file becomes too large for easy handling I copied the WiFi routines to a separate file. So the files of the project are now:
- Connect to Homey Pro.ino –> main code
- jwWiFiRoutines.h –> WiFi routines
- arduino_secrets.h –> SSIDD and password for WiFi connection
Code for Homey Pro interfacing
3.6 Making temperature measurements available via the Arduino IoT cloud
Based on information provided in the article Getting started with the Arduino Nano 33 IoT , I created an Arduino Create account, installed the required Arduino Create plugin on my computer and went to the Arduino IoT Cloud website.
So if I would subscribe to the Maker Plan from The Netherlands I need to pay yearly $87 dollars including tax. Hmm… this is too expensive for me as there are cheaper alternatives available. Also data retention is only 3 months.
So I will first concentrate on interfacing with Homey Pro and maybe later look for free alternative IoT cloud options.
3.7 Interfacing with Homey Pro
Connecting Arduino Boards works by installing an app on Homey Pro; the Homeyduino app. This app allows you to connect your Arduino based creations with the Homey Pro. Next to installing the app on Homey Pro you also need to install the Homeyduino Arduino library on the Arduino board. A usage guide, Homeyduino API reference and a set of example projects are available. Flows can be created on the Homey Pro to define actions based on information received from an Arduino board.
The Homey Community forum has also posts w.r.t. using the Homeyduino app. Be aware that the company Athom is not supporting this app anymore, but it still works.
3.7.1 Test code to interface with the Homeyduino app
I loaded the example code “dht11-esp8266” via File > Examples > Homeyduino > Example projects > dht11. Although it cannot be used straight away for the Arduino Nano 33 IoT board, it is helpful in writing my own code for the Nano 33 IoT.
The setup routine contains:
1 2 3 |
Homey.begin("DHT11 sensor"); Homey.setClass("sensor"); Homey.addCapability("measure_temperature"); |
The loop routine contains:
1 2 3 4 5 6 |
Homey.loop(); unsigned long currentMillis = millis(); if(currentMillis - previousMillis > interval) { previousMillis = currentMillis; updateSensor(); } |
The update sensor routine contains:
1 |
Homey.setCapabilityValue("measure_temperature", (int) temperature); |
From the usage guide:
Homey.begin("devicename")
in thesetup()
routine: the device name entered should be unique for all Homeyduino devices in your network;Homey.loop()
in theloop()
routine: homey.loop function needs to be executed as often as possible, therefore use of Arduino’s delay() function should be avoided at all cost. Alternative delay methods are described in the Homeyduino usage guide;- The Arduino device can be integrated with Homey flows by defining triggers, conditions and actions which will be available directly in the Homey flows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
/* Connect Arduino Nano 33 IoT to a Homey Pro via WiFi Filename : Connect to Homey Pro.ino Created by: Jan-Willem Kruse Date : 20-Dec-'21 Version : 1.00 Includes : jwWiFiRoutines.h arduino_secrets.h (via jwWiFiRoutines.h) Article : How to measure temperature with Arduino Nano 33 IoT and DS18B20 https://smarthome.familykruse.eu/temperature-arduino-nano-33-iot-ds18b20/ Parts : - Arduino Nano 33 IoT Note : - Based on example code ConnectWithWPA from the WiFiNINA library https://www.arduino.cc/en/Reference/WiFiNINA */ // ### LOAD REQUIRED ARDUINO LIBRARIES ### #include <SPI.h> #include <WiFiNINA.h> #include <Homey.h> // ### INITIALISE ### char boardhostname[] = "Arduino-Nano-33-IoT-01"; byte LED=13; //Internal orange LED of the Ardduino Nano 33 IoT // const char* name = "JWKexample"; #include "jwWiFiRoutines.h" // ### START ## void setup() { // Initialize serial and wait for port to open: Serial.begin(9600); while (!Serial) { ; // wait for serial port to connect. Needed for native USB port only } // Setup internal orange LED pinMode(LED,OUTPUT); digitalWrite(LED,LOW); //LED is off if no WiFi connection // Connect to WiFi network CheckWiFiModule(); //Check WiFi module is available & firmware WiFi.setHostname(boardhostname); // Set host name of this Arduino board ConnectToWiFiNetwork(); // Connect to WiFi network and provide WiFi info in serial monitor // Homey Pro Homey.begin("DS18B20 sensor"); Homey.setClass("sensor"); Homey.addCapability("measure_temperature"); } void loop() { // check the network status connection once every 10 seconds, flash the LED every 5 seconds delay(5000); heartBeat(); // Shortly flash the internal orange LED Homey.loop(); Homey.setCapabilityValue("measure_temperature", 12); delay(5000); heartBeat(); Homey.setCapabilityValue("measure_temperature", 31); // Check WiFi status status=WiFi.status(); printWiFiStatus(status); Serial.println(); // Reconnect if WiFi connection lost if (status != WL_CONNECTED) { discon=discon+1; // count disconnections digitalWrite(LED,LOW); ConnectToWiFiNetwork(); } } |
When compiling the Arduino code I got the following error in the Arduino IDE:
fatal error: Ethernet2.h: No such file or directory #include <Ethernet2.h>
compilation terminated.
Hmm that is not what I wanted to see… The Homeyduino library info on the Arduino website states the following:
Homeyduino allows you to connect your Arduino projects with Homey.
This library communicates with the Homeyduino Homey app using an ethernet or WiFi connection, making it easier than ever to connect your projects with Homey. Homeyduino is compatible with all your regular Arduino boards, ESP8266 and ESP32 based boards.
Indicating it should work with the Arduino Nano 33 IoT. The site above also provides a link to the documentation, where under the section welcome you will find a link to the usage guide. This guide lists under “Hardware prerequisites” the following:
Homeyduino is compatible with all ESP8266 and ESP32 based development boards as well as Arduino boards compatible with the Arduino Ethernet v2 shield.
Support for using the legacy Arduino Ethernet v1 shield is also available, but you will need to change a configuration setting in one of the libraries files, click here for more information.
Other networked Arduino devices such as Arduino Yun and the Arduino GSM shield are currently not supported.
The software has been tested on the following board configurations:
-
- Arduino UNO with ethernet v2 shield
- Arduino Leonardo with ethernet v2 shield
- Arduino Due with ethernet v2 shield
- ESP8266 NodeMCU v1
- ESP32 devkit
This is in conflict with the previous statement; it is clear that the Arduino Nano 33 IoT with WiFiNINA is not supported. Is there a solution available to get it working?
3.7.2 Modifying the Homeyduino arduino source code
I started looking at the Homeyduino library at GitHub to see if people made comments about supporting the Arduino Nano 33 IoT. Unfortunately this was not the case, but there was a post w.r.t. adding support for arduino uno wifi (rev2) under “Pull requests”. Opening this post and selecting “Files changed” provides a clue where to look for to get the Arduino Nano 33 IoT supported.
The fix for the Arduino UNO WiFi refers also to the WiFiNINA library. So I need to find a similar definition as ARDUINO_ARCH_MEGAAVR, but then for the Arduino Nano 33 IoT board.
The definition seems to be related to the CPU of the board, which is for the Nano 33 IoT the Cortex-M0 32-bit SAMD21. I started by doing a file search in all the arduino related folders to look for “ARDUINO_ARCH_” to find the file containing these definitions. It turned out that there is not one file with these definitions, but that the definition is set in the arduino.h file within the folder for the CPU type (samd):
C:\Users\<USER>\AppData\Local\Arduino15\packages\arduino\hardware\samd\1.8.12\cores\arduino
Within this file the following code is found:
1 2 3 |
#ifndef ARDUINO_ARCH_SAMD #define ARDUINO_ARCH_SAMD #endif |
So likely the definition to add to the homey.h
file is “ARDUINO_ARCH_SAMD”. In the homey.h
file, before the following instructions:
1 2 |
#else #include <Ethernet2.h> |
I added:
1 2 3 4 5 6 7 8 9 |
#elif defined(ARDUINO_ARCH_SAMD) #include <WiFiNINA.h> #include <WiFiUdp.h> #define CLIENT_TYPE WiFiClient #define UDP_SERVER_TYPE WiFiUDP #define TCP_SERVER_TYPE WiFiServer #define UDP_TX_PACKET_MAX_SIZE 1024 #define MAXCALLBACKS 10 #define CAN_NOT_STOP_TCP |
Fingers crossed it will work.
Luckily id did. No compile errors anymore and hence I uploaded the code to the Arduino Nano 33 IoT. Later I discovered that the output of the compilation also shows some hints:
“C:\\Users\\ …/bin/arm-none-eabi-g++” … -DARDUINO_SAMD_NANO_33_IOT -DARDUINO_ARCH_SAMD …”
This indicates “ARDUINO_SAMD_NANO_33_IOT” should be more specific. Modifying the code (only the first line) above with this definition did work.
1 2 3 4 5 6 7 8 9 |
#elif defined(ARDUINO_SAMD_NANO_33_IOT) #include <WiFiNINA.h> #include <WiFiUdp.h> #define CLIENT_TYPE WiFiClient #define UDP_SERVER_TYPE WiFiUDP #define TCP_SERVER_TYPE WiFiServer #define UDP_TX_PACKET_MAX_SIZE 1024 #define MAXCALLBACKS 10 #define CAN_NOT_STOP_TCP |
Note: to assure the Arduino IDE re-compiles the source code of homey.h
, disable the inclusion of the homey.h library in the main code by using
1 |
//#include <Homey.h> |
Compile the code (which will give an error) and remove the //
and compile the code again. No errors should be shown now.
3.7.3 Connecting the test code with the Homeyduino app
Via the Homey Pro desktop I added the device “DS18B20 sensor” via the Homeyduino app. Every 5 seconds it changes the displayed temperature from 12°C to 31°C, as we told the Arduino board to do so in the code. So the test code works and it is time to get real temperature measurements.
3.7.4 Realtime temperature measurements
Work in progres 🙂
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
/* Arduino Nano 33 IoT - Realtime temperature at Homey Pro via WiFi Filename : Realtime temperature to Homey Pro.ino Created by: Jan-Willem Kruse Date : 22-Dec-'21 Version : 1.00 Includes : jwWiFiRoutines.h arduino_secrets.h (via jwWiFiRoutines.h) Article : How to measure temperature with Arduino Nano 33 IoT and DS18B20 https://smarthome.familykruse.eu/temperature-arduino-nano-33-iot-ds18b20/ Parts : - Arduino Nano 33 IoT - Dallas DS19B20 temperature sensor - Homey Pro Note : - Based on example code ConnectWithWPA from the WiFiNINA library https://www.arduino.cc/en/Reference/WiFiNINA */ // ### LOAD REQUIRED ARDUINO LIBRARIES ### #include <SPI.h> #include <WiFiNINA.h> #include <Homey.h> #include <OneWire.h> // Library by Jim Studt etc. #include <DallasTemperature.h> // Library by Miles Burton etc. // ### INITIALISE ### char boardhostname[] = "Arduino-Nano-33-IoT-01"; char homeyDeviceName[] = "Outdoor T Device 01"; byte LED=13; //Internal orange LED of the Ardduino Nano 33 IoT // const char* name = "JWKexample"; #include "jwWiFiRoutines.h" unsigned long previousMillis = 0; // Wait at least 1 second before reading out DS18B20 sensor, as the temperature conversion in 12-bit mode can take up to 750 ms const unsigned long interval = 10000; // Interval in milliseconds, 10 seconds // ## SETUP ONE WIRE BUS ## // Define pin D5 of the Arduino as the pin to which the 1-Wire bus is connected (the DQ data line of the DS18B20 temperature sensor) #define ONE_WIRE_BUS 5 // Create a new instance of the oneWire class to communicate with any OneWire device: OneWire oneWire(ONE_WIRE_BUS); // Pass the oneWire reference to DallasTemperature library: DallasTemperature sensors(&oneWire); // ## INITIALISE VARIABLES ## int deviceCount = 0; float tempC; float tempF; // ### START ## void setup() { // Initialize serial and wait for port to open: Serial.begin(9600); while (!Serial) { ; // wait for serial port to connect. Needed for native USB port only } // Setup internal orange LED pinMode(LED,OUTPUT); digitalWrite(LED,LOW); //LED is off if no WiFi connection // Connect to WiFi network CheckWiFiModule(); //Check WiFi module is available & firmware WiFi.setHostname(boardhostname); // Set host name of this Arduino board ConnectToWiFiNetwork(); // Connect to WiFi network and provide WiFi info in serial monitor // Homey Pro Homey.begin(homeyDeviceName); Homey.setClass("sensor"); Homey.addCapability("measure_temperature"); // DS18B20 sensors sensors.begin(); // Start up the library } void loop() { // ### WIFI ### // check the network status connection once every 4 seconds, flash the LED every 2 seconds delay(2000); heartBeat(); // Shortly flash the internal orange LED delay(2000); heartBeat(); // Check WiFi status status=WiFi.status(); printWiFiStatus(status); Serial.println(); // Reconnect if WiFi connection lost if (status != WL_CONNECTED) { discon=discon+1; // count disconnections digitalWrite(LED,LOW); ConnectToWiFiNetwork(); } // ### HOMEY ### Homey.loop(); unsigned long currentMillis = millis(); if(currentMillis - previousMillis > interval) { previousMillis = currentMillis; updateSensor(); } } // --- SUB ROUTINES --- void updateSensor() { // ### READ TEMPERATURE SENSORS ### // Send the command for all devices on the bus to perform a temperature conversion sensors.requestTemperatures(); // Fetch the temperature in degrees Celsius & Fahrenheit for device index, in this case 0 for the first and only device float tempC = sensors.getTempCByIndex(0); float tempF = sensors.getTempFByIndex(0); // ### OUTPUT TO SEERIAL MONITOR ### Serial.print("Temperature: "); Serial.print(tempC); Serial.print(" °"); // shows degree symbol, if it does not work try Serial.print(" \xC2\xB0"); Serial.print("C | "); // Print the temperature in Fahrenheit Serial.print(tempF); Serial.print(" °"); // shows degree symbol, if it does not work try Serial.print(" \xC2\xB0"); Serial.println("F"); // ### OUTPUT TO HOMEY ### Homey.setCapabilityValue("measure_temperature", tempC); Homey.trigger("temperature", tempC); } |
Code changes:
- Serial renamed to SerialUSB
- Interval to read temperature changed; measure every 5 minutes. if increase of more than 0.5c it will measure more frequent; each 30 seconds
- interval –> HomeyInterval
- defining multiple T sub devices: https://community.homey.app/t/homeyduino-device-with-two-temperature-sensors-registers-only-one/3721/13
Problem: which sensor is what?
Problem: Flow Cards will not be automatically generated for sub-capabilities, you should create these cards yourself.
0 Comments