How to Post on Twitter from Arduino and ESP8266 IoT Project

Twitter is a free social platform that can be very useful for your IoT project. You can use it instead of paying for a messaging platform. In this project, I will teach you how you can use Twitter to send temperature alerts from your internet-enabled Arduino project. The project utilizes ESP8266 WiFi module to connect Arduino to the internet. The wifi module hooks onto the home wifi for internet. You can also use the ESP8266 alone but in this simple project, we shall use it together with Arduino Uno. For integration with Twitter, the project uses Thingspeak’s ThingTweet App.

ThingTweet is a twitter integration app built by ThingSpeak that allows your IoT project to send tweet messages. You can use the App to send alerts via Twitter. For example, you can send an alert when the temperature in your room reaches a certain value. In this post, you will learn how to integrate your Twitter account with ThingSpeak through the ThingTweet app and post your first tweet alert when the temperature reading on TPM36 sensor exceeds 29 degrees Celcius

Project requirements

Arduino

ESP8266

10k resistor

Temperature sensor FTDI chip/ CP2102/Voltage divider

Assumptions: I am assuming you have connected your ESP8266 Wi-Fi Module and that your WiFi has internet connectivity.

Building the Circuit for the Simple IoT Project

The circuit for this project is very simple. Using the following schematic diagram to build your circuit.

Schematic Diagram

how to tweet with arduino using thingspeak
Schematic diagram for Arduino + ESP8266 + Thingspeak Twitter project

How to Link Twitter with ThingTweet in ThingSpeak

select thingtweet api
Log in to ThingSpeak and Select ThingTweet option under Apps.
link twitter to thingspeak
Link ThingTweet App by clicking the ‘Link Twitter Account’ Button
authorize thingtweet to access twitter account
Authorize ThingTweet to Access to the Twitter Account you wish to integrate
ThingTweet authorization complete
You will be notified that linking to ThingTweet has been succesful. Click ‘Back to ThingTweet to return to ThingSpeak platform
copy api key
Now, copy the API key that has been generated for the Twitter Account you have just linked and paste it to the Arduino code

How the Project Works

How TMP36 Temperature Sensor Works

To measure temperature, I am using the TMP36 analog temperature sensor. This is a small solid-state, transistor-like chip that measures the ambient temperature. It uses the principle of proportionate increase of voltage across a diode with an increase in temperature. This voltage change is then amplified to produce a measurable analog voltage output. The ADC of Arduino microcontroller detects this output signal and converts it to a 10-bit value.

The 10-bit output value is then multiplied with the ADC resolution to generate equivalent voltage with respect to a 5V reference. The formula for this calculation is Vout = (ADC output) x (Input voltage)/(1024). This voltage output is then converted to degree Celcius using the formula: Temp in degree Celcius = ((Vout in mV) – (500mV offset voltage ))/10. The 0.5V offset voltage is inherent in the output of the TMP36. For example, assuming the ADC output is 500, the Vout = (500 x 5V)/1024 = 2.441V This output voltage can be converted to degrees as follows: (2441 – 500)/10 = 194.1 degrees.

Arduino Sketch

How the Arduino Code Works

The code has two functional units. The first one is for reading the temperature as measured by the TMP36. The code works as described earlier in this post. The analog reading from the TMP36 temperature sensor is stored in variable reading. This reading already converted to a 10-bit value by the ADC of Arduino. The value is then mapped to a 5V input by multiplying with 5V and dividing by 1024 (the number of possible values). This value is then converted to equivalent degrees Celcius and stored in a float variable temperatureC.

int reading = analogRead(sensorPin);
float voltage = reading * 5.0;
voltage /= 1024.0;
//Serial.print(voltage);
//Serial.println(" volts");
float temperatureC = (voltage - 0.5) * 100 ;
Serial.print(temperatureC);
Serial.println(" degrees C");

The second section involves the ESP8266. When temperature exceeds 29 degrees, the ESP8266 initializes and sends an alert to twitter via ThingTweet API of ThingSpeak. The code starts by creating a HTTP POST structure using the function postRequest(). This functions returns the POST structure in form of a string and stores it in variable payload. The ESP8266 then creates a TCP connection to the Thingspeak server using the function tcp_connect(). For TCP connection to be established, you need to pass server IP address and port number. Once the tcp connection has been established,the alert is sent using the function postStatus().

Video: How to Post on Twitter from Arduino ESP8266 Project

In this video, you will learn how to send temperature alert messages on twitter from an Arduino and ESP8266 IoT Project using Thingspeak’s ThingTweet App