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 = 15000; // 15 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 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);
}14.5 Uploading SHT31 Data to Thingspeak
Download code#include <Arduino.h>
#include <Wire.h>
#include <WiFi.h>
#include "ThingSpeak.h"
#include "Adafruit_SHT31.h"
// ── thingspeak settings ───────────────────────────────────────────
unsigned long myChannelNumber = 1689148; // your channel number
const char* myWriteAPIKey = "A58V29JE8UIGWI16"; // your write api key
// ──────────────────────────────────────────────────────────────────
// ── wifi settings ─────────────────────────────────────────────────
const char* ssid = "HUJI-guest"; // your wifi ssid
const char* password = ""; // wifi password (if any)
// ──────────────────────────────────────────────────────────────────
// sht31 sensor object
Adafruit_SHT31 sht31 = Adafruit_SHT31();
// wifi client for thingspeak
WiFiClient client;
// time between uploads (milliseconds)
int wait_between_uploads = 15000; // 15 seconds (minimum for free account)
void setup() {
// start serial communication
Serial.begin(115200);
// start i2c and sht31 sensor
if (!sht31.begin(0x44)) { // default i2c address
Serial.println("error: sht31 not found");
} else {
Serial.println("sht31 sensor initialized");
}
// connect to wifi
WiFi.disconnect(true);
delay(100);
Serial.print("connecting to wifi: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("wifi connected, ip address: ");
Serial.println(WiFi.localIP());
Serial.println();
// initialize thingspeak
ThingSpeak.begin(client);
}
void loop() {
// read temperature and humidity from sht31
float temperature = sht31.readTemperature();
float humidity = sht31.readHumidity();
// check for valid readings
if (isnan(temperature) || isnan(humidity)) {
Serial.println("error: failed to read from sht31");
} else {
Serial.print("temp_deg_C: ");
Serial.print(temperature, 2);
Serial.print(" rel_hum_%: ");
Serial.println(humidity, 2);
// set fields to send to thingspeak
ThingSpeak.setField(1, temperature); // field 1: temperature
ThingSpeak.setField(2, humidity); // field 2: humidity
// write to thingspeak
int httpCode = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if (httpCode == 200) {
Serial.println("uploaded to thingspeak.");
} else {
Serial.print("upload failed, http error code: ");
Serial.println(httpCode);
}
}
Serial.println("waiting to upload next reading...");
Serial.println();
// stop client connection (optional)
client.stop();
// wait before next upload
delay(wait_between_uploads);
}