ThingSpeak is an open-source Internet of Things (IoT) application and API to store and retrieve data from things using the HTTP and MQTT protocol over the Internet or via a Local Area Network. ThingSpeak enables the creation of sensor logging applications, location tracking applications, and a social network of things with status updates.
1. Introduction
This tutorial explains how to set up accounts with ThingSpeak and the basics. ThingSpeak is an open cloud data platform where you can store and retrieve data.
2. ThingSpeak basics and account setup
URL : https://thingspeak.com/
Create a ThingSpeak account by visitng the url provided above. Click on the “Get Started For Free” button and sign up as new user.
>>Now, create a new channel by clicking on the button as shown in the below image – A channel is a source for your data. Where you can store and retrieve data. A channel can have a maximum of 8 fields. It means you can store 8 different data to a channel.
>>Create channel(s) to store data from sensor(s).
>>Scroll down and save the channel.
>>Channel Id is the identity of your channel. Note down this.
>>API ( Application Programming Interface ) keys are the keys to access your channel. In simple language, you can understand that these are passwords to access your channel. You can access your channel in two ways-
1. To update channel / data logging : API Write Key will be used to access in this mode.
2. To retrieve data : API Read Key will be used to access in this mode.
Click on the API tab to know your API keys.
3. Project using ThingSpeak
>> Description
This is a simple project that uses an ESP8266 module to send the sensor data to the ThingSpeak channel.
>>Demonatration
>>Hardware required
- Arduino Uno
- ESP8266 Wifi module
- Breadboard
- LDR
- LM35
- 220 ohms resistor
- Jumper cables
>>Circuit connections
ESP8266 connections
>>Connect TX pin of the module to pin 2 of the Arduino Uno and connect RX pin of the module to pin 3 on Uno.
>>Connect the Vcc and CH_EN pin of module to 3.3V on Arduino and GND pin of the module to the ground.
LDR connections
>>Connect the 220 ohms resistor and LDR in series and connect the 5v supply to the resistor and the other end of the LDR to the ground.
>>Connect the resistor-LDR junction to A0 pin on the Arduino.
LM35 connections
>>Connect the Vcc pin and the GND pin to the 5V and ground respectively.
>>Connect the Vout pin to A1 pin on the Arduino.
>>Code
#include <SoftwareSerial.h>
#define RX 2
#define TX 3
String AP = "Wifi_Name"; // AP NAME
String PASS = "Wifi_Pwd"; // AP PASSWORD
String API = "API key"; // Write API KEY
String HOST = "api.thingspeak.com";
String PORT = "80";
int countTrueCommand;
int countTimeCommand;
boolean found = false;
int valSensor = 1;
SoftwareSerial esp8266(RX,TX);
void setup() {
Serial.begin(9600);
esp8266.begin(115200);
sendCommand("AT",5,"OK");
sendCommand("AT+CWMODE=1",5,"OK");
sendCommand("AT+CWJAP=\""+ AP +"\",\""+ PASS +"\"",20,"OK");
}
void loop() {
String getData = "GET /update?api_key="+ API +"&field1="+getTemperatureValue()+"&field2="+getLDRValue();
sendCommand("AT+CIPMUX=1",5,"OK");
sendCommand("AT+CIPSTART=0,\"TCP\",\""+ HOST +"\","+ PORT,15,"OK");
sendCommand("AT+CIPSEND=0," +String(getData.length()+4),4,">");
esp8266.println(getData);delay(1500);countTrueCommand++;
sendCommand("AT+CIPCLOSE=0",5,"OK");
}
String getTemperatureValue()
{
int temp;
temp=analogRead(A1)/10.24;
return String(temp);
}
String getLDRValue()
{
int ldr=analogRead(A0);
return String(ldr);
}
void sendCommand(String command, int maxTime, char readReplay[]) {
Serial.print(countTrueCommand);
Serial.print(". at command => ");
Serial.print(command);
Serial.print(" ");
while(countTimeCommand < (maxTime*1))
{
esp8266.println(command);//at+cipsend
if(esp8266.find(readReplay))//ok
{
found = true;
break;
}
countTimeCommand++;
}
if(found == true)
{
Serial.println("OYI");
countTrueCommand++;
countTimeCommand = 0;
}
if(found == false)
{
Serial.println("Fail");
countTrueCommand = 0;
countTimeCommand = 0;
}
found = false;
}