In this project, you will post to Twitter using an ESP8266. The purpose of this project is to show you the limitless possibilities that this $4 WiFi module offers when integrated with the free platform I'm going to show you.
To do this task, sign up for a free service called “IFTTT“, which stands for “If This, Then That“.
IFTTT is a platform that allows you to have creative control over a variety of products and apps.
Apps may be made to work in tandem. When you make a request to IFTTT, for example, it triggers a recipe that sends you an email alert.
Please finish the following tutorials before proceeding to read this project:
Creating Your IFTTT Account
Creating an account on IFTTT is free!
Go to the official site: https://ifttt.com/, and click the “Sign Up” button at the top of the page.
Complete the form with your personal information (see figure below) and create your account.
After creating your account, follow their getting started tutorial.
Open the Applet
You may also use an Applet that I made that integrates perfectly with this project.
If you're logged in to IFTTT and open the URL below, you'll be able to use my Applet right away:
Press the “Turn on” button:
You then need to allow Maker Webhooks and Twitter access permissions. Allow IFTTT to connect to your Twitter account. When you finish connecting to Twitter, a new page loads.
Complete the Applet
Fill the Applet with your own information. Follow these instructions:
- Type “post_tweet” in your event name
- Edit the text that you want your ESP8266 to send out as a Tweet
- Press the “Save” button
Now, go to this URL: https://ifttt.com/maker_webhooks and open the “Settings” tab.
Keep a copy of your secret keys in a safe place (you'll need them later in this project). My secret key, for example, is b6eDdHYblEv2Sy32qLwe.
Test Your Applet
Let’s test if your request is working properly. Replace YOUR_API_KEY with the following URL:
https://maker.ifttt.com/trigger/post_tweet/with/key/YOUR_API_KEY
With your API key:
https://maker.ifttt.com/trigger/post_tweet/with/key/b6eDdHYblEv2Sy32qLwe
Open your URL with your API key in your browser.
You should see something similar to the preceding figure. Then go to Twitter, and there should be a new tweet there!
If you like Twitter, you may follow me there at @lededitpro for electronics news.
Parts List
You will need the following items to proceed with this project:
You can use the preceding links to find all the parts for your projects at the best price!
Schematics (3.3V FTDI Programmer)
The schematics for this project are quite simple. To upload code, you only need to establish serial connectivity between your FTDI programmer and your ESP8266.
Downloading ESPlorer
To create and save Lua files to your ESP8266, I recommend using the ESPlorer program created by 4refr0nt.
To download and install ESPlorer, follow these instructions:
- Click here to download ESPlorer.
- Unzip that folder.
- Go to the main folder.
- Run ESPlorer.jar. It’s a Java program, so you need Java installed on your computer.
- Open the Explorer.
Writing Your Lua Script
Don't forget that you need to flash the NodeMCU firmware on your ESP first. Copy and paste the following code into ESPlorer. Then, on line 5, provide your network credentials, and on line 13, enter your API key.
-- LEDEdit PRO
-- Complete project details at https://lededitpro.com
wifi.setmode(wifi.STATION)
wifi.sta.config("YOUR_NETWORK_NAME","YOUR_NETWORK_PASSWORD")
-- A simple http client
conn = nil
conn=net.createConnection(net.TCP, 0)
conn:on("receive", function(conn, payload) end)
conn:connect(80,"maker.ifttt.com")
conn:on("connection", function(conn, payload)
conn:send("POST /trigger/post_tweet/with/key/YOUR_API_KEY HTTP/1.1\r\nHost: maker.ifttt.com\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n") end)
conn:close()
print('Posted Tweet')
Uploading Your Lua Script
When you open ESPlorer, you should see a window similar to the one shown in the preceding figure. To transmit commands to your ESP8266, use the following instructions: Connect the FTDI programmer to the PC.
- Set the bad route as 9600.
- Select your FTDI programmer port (COM3, for example).
- Press Open/Close.
- Select the NodeMCU+MicroPtyhon tab.
- Copy your Lua script into ESPlorer.
Then you simply click the “Send to ESP” button. And in the ESPlorer window, you should see a printout saying “Posted Tweet“. The following figure highlights in red anything that you need to worry about or modify.
Limitations and Taking it Further
Twitter does not enable you to repost the same tweet multiple times. To add parameters to your POST request, use the following script (change line 5 with your network credentials and line 13 with your API key).
I'm using the parameter “value1” and sending the value “True” in my example below. IFTTT supports three parameters called “value1“, “value2“, and “value3” to customize and personalize your tweets.
-- LEDEdit PRO
-- Complete project details at https://lededitpro.com
wifi.setmode(wifi.STATION)
wifi.sta.config("YOUR_NETWORK_NAME","YOUR_NETWORK_PASSWORD")
-- A simple http client
conn = nil
conn=net.createConnection(net.TCP, 0)
conn:on("receive", function(conn, payload) end)
conn:connect(80,"maker.ifttt.com")
conn:on("connection", function(conn, payload)
conn:send("POST /trigger/post_tweet/with/key/YOUR_API_KEY HTTP/1.1\r\n"..
"Host: maker.ifttt.com\r\nConnection: close\r\nAccept: */*\r\nContent-Type: application/json\r\n" ..
"Content-Length: 17\r\n\r\n{\"value1\":true}\r\n") end)
conn:close()
print('Posted Tweet')
Make sure the right “Content-Length” is set in your POST request. It's “17” in my example, but if you update “value1” or add additional parameters to your body request, the “Content-Length” changes as well. If you request the wrong content length, it will fail.
Using the ESP, you can post data from your sensors (like temperature, humidity, and so on) directly to Twitter.
Here’s the end result:
If you like ESP8266, you may also like:
- Install ESP8266 Board in Arduino IDE in less than 1 minute
- ESP8266 Pinout Guide: How should I use the GPIO pins?
- Send Email from ESP8266 NodeMCU via SMTP Server
- How to Send WhatsApp Messages From ESP8266 NodeMCU
We hope you find this tutorial useful. Thanks for reading.