The Internet of Things ( or IoT) is a network of interconnected computing devices such as digital machines, automobiles with built-in sensors, or humans with unique identifiers and the ability to communicate data over a network without human intervention.
Hello readers, I hope you all are doing great. In this tutorial, we will learn how to send sensor readings from ESP32 to the ThingSpeak cloud. Here we will use the ESP32’s internal sensor like hall-effect sensor and temperature sensor to observe the data and then will share that data cloud.
What is ThingSpeak?
It is an open data platform for IoT (Internet of Things). ThingSpeak is a web service operated by MathWorks where we can send sensor readings/data to the cloud. We can also visualize and act on the data (calculate the data) posted by the devices to ThingSpeak. The data can be stored in either private or public channels.
ThingSpeak is frequently used for internet of things prototyping and proof of concept systems that require analytics.
Features of ThingSpeak
- ThingSpeak service enables users to share analyzed data through public channels: Users can view multiple options on their channels via the settings panel. The tab displays sharing options, allowing the user to make their channel private, public or shared with specific users. Professionals can import and export data through their channels as well.
- ThingSpeak allows professionals to prepare and analyze data for their businesses: Weather forecasters use the MATLAB Analysis app to prepare, analyze, and filter data, such as estimating average humidity or calculating dew point. Users can use the visualization and analysis applications to perform operations on live or historical data by using template codes. To enable modular coding, industry professionals can add new functions to the software. Companies can use ThingSpeak Analysis to read stored data or write new data to their private channels. They can scrape numbers from various web pages thanks to the URL filter.
- ThingSpeak updates various ThingSpeak channels using MQTT and REST APIs: Professionals in the industry also use the platform to analyze and chart numerical data sent from smart devices and stored on various channels. Business owners can update their feeds, clear, or delete their channels entirely by using REST API calls like POST, GET, DELETE, or PUT. MQTT Publish methods allow users to update their feeds, whereas MQTT Subscribe methods allow them to receive messages.
Preparing Arduino IDE for ESP32 and ThingSpeak
- We are using Arduino IDE to compile and upload code into the ESP32 module. To know more about Arduino IDE and how to use it, follow our previous tutorial i.e., on the ESP32 programming series.
Downloading and installing the required Library file:
- Follow the link attached below to download theThingSpeak Arduino library:
https://github.com/mathworks/thingspeak-arduino
- Open the Arduino IDE.
- Go to Sketch >> Include Library >> Add .ZIP Library and select the downloaded zip file.
Fig. 2: Adding ThingSpeak Library
To check whether the library is successfully added or not:
- Go to Sketch >> Include Library >> Manage Libraries
Fig. 3
- Type thingspeak in the search bar.
Fig. 4: Arduino IDE Library Manager
- The ThingSpeak library by MathWorks has been successfully downloaded.
This library comes with multiple example codes. You can use any of the example codes as per your requirements ad also modify the example code.
Getting Started with ThingSpeak
- To create an account or log in to ThingSpeak (operated by MathWorks) server follow the link: https://thingspeak.com/
- Click on Get Started for free.
Fig. 6: Getting Started For Free
- Enter your details to create a MathWorks account as shown below:
Fig. 7: Create New Account
- If you have already created a MathWorks account, then click on Sign in.
Fig. 8: MathWorks Sign in
- Create a channel by clicking on the New Channel
Fig. 9: New Channel
- Enter the respective details in the channel.
- As we already mentioned, we will use ESP32’s inbuilt sensors, Hall and temperature sensor to take the readings and then publish them to the ThingSpeak server.
- So we are using two files, field1 and field2 for temperature and hall readings respectively.
- You can use/enable more than two fields as per your requirements.
Fig. 10: Enter the Details in Channel
- Click o the save button to save the channel details.
- After successfully saving the channel, a new window will open containing the channel details and Channel Stats.
- In the same window, go to API Keys which contains the Write API keys and Read API keys.
- Copy the Write API key and paste this in ESP32 Arduino code to send the sensor values to ThingSpeak.
- You can also customize the chart in Private View. Click on the icon present at the top-right menu of Field Chart (in red box) to edit the chart.
- Edit the details as per your requirements and click on the save button to save the details.
Fig. 13: Field Chart Edit
Arduino Code
We have already published a tutorial on the ESP32 hall sensor and internal temperature sensor.
// ——style guard —-
#ifdef __cplusplus
extern “C”
{
#endif
uint8_t temprature_sens_read();
#ifdef __cplusplus
}
#endif
uint8_t temprature_sens_read();
// ——header files—-
#include <WiFi.h>
#include “ThingSpeak.h”
// —–netwrok credentials
const char* ssid = “SSID”; // your network SSID (name)
const char* password = “PASSWORD”; // your network password
WiFiClient client;
// —–ThingSpeak channel details
unsigned long myChannelNumber = 1;
const char * myWriteAPIKey = “API Key”;
// —– Timer variables
unsigned long lastTime = 0;
unsigned long timerDelay = 1000;
void setup()
{
Serial.begin(115200); // Initialize serial
WiFi.mode(WIFI_STA);
if(WiFi.status() != WL_CONNECTED)
{
Serial.print(“Attempting to connect”);
while(WiFi.status() != WL_CONNECTED )
{
WiFi.begin(ssid, password);
delay(1000);
}
Serial.println(“\nConnected. “);
}
ThingSpeak.begin(client); // Initialize ThingSpeak
}
void loop()
{
if ((millis() – lastTime) > timerDelay )
{
int hall_value = 0;
float temperature = 0;
hall_value = hallRead();
// Get a new temperature reading
temperature = ((temprature_sens_read()-32)/1.8 );
Serial.print(“Temperature (ºC): ” );
Serial.print(temperature);
Serial.println(“ºC” );
Serial.print(“Hall value:” );
Serial.println(hall_value);
ThingSpeak.setField(1, temperature );
ThingSpeak.setField(2, hall_value );
// Write to ThingSpeak. There are up to 8 fields in a channel, allowing you to store up to 8 different
// pieces of information in a channel. Here, we write to field 1.
int x = ThingSpeak.writeFields(myChannelNumber,
myWriteAPIKey );
if(x == 200)
{
Serial.println(“Channel update successful.” );
}
else
{
Serial.println(“Problem updating channel. HTTP error code ” + String(x) );
}
lastTime = millis();
}
}
Code Description
- Style guard is used at the beginning to declare some function to be of “C” linkage, instead of “C++” Basically, to allow C++ code to interface with C code.
- Add the required header files.
- We have already discussed above how to download and add the ThingSpeak library file to Arduino IDE.
- Enter the network credentials (SSID and Password).
- A Wi-Fi client is created to connect with ThingSpeak.
- Define timer variables.
Fig. 18
- Add the channel number and API (Write) Key. If you have created only one channel then the channel number will be ‘1’.
Fig. 19
Setup()
-
- Initialize the Serial monitor with a 115200 baud rate for debugging purposes.
Fig. 20
- Set ESP32 Wi-Fi module in station mode using mode() function.
- Enable ESP32’s Wi-Fi module using begin() function which is using SSID and password as arguments.
- Wait until the ESP32 is not connected with the wifi network.
Fig. 21
- Initialize the ThingSpeak server using begin() function that is passing client (globally created) as an argument.
Fig. 22
Loop()
-
- Inside the loop() function, define an integer type variable to store the hall sensor readings.
Fig. 23
- Define another float type variable to store temperature readings.
Fig. 24
- Call the hallRead() function to store the hall sensor readings into hall_value
Fig. 25
- Temperature_sens_read() function is used to read the temperature of ESP32 core.
- Temperature observed by the internal temperature sensor is in Fahrenheit
- o convert observed temperature i.e., in Fahrenheit into Celsius :
(F-32) *(5/9) = degree Celsius
Fig. 26
- Print the temperature in degree Celsius and Hall sensor observations on serial moitor.
Fig. 27
- Set the number of fields you have created to the thingSpeak server. We are adding only two fields. You can add up to maximum of 8 fields for different readings.
Fig. 28
- writeFields() function is used to write data to the ThingSpeak server. This function is using the channel number and API key as an argument.
Fig. 29
- Return the code 200 if the sensor readings are successfully published to ThingSpeak server and print the respective results on the serial monitor.
Fig. 30
Testing
- Connect the ESP32 module with your laptop using USB cable.
- Select the right development board in Tools >> Boards >> DOIT ESP32 DevKit V1.
- Compile and upload the code into ESP32 using Arduino IDE.
- Make sure that you have entered the right Wi-Fi credentials, API key and channel number before uploading the code.
- Open the ThingSpeak website where you have created a channel and check the sensor readings.
- A screenshot of the field chart we have created is show below. Where you can see the temperature and hall sensor values on the chart.
Fig. 31: ThingSpeak Channel Stats
- To see the sensor values on Arduino IDE open the serial monitor with a 115200 baud rate.
Fig. 32: Results on the Serial Monitor
This concludes the tutorial. I hope you found this of some help and also to see you soon with new tutorial on ESP32.
JLCPCB – Prototype 10 PCBs for $2 (For Any Color)
China’s Largest PCB Prototype Enterprise, 600,000+ Customers & 10,000+ Online Orders Daily
How to Get PCB Cash Coupon from JLCPCB: https://bit.ly/2GMCH9w
The post Sending Data to Cloud with ESP32 and ThingSpeak appeared first on The Engineering Projects.
No comments:
Post a Comment