In this article we will learn how to implement Tweeting Senor Data with NodeMCU EPS8266 and ThingSpeak. If your are new to Internet of Things (IoT), learn about IoT by visiting our Internet of Things tutorial for beginners.
Contents
Aim of Experiment
To tweet sensors data on Twitter using NodeMCU and ThingSpeak.
Components Required
- NodeMCU – 1
- DHT11 Sensor – 1
- Breadboard – 1
Connections Diagram (Schematic)
Code (C++/Arduino IDE)
#include<DHT.h>
#include<ESP8266WiFi.h>
//Use ThingTweet App in ThingsSpeak to get the below API key
String apiKey = "APIKEY";
const char* ssid = "ssid";
const char* password = "password";
const char* server = "api.thingspeak.com";
#define DHTTYPE DHT11
const int DHT_pin = D1;
DHT dht(DHT_pin, DHTTYPE);
WiFiClient client;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.print("\nConnecting to WiFi...with SSID: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
Serial.println("Connecting...");
while(WiFi.status() != WL_CONNECTED){
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connection successful!");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
//Read the temperature
float t = dht.readTemperature();
delay(10); //necessary for reading correct values
//Read the humidity
float h = dht.readHumidity();
if(client.connect(server, 80)){
String postStr = "Value1: " + String(t) + " Centi Value2: " + String(h) + " Percent";
//String postStr = "This is a tweet to check whether NodemCU can send a long line of text or not.";
postStr += "\r\n\r\n";
client.print("GET /apps/thingtweet/1/statuses/update?key=" + apiKey + "&status=" + postStr + " HTTP/1.1\r\n");
client.print("Host: api.thingspeak.com\r\n");
client.print("Accept: */*\r\n");
client.print("User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n");
client.print("\r\n");
Serial.println(postStr);
Serial.println("Data sent to Twitter");
}
client.stop();
}
void loop() {
}
Note: For some unknown reason, Twitter is rejecting any text containing the words “Temperature”, “Humidity”, and sub strings of these words. Try any other text.
Video Explanation
References
Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.
He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.
He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.
Leave a Reply