In many scenarios we need to monitor state of a switch in a factory online. This can be done using IoT or IIoT(Industrial Internet of Things) platform such as the Arduino IoT platform or IBM IoT platform or custom Node-Red based IoT platform(see NodeMCU Node Red Industrial Iot platform) and there are many others IoT platform. In this educational tutorial we will show how to monitor the state of a switch using the Arduino IoT cloud platform. This is continuation of the previous tutorial Simple Arduino IoT cloud tutorial where a LED is turned on and off from the internet.
For this tutorial we are going to use NodeMCU ESP8266 as our IoT device to which the switch is connected. Once the switch state is changed it is detected by the ESP8266 IoT device and the state is reported to the Arduino IoT cloud via WiFi network. We can then see the state of the switch from anywhere where we have access to internet.
The switch is connected to the NodeMCUGPIO pin 13 which is D0 labelled on the board at one end and grounded on the other. The pin is also pulled high using 10KOhm pull-up resistor. So nornally the NodeMCU IoT device will read high or logical true and when the switch is connected the NodeMCU IoT device will read low or logical false.
On the breadboard the above circuit looks like this.
The process of making IoT application with Arduino IoT cloud platform are as follows.
1. Create a thing
2. Write Sketch
3. Create Dashboard
1. Create a thing
The first thing we need to do in Arduino IoT platform is to create a thing. The thing is created using the CREATE THING button.
You should see a new thing setup web page which is untitled. Here you have to provide some name for the IoT thing, add cloud variable that is to be tracked, add IoT device information and network credentials.
We will name the IoT thing as SWITCH and then click on the ADD VARIABLE to add a cloud variable which is shown below.
We have named the IoT cloud variable as SW, used the boolean data type and used the On change update policy. Now the thing web page shows the variable we just added.
Next we have to configure our IoT device which is NodeMCU ESP8266. This is added from the Associate Device panel. Click on the Select Device button.
Here you are presented with IoT device that the IoT cloud has detected from the previous use like the one shown above or you can use the SET UP NEW DEVICE to add your IoT device if this is the first time you are setting up. Since I had setup NodeMCU 1.0(ESP-12E module) earlier in tutorial Beginner guide to Arduino IoT cloud platform. Once you have added your IoT device it will appear in the Arduino IoT thing webpage as shown below.
The next step is to configure the network where we have to provide the SSID and password for the WiFi network and secret key which is unique to the connected IoT device. Click on the Configure button in the network section.
Then enter your WiFi network credentials and the secret key for your IoT device which was configured earlier.
At this point the IoT thing setup is complete. The next thing to do is to write the code in the sketch. Click on the sketch tab and there will be initial program code in the sketch. Here we will have to write the code that will monitor the state of the switch.
Just below the #include "thingProperties.h" add the following line which sets alias name swPin to the GPIO13 where the switch is connected.
const int swPin = 16;
Then in the setup() function write the following lines of code that will make the swPin as an input pin.
pinMode(swPin, INPUT);
These are shown below.
Then in the loop() function add the following lines of code which will read the state of the switch and print the state on serial monitor.
sw = digitalRead(swPin);
Serial.println(sw);
delay(100);
This is shown below.
The full sketch program code is below.
/*
Sketch generated by the Arduino IoT Cloud Thing "Untitled"
https://create.arduino.cc/cloud/things/6494352f-39f7-43ed-b61d-49a95b172a24
Arduino IoT Cloud Variables description
The following variables are automatically generated and updated when changes are made to the Thing
bool sw;
Variables 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 "thingProperties.h"
const int swPin = 16;
void setup() {
// 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);
pinMode(swPin, INPUT);
// 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
*/
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
}
void loop() {
ArduinoCloud.update();
// Your code here
sw = digitalRead(swPin);
Serial.println(sw);
delay(100);
}
The next step is to compile and upload the code. Click on the compile and upload code button.
After successful uploading of the program into NodeMCU IoT device you should see the message.
We can test whether the code works or not by opening the serial monitor. The serial monitor will show 1 when the switch is connected to the ground and 0 when the switch is connected to 3.3V.
The serial monitor is shown below.
This shows that the code is working and the switch is also working.
The next step is to create a dashboard. Click on the Dashboard and you will either empty list if you don't have made a dashboard previously or you will be presented with list of dashboard that you had build earlier. An example is shown below.
Click on the CREATE button to create a new dashboard for making a switch monitoring web interface.
The new dashboard is untitled so give it a sensible name like Switch Monitor.
Use the Add button to add a Status control.
Then in the Status control edit window, give the control some name like Switch.
Click on the Link Variable button in the Linked Variable section.
In the link variable window choose the SWITCH as the things and SW as the variable and then click the link variable button.
Finally click on Done button.
Now if the switch is not connected to the ground we will see ON on the status control.
If we connect the pin to the ground using the switch then the switch status changes to OFF as shown below.
So we have thus illustrated how you can monitor the state of a switch remotely using Arduino cloud IoT platform. We can also monitor state of LED or any sensor connected to the IoT device like NodeMCU ESP8266 IoT device used in this education tutorial.