ad space

Simple Arduino IoT cloud tutorial

 In this simple Arduino IoT cloud tutorial we will show how to turn on or off a LED connected to NodeMCU ESP8266 board. This is a beginner tutorial on how to use Arduino IoT platform to control devices such as LED or switch or motor etc from the internet. 

To start with, connect a LED with 220Ohm resistor to the pin 13(D7) of NodeMCU ESP8266 IoT board as shown below.

NodeMCU Arduino IoT
 On the actual boardboard it will looks similar to the one shown in the picture below.

 

NodeMCU IoT Arduino

Now open the Arduino IoT cloud and from the dashboard, click on the things tab.


 Then the things list table will show up like the one below. In the below picture you can see that there is already one things present which is the DHT11 that was created in the earlier tutorial Beginner guide to Arduino IoT cloud platform. Click on the Create button to create a new thing.

Then you will see the new thing creation web page. The thing name is untitled which we will change to LEDControl and we will create a new cloud variable by clicking on the ADD VARIABLE.


In the new variable window, name the variable as myLED,then select the data type as boolean, select the variable permission as Read and Write, set the variable update policy to On Change and finally click on Add Variable as shown below.

arduino IoT platform

After this you should see that the new thing has a cloud variable called myLED which was just created.

The next step is to associate the IoT connected device which in this tutorial is NodeMCU ESP8266.

To add new IoT connected device click on the Select Device.

 In the new window either select the detected NodeMCU device or use the SET UP NEW DEVICE button to search and select your IoT device.

IoT Connected Device

Once you have selected your IoT connected device it will appear under the Associate Device in the LEDControl thing webpage.

The next step is to setup the IoT network credentials that is your WiFi network credentials. Click on the Configure button in the Network section.

In the configure network window, enter your WiFi SSID and password and also enter your secret key that was provided to you.

IoT network credentials

At this point we have set up our variables, configured the IoT device and the IoT network credentials and thus completed the Setup part. Next we move on the programing part. 

Click on the Sketch tab and you will see program code generated by Arduino cloud IoT platform.


In the sketch write const int my_LED = 13; just under the #include "thingsProperties.h" line and write pinMode(my_LED, OUTPUT); in the setup() function as shown below.


 Near the bottom of the program a self generated function called onMyLEDChange() in this case will be there. Inside the function write the following lines of code.

void onMyLEDChange()  {
  // Add your code here to act upon MyLED change
  Serial.println(myLED);
 
  if(myLED)
    digitalWrite(my_LED,HIGH);
  else
    digitalWrite(my_LED,LOW);
}

The complete code is provided below.


/* 
  Sketch generated by the Arduino IoT Cloud Thing "Untitled"
  https://create.arduino.cc/cloud/things/c1aeddb7-451b-4e85-8402-de878412c455 

  Arduino IoT Cloud Variables description

  The following variables are automatically generated and updated when changes are made to the Thing

  bool myLED;

  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 my_LED = 13;

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(my_LED, OUTPUT);

  // 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 
  
  
}


/*
  Since MyLED is READ_WRITE variable, onMyLEDChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onMyLEDChange()  {
  // Add your code here to act upon MyLED change
  Serial.println(myLED);
  
  if(myLED)
    digitalWrite(my_LED,HIGH);
  else
    digitalWrite(my_LED,LOW);
}

Click on the compile and upload button to upload the code to the IoT device NodeMCU.


Once completed you should see upload successful message at the bottom. 


 

The next step is to build the web interface for IoT monitoring and controlling. Here we have to build IoT control web page for controlling the LED. This is done via the dashboard tab.

Picture below shows one dashboard already created for DHT11 sensor. Click on the Create button create a new dashboard for LED control.

The new dashboard is untitled and we add new control using the ADD button.


Name the dashboard like LEDcontrol and click on the ADD button to add a switch control.

 
 

 
Click on the edit setting.
 

Change the name of the control to LED and click on the Link variable button.

In the link variable option choose the LEDControl things, choose the cloud variable myLED and click on LINK VARIABLE as shown below.


Then click on Done.

The next step is to verify the IoT connectivity and check the application. Initially when the switch is off the LED is off as shown below.



Now when the switch is turned on the LED will be turned on.

This illustrates that the IoT application with Arduino IoT platform is working. Hence here we have here demonstrated how to turn on or off a LED connected to NodeMCU ESP8266 board using Arduino IoT cloud platform.

In this tutorial we have used Arduino cloud IoT platform to make IoT application. There are several other IoT platform such as google IoT platform, IBM IoT platform etc which can be similarly be used for home of industrial IoT. Another way is to create your own IoT platform with NodeRed or Proteus by hosting your own webserver on a local PC. Example of making your own IoT application with Node Red IoT platform are illustrated in the tutorials Display Temperature on Web with NodeMCU, LM35 and Node-Red and Node-Red ESP8266 MQTT Publish Example. Proteus Professional IoT platform can also be used to build IoT application which are demonstrated in the tutorial Arduino ESP8266 IoT development platform with Proteus and Temperature Logger with Proteus IIoT platform.



Post a Comment

Previous Post Next Post