IOT

Getting Started with Arduino IoT Cloud with ESP8266

The Arduino community has released Arduino IoT Cloud, an IoT platform. From start to finish, the Arduino IoT Cloud delivers an end-to-end solution that makes creating linked projects simple for makers, IoT enthusiasts, and professionals. HTTP REST API, MQTT, command-line tools, Javascript, and WebSockets are among the interaction methods supported by the platform. You can connect numerous devices together and have them share data in real time. A simple user interface allows you to monitor data from anywhere. You can read the documentation section to learn more about the Arduino IoT Cloud.

Basically, this is an introduction to Arduino IoT Cloud, in which we will learn how to use the Arduino IoT Cloud with an ESP8266.

In this tutorial, we’ll use the Arduino IoT Cloud Dashboard to operate an LED. Similarly, we’ll upload sensor data to the cloud dashboard and display it in a variety of attractive widgets. You can also use Arduino IoT Cloud to complete the Home Automation Project.

Hardware Required:

  • NodeMCU ESP8266-12E Board ow Wemos D1 Mini
  • 5mm LED
  • MPU6050 Gyroscope Accelerometer Sensor
  • Male/Female Jumper Wires
  • 830 Points Breadboard
  • Micro-USB Data Cable

Circuit Diagram & Hardware

As indicated in the image above, make a simple connection. Connect the MPU6050 to I2C and the LED to any digital pin, such as GPIO0.

Setting up Arduino IoT Cloud DashBoard

Let us set up the Arduino IoT Cloud dashboard. To do that visit the Arduino Digital Store Link.

You must first establish an Arduino account, or simply sign in if you already have one. Select IoT Cloud. You will be brought to the dashboard, where you must complete all of the necessary setup steps.

  • Creating Thing

Now press the Create Thing button. The setup part, as well as the sketch and serial monitor sections, are all shown here.

You must now create a variable and name it whatever you want. For example, if I want to operate an LED, I’ll call it LED. Select Light as the variable type since we need to transmit the 1 and 0 commands. Cloud Light LED will be immediately declared as the variable. The variable permissions can be set to read-write or read-only. As a result, you can assign both Read and Write to LED. Finally, press the Add Variable button. This will correctly create a variable.

Similarly, for MPU6050, add another variable to measure the tilt angle. So go ahead and click on add variable and call it X. Choose angle from the list of variables. As a result, a variable will be declared automatically. Because we are simply reading the sensor value, set the permission to read-only. Finally, select the add variable option. As a result, the second angle variable will be added.

  • Assign two additional variables to the Y and Z angles.

Setting Up Device

We must now configure the device. To do so, go to Device. We’ll use a 3rd party device because the ESP8266 is a third-party device. Select ESP8266 Board from the drop-down menu. Select your ESP8266 Type from the model. Then select Continue.

You can call the device whatever you want.

A device ID has been generated, as well as a Secret Key. While coding, they are extremely critical criteria. You can either copy it or download the PDF file.

Return to your dashboard and select Continue. So you’ve completed the device configuration.

Setting Up Network

We must now configure the network as well. So, select Network Setup. Here you should enter the WiFi SSID, Password, and Secret Key that you previously downloaded. Then press the save button.

Setting Up Dashboard

To configure the widgets, go to the dashboard.

Click on this edit sign. Then select Add here. Select switch from this widget list to turn on and off the LED. Give the switch any name you choose. Then, from the list, link a variable as LED. After that, select Link Variable.

Similarly, add three more widgets for the X, Y, and Z angles. For each of the three variables, you can choose from Gauge, Chart, or Numeric Value. Select a connected variable after giving widget any name. Select the minimum and maximum angle ranges from 0 to 360 from the value range.

So, our widget display dashboard is ready now.

Source Code/Program

The Arduino IoT Cloud with ESP8266 source code is simple to use because some commands and declarations are produced automatically.

Go to the sketch tab. As a result, a default drawing will be constructed depending on several criteria. Some header files have been defined, and LED parameters have been configured. As a result, we must change the code.

Below is the changed code for the three tabs.

Main_Code.ino

#include “arduino_secrets.h”
/*
Sketch generated by the Arduino IoT Cloud Thing “Untitled”
https://create.arduino.cc/cloud/things/3b2c44f9-e11e-4bea-9d37-e1ac566f55d3

Arduino IoT Cloud Variables description

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

CloudLight lED;
CloudAngle x;
CloudAngle y;
CloudAngle z;

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”
#include<Wire.h>

const int MPU_addr=0x68;
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
int minVal=265;
int maxVal=402;
double X;
double Y;
double Z;

void setup() {
// Initialize serial and wait for port to open:
Serial.begin(9600);
pinMode (0, OUTPUT);
Wire.begin();
Wire.beginTransmission(MPU_addr);
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
// 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
*/
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
}

void loop() {
ArduinoCloud.update();
MPU6050_Read();
// Your code here


}

void onLEDChange() {
if (lED == 1)
{
digitalWrite (0, HIGH);
}
else
{
digitalWrite (0, LOW);
}
// Do something
}

void MPU6050_Read()
{
Wire.beginTransmission(MPU_addr);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU_addr,14,true);
AcX=Wire.read()<<8|Wire.read();
AcY=Wire.read()<<8|Wire.read();
AcZ=Wire.read()<<8|Wire.read();
int xAng = map(AcX,minVal,maxVal,-90,90);
int yAng = map(AcY,minVal,maxVal,-90,90);
int zAng = map(AcZ,minVal,maxVal,-90,90);
X= RAD_TO_DEG * (atan2(-yAng, -zAng)+PI);
Y= RAD_TO_DEG * (atan2(-xAng, -zAng)+PI);
Z= RAD_TO_DEG * (atan2(-yAng, -xAng)+PI);

x=X;
y=Y;
z=Z;
Serial.print(“AngleX= “);
Serial.println(X);
Serial.print(“AngleY= “);
Serial.println(Y);
Serial.print(“AngleZ= “);
Serial.println(Z);
Serial.println(“—————————————–“);
}

thingProperties.h

// Code generated by Arduino IoT Cloud, DO NOT EDIT.

#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>


const char THING_ID[] = “3b2c44f9-e11e-4bea-9d37-e1ac566f55d3”;
const char DEVICE_LOGIN_NAME[] = “772ae50b-9678-465c-96c6-fa7c7d0d9aad”;

const char SSID[] = SECRET_SSID; // Network SSID (name)
const char PASS[] = SECRET_PASS; // Network password (use for WPA, or use as key for WEP)
const char DEVICE_KEY[] = SECRET_DEVICE_KEY; // Secret device password

void onLEDChange();

CloudLight lED;
CloudAngle x;
CloudAngle y;
CloudAngle z;

void initProperties(){

ArduinoCloud.setBoardId(DEVICE_LOGIN_NAME);
ArduinoCloud.setSecretDeviceKey(DEVICE_KEY);
ArduinoCloud.setThingId(THING_ID);
ArduinoCloud.addProperty(lED, READWRITE, ON_CHANGE, onLEDChange);
ArduinoCloud.addProperty(x, READ, ON_CHANGE, NULL);
ArduinoCloud.addProperty(y, READ, ON_CHANGE, NULL);
ArduinoCloud.addProperty(z, READ, ON_CHANGE, NULL);

}

arduino_secrets.h

#define SECRET_SSID “MyNetwork”
#define SECRET_PASS “12345678”
#define SECRET_DEVICE_KEY “9W5XHKSF8YAAO3OMDLFS”

Testing Arduino IoT Cloud ESP8266

You can now open the Serial Monitor after uploading the code. The MQTT Connection is formed, and the device is linked to the WFi Network, as seen on the serial monitor. The Tilt Angle for the X, Y, and Z-Axis is also displayed.

Arduino IoT Cloud ESP8266 Getting Started

Now go back to the dashboard to check the widget. As you can see all the data are uploaded here after a certain interval. To check the working tilt the sensor checks the value on the Serial Widget screen. Similarly, to turn ON and OFF the LED, send the command from the dashboard.

Conclusion:

I hope all of you had understand how to use the Arduino IoT Cloud with ESP8266. We MATHA ELECTRONICS will be back soon with more informative blogs soon.

Leave a Reply

Your email address will not be published.