Startertutorials Blog
Tutorials and articles related to programming, computer science, technology and others.
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.

Categories: IoT Lab. No Comments on Tweeting Senor Data with NodeMCU EPS8266 and ThingSpeak
0
(0)

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.

 

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)

Tweeting Senor Data with NodeMCU EPS8266 and ThingSpeak


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


 

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

 

 

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Suryateja Pericherla

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

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