fbpx

ESP8266-THINKSPEAK

ThingSpeak is an open-source IoT analytics application service that allows you to aggregate, visualize and analyze live data streams in the cloud from things(ESP) using the HTTP protocol over the Internet or via a Local Area Network.

Thinkspeak Platform

  • Lets post adc value to the thinkspeak dashboard.
  • Create account in thinkspeak: https://thingspeak.com
  • Click New Channel to create a new ThingSpeak channel.

Give the following necessary data in the field:   

  • Provide a unique name for the ThingSpeak channel. 
  •  Provide a description of the ThingSpeak channel.   
  • Enter a field name.
  • Each ThingSpeak channel can have up to 8 fields. 

After filling all necessary data save the channel information.To post the ADC value to server from ESP8266 we need API key of thinkspeak channel

Use this key to write data from esp8266 to a channel. 

After successfully creating a thinkspeak channel, a field chart will be generated to display data.

The following chart show the adc value posted from esp8266 at different time period.

Program

 #include <ESP8266WiFi.h>
String apiKey = "2T86TRZ5W3JH3866"; /* Enter your Write API key from ThingSpeak */
const char *ssid = "Imake";                    /* replace with your wifi username */
const char *pass = "********";                 /* replace with your wifi password */
const char* server = "api.thingspeak.com"; 


WiFiClient client;
int sensorValue = 0;  /*  variable to store the value coming from the ADC */


void setup()
{
Serial.begin(115200);
delay(10);
Serial.println("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
}


void loop()
{
sensorValue = analogRead(A0);
if (client.connect(server, 80)) // "184.106.153.149" or api.thingspeak.com
{
String postStr = apiKey;
postStr += "&field1=";
postStr += String(sensorValue);
postStr += "\r\n";


client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
delay(2000);
}
client.stop();
delay(10000);
} 

Leave a Reply

Your email address will not be published. Required fields are marked *