8  ThingSpeak

Using ThingSpeak with ESP32

8.1 Introduction

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.

8.2 Creating a ThingSpeak Channel

  1. Sign in or create an account on the ThingSpeak website.
  2. Click on “Channels” and then click “New Channel”.
  3. 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.
  4. Click “Save Channel”.
  5. Once you have created your channel, you will be taken to the Channel page.
  6. On this page, you will see your Channel ID listed under the channel name.
  7. To get your Write API Key, click on the “API Keys” tab at the top of the screen.
  8. 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.

8.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);
}