14 Cloud connectivity
14.1 ThingSpeak
ThingSpeak is an open-source Internet of Things (IoT) platform that allows users to collect, analyze, and act on data from sensors or devices. It is a useful tool for monitoring and controlling devices in real-time, and it is commonly used in research projects, industrial automation, and smart homes. In this tutorial, we will introduce the basic concepts of ThingSpeak and demonstrate how to use it for data collection and visualization. In this tutorial, we will learn how to create a channel and upload data to it using an ESP32.
14.2 Creating a ThingSpeak Channel
- Sign in or create an account on the ThingSpeak website.
- Click on “Channels” and then click “New Channel”.
- Fill in the required fields, such as the name and description of the channel, as well as the fields for the data you want to collect. You can have up to 8 fields in a channel.
- Click “Save Channel”.
- Once you have created your channel, you will be taken to the Channel page.
- On this page, you will see your Channel ID listed under the channel name.
- To get your Write API Key, click on the “API Keys” tab at the top of the screen.
- Under the “Write API Key” section, you will see your unique Write API Key.
Make sure to keep your Write API Key private as it is used to allow your device to upload data to your ThingSpeak channel.
14.3 Uploading Data to ThingSpeak with ESP32
This code is an example of how to send data from an ESP32 microcontroller to the ThingSpeak platform using WiFi. The ESP32 reads sensor data (in this example, arbitrary values are set as fields) and sends them to the ThingSpeak server periodically. The code includes the necessary network credentials and ThingSpeak channel and API key information. The code is well-commented to explain what each part of the program does.
Download code#include <WiFi.h>
// Include the ThingSpeak library
#include "ThingSpeak.h"
// Set the ThingSpeak channel and API key information
unsigned long myChannelNumber = 1689148;
const char* myWriteAPIKey = "A58V29JE8UIGWI16";
// Set the WiFi network credentials
const char* ssid = "HUJI-guest"; // your wifi SSID name
const char* password = ""; // wifi password
// Set the ThingSpeak server address
const char* server = "api.thingspeak.com";
// Create a WiFiClient object to connect to the WiFi network
WiFiClient client;
// Set the time to wait between uploading data to ThingSpeak (in milliseconds)
int wait_between_uploads = 10000; // 10 seconds
void setup() {
// Start the serial communication at 112500 baud
Serial.begin(112500);
// Disconnect any previous WiFi connection
WiFi.disconnect();
delay(10);
// Connect to the WiFi network
WiFi.begin(ssid, password);
Serial.print("Connecting to ");
Serial.println(ssid);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("ESP32 connected to WiFi: ");
Serial.println(ssid);
Serial.println();
// Initialize the ThingSpeak library with the WiFi client
ThingSpeak.begin(client);
}
void loop() {
// Set the values to be sent to ThingSpeak
ThingSpeak.setField(1, 11);
ThingSpeak.setField(2, 22);
ThingSpeak.setField(3, 33);
ThingSpeak.setField(4, 44);
// Send the data to ThingSpeak
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
// Print a message to the serial monitor indicating that the data has been uploaded
Serial.println("Uploaded to ThingSpeak server.");
// Disconnect the WiFi client
client.stop();
// Wait for the specified amount of time before uploading the next set of data
// thingspeak needs minimum 15 sec delay between updates on a free acount,
// a paid acount can update every 1 sec
Serial.println("Waiting to upload next reading...");
Serial.println();
delay(wait_between_uploads);
}
14.4 Serial read and blink
Download code// Define the built-in LED pin
#define LED_PIN LED_BUILTIN // Most ESP32 boards use GPIO 2 for the built-in LED
void setup() {
// Initialize serial communication at 115200 baud
Serial.begin(115200);
// Wait for the serial monitor to connect
while (!Serial) {
; // Wait for serial port to connect. Needed for native USB port only
}
// Set the LED pin as an output
pinMode(LED_PIN, OUTPUT);
// Print instructions
Serial.println("Enter the number of times you want the LED to blink:");
}
void loop() {
// Check if data is available to read
if (Serial.available() > 0) {
// Read the integer from the serial monitor
int blinkCount = Serial.parseInt();
// Check if the parsed integer is greater than 0
if (blinkCount > 0) {
// Print the received number
Serial.print("Blinking LED ");
Serial.print(blinkCount);
Serial.println(" times");
// Blink the LED the specified number of times
for (int i = 0; i < blinkCount; i++) {
digitalWrite(LED_PIN, HIGH); // Turn on the LED
delay(500); // Wait for 500 milliseconds
digitalWrite(LED_PIN, LOW); // Turn off the LED
delay(500); // Wait for 500 milliseconds
}
// Print done message
Serial.println("Done blinking");
}
}
}
14.5 Basic Code for Uploading Data to Thingspeak
Download code// #include <ESP8266WiFi.h>
#include <WiFi.h>
#include "ThingSpeak.h"
unsigned long myChannelNumber = write_here_the_relevant_number;
const char * myWriteAPIKey = "write_here_the_correct_API_key";
const char* ssid = "wifi_name"; // your wifi SSID name
const char* password = "wifi_password" ;// wifi pasword
const char* server = "api.thingspeak.com";
WiFiClient client;
void setup() {
Serial.begin(115200);
WiFi.disconnect();
delay(10);
WiFi.begin(ssid, password);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
ThingSpeak.begin(client);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("NodeMcu connected to wifi...");
Serial.println(ssid);
Serial.println();
}
void loop() {
ThingSpeak.setField(1,11);
ThingSpeak.setField(2,22);
ThingSpeak.setField(3,33);
ThingSpeak.setField(4,44);
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
Serial.println("uploaded to Thingspeak server....");
client.stop();
Serial.println("Waiting to upload next reading...");
Serial.println();
// thingspeak needs minimum 15 sec delay between updates
delay(20000);
}
14.6 Uploading Thermistor Data to Thingspeak
Download code#include <WiFi.h>
// Include the ThingSpeak library
#include "ThingSpeak.h"
// Set the ThingSpeak channel and API key information
unsigned long myChannelNumber = channel_number_here;
const char* myWriteAPIKey = "API_key_here";
// Set the WiFi network credentials
const char* ssid = "wifi_name"; // your wifi SSID name
const char* password = "wifi_password"; // wifi password
// Set the ThingSpeak server address
const char* server = "api.thingspeak.com";
// Create a WiFiClient object to connect to the WiFi network
WiFiClient client;
// Set the time to wait between uploading data to ThingSpeak (in milliseconds)
int wait_between_uploads = 10000; // 10 seconds
// Thermistor setup------------------------------------------------------------------
const int analog_pin = A4; // Pin into which we connect the input voltage
int val ; // variable to store the value read
float v2; // variable to store the voltage of v2
int r1 = 10000; // R1 value (in the voltage divider)
float v1 = 3.3; // V1 value
int b_val = 4150; // B value of the resistor (from the data sheet)
float r2; // variable to store the resistance of r2 (thermistor resistance)
float t; // variable to store the temperature
int adc_steps = 4095; // the number of increments our ADC has (ESP32 = 12bit)
float r_25 = 10000; // the thermistor resistance at 25C (from the data sheet)
// function to convert resistance to temperature according to the b value
// equation in this link:
// https://www.thinksrs.com/downloads/programs/therm%20calc/ntccalibrator/ntccalculator.html
float resistance_to_temperature(int b, float r, float r_25) {
return b/(log(r/r_25)+(b/298.15))-273.15;
}
void setup() {
// Start the serial communication at 112500 baud
Serial.begin(112500);
// Disconnect any previous WiFi connection
WiFi.disconnect();
delay(10);
// Connect to the WiFi network
WiFi.begin(ssid, password);
Serial.print("Connecting to ");
Serial.println(ssid);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("ESP32 connected to WiFi: ");
Serial.println(ssid);
Serial.println();
// Initialize the ThingSpeak library with the WiFi client
ThingSpeak.begin(client);
}
void loop() {
val = analogRead(analog_pin); // read the input pin (reading v2)
v2 = (v1/adc_steps)*val; // convert to voltage of v2 according to ADC
r2 = (v2*r1)/(v1-v2);
t = resistance_to_temperature(b_val, r2, r_25);
Serial.print("Temperature: ");
Serial.print(t); // print temperature
Serial.println(" C");
// Set the values to be sent to ThingSpeak
ThingSpeak.setField(1, t);
// Send the data to ThingSpeak
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
// Print a message to the serial monitor indicating that the data has been uploaded
Serial.println("Uploaded to ThingSpeak server.");
// Disconnect the WiFi client
client.stop();
// Wait for the specified amount of time before uploading the next set of data
// thingspeak needs minimum 15 sec delay between updates on a free acount,
// a paid acount can update every 1 sec
Serial.println("Waiting to upload next reading...");
Serial.println();
delay(wait_between_uploads);
}