This is a continuation of the blog post I posted earlier this week Arduino MKR WiFi 1010 + MKR ENV Shield , as part of this blog post I using am using Arduino MKR 1010 (a MRK family of boards that has WiFi and Bluetooth), MKR Environment Shield to post sensor values to Arduino IoT cloud, and an I2C OLED to display sensor values and to show data posted on the Arduino IoT cloud. I have always wanted to try Arduino IoT cloud and finally thought that I should give it a try , given that I received the MKR Environment shield to Roadtest the Arduino MKR 1300 LoRa boards.
The setup was pretty simple, and this was my first time using Arduino Web Editor. I was surprised with all the feature's (here is link that details on the setup - https://www.arduino.cc/en/IoT-Prime/Experiment04 ). The only thing to note, is the 'feature usage' tab on the right, which shows you the amount of storage you have used for Sketches and other files, the number of sketches you have in the cloud, and also the number of compilations. I was using the editor to upload sketches and finally came across and error that said "you have exceeded the number of compilations" , then I figured out I had to upgrade to the Maker plan which had unlimited compilation time - which you can find more about at - https://store.arduino.cc/digital/create . In addition, I also found a tweet that said get a free upgrade now to Arduino create and followed the link that gave me Maker plan free for 3 months, here is the link https://blog.arduino.cc/2020/03/20/work-remotely-with-arduino-create-get-a-free-upgrade-now/, per the blog post it say you have till June 30th to use the voucher code , so hurry !!
Here is picture of the Arduino Cloud IoT dashboard that was setup with the sensor values from the ENV shield.
Now since I upgraded to the Maker account this meant i had access to more Properties - which are all boxes that you see above, which include senor values and other boxes like a switch that you can use to trigger something like a relay/Led connected to Arduino MKR 1010, the free account has 5 per thing, and the maker has 20 per thing.
In my scenario, I have an OLED display show sensor values from the MKR Environment shield, when the send message button in the cloud dashboard above turned OFF, here is a picture of the OLED , showing Temperature( C), humidity, Illuminance and UVIndex.
And to send a message to the Arduino MKR 1010 from the Arduino IoT cloud, I type the message in the message box and then turned ON the send message button cloud dashboard, this is basically setup in the Arduino sketch.
Here is the Arduino sketch that was put together in the Web editor, some of it was auto generate based on the properties setup in the IoT cloud, screenshot below the sketch
//Include the libs for simple I2C 128x32 oled. #include <Wire.h> #include "SSD1306Ascii.h" #include "SSD1306AsciiWire.h" // 0X3C+SA0 - 0x3C or 0x3D #define I2C_ADDRESS 0x3C // Define proper RST_PIN if required. #define RST_PIN -1 SSD1306AsciiWire oled; /* Sketch generated by the Arduino IoT Cloud Thing "MKR1010Home" https://create.arduino.cc/cloud/things/108fc99f-6b52-4751-97f3-43653107b351 Arduino IoT Cloud Properties description The following variables are automatically generated and updated when changes are made to the Thing properties String mesg; float humd; float illum; float temp; float uvind; bool led; Properties which are marked as READ/WRITE in the Cloud Thing will also have functions which are called when their values are changed from the Dashboard. These functions are generated with the Thing and added at the end of this sketch. */ #include <Arduino_MKRENV.h> #include "thingProperties.h" String vals =""; void setup() { //pinMode(LED_BUILTIN, OUTPUT); // Initialize serial and wait for port to open: Serial.begin(9600); // This delay gives the chance to wait for a Serial Monitor without blocking if none is found delay(1500); // Defined in thingProperties.h initProperties(); // Connect to Arduino IoT Cloud ArduinoCloud.begin(ArduinoIoTPreferredConnection); /* The following function allows you to obtain more information related to the state of network and IoT Cloud connection and errors the higher number the more granular information you’ll get. The default is 0 (only errors). Maximum is 4 */ Wire.begin(); Wire.setClock(400000L); #if RST_PIN >= 0 oled.begin(&Adafruit128x32, I2C_ADDRESS, RST_PIN); #else // RST_PIN >= 0 oled.begin(&Adafruit128x32, I2C_ADDRESS); #endif // RST_PIN >= 0 oled.setFont(Adafruit5x7); uint32_t m = micros(); oled.clear(); } void loop() { ArduinoCloud.update(); // Your code here if (!ENV.begin()) { Serial.println("Failed to initialize MKR ENV shield!"); while (1); } temp = ENV.readTemperature(); illum = ENV.readIlluminance(); humd = ENV.readHumidity(); uvind = ENV.readUVIndex(); oled.clear(); // print sensor values to Serial Monitor Serial.print("Temperature = "); Serial.print(temp); Serial.println(" C"); Serial.print("Humidity = "); Serial.print(humd); Serial.println(" %"); Serial.print("Illuminance = "); Serial.print(illum); Serial.println(" lx"); Serial.print("UVIndex = "); Serial.print(uvind); Serial.println(" "); Serial.print("Send Message = "); Serial.print(led); Serial.println(" "); Serial.print("Message from Cloud = "); Serial.print(mesg); Serial.println(" "); vals = "T: "+String(temp)+ "\nH:"+String(humd)+"\nI:" + String(illum)+ "\nUV:"+String(uvind); if(led){ oled.println(mesg); }else{ oled.println(vals); } delay(20); } void onLedChange() { } void onMesgChange() { }
In addition, once you hit the Edit sketch button a thingProperties.h file was auto-generated, here are the details, and in the secret tab, you have to update your WiFi SSID and Password.
#include <ArduinoIoTCloud.h> #include <Arduino_ConnectionHandler.h> const char THING_ID[] = "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxxx"; const char SSID[] = SECRET_SSID; // Network SSID (name) const char PASS[] = SECRET_PASS; // Network password (use for WPA, or use as key for WEP) void onMesgChange(); void onLedChange(); String mesg; float humd; float illum; float temp; float uvind; bool led; void initProperties(){ ArduinoCloud.setThingId(THING_ID); ArduinoCloud.addProperty(mesg, READWRITE, ON_CHANGE, onMesgChange); ArduinoCloud.addProperty(humd, READ, 30 * SECONDS, NULL); ArduinoCloud.addProperty(illum, READ, 30 * SECONDS, NULL); ArduinoCloud.addProperty(temp, READ, 30 * SECONDS, NULL); ArduinoCloud.addProperty(uvind, READ, 30 * SECONDS, NULL); ArduinoCloud.addProperty(led, READWRITE, ON_CHANGE, onLedChange); } WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);
Here is a screenshot of the fancy Web Editor -Serial Monitor in action, receiving a message from the Arduino IoT cloud.