How to Post on Twitter using an ESP8266

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.

Creating an account on IFTTT is free

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:

turn applet on

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.

configure

Complete the Applet

Fill the Applet with your own information. Follow these instructions:

  1. Type “post_tweet” in your event name
  2. Edit the text that you want your ESP8266 to send out as a Tweet
  3. Press the “Save” button
configure-2

Now, go to this URL: https://ifttt.com/maker_webhooks and open the “Settings” tab.

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.

copy ifttt api key

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.

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.

Post on Twitter from an ESP8266

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.

ESP Bitcoin price bb

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:

  1. Click here to download ESPlorer. 
  2. Unzip that folder.
  3. Go to the main folder.
  4. Run ESPlorer.jar. It’s a Java program, so you need Java installed on your computer.
  5. Open the Explorer.
esplorer start - Post on Twitter from an ESP8266

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.

  1. Set the bad route as 9600.
  2. Select your FTDI programmer port (COM3, for example).
  3. Press Open/Close.
  4. Select the NodeMCU+MicroPtyhon tab.
  5. 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.

esplorer IDE - Post on Twitter from an ESP8266

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:

Post on Twitter from an ESP8266

If you like ESP8266, you may also like:

We hope you find this tutorial useful. Thanks for reading.

Oh hi there It’s nice to meet you.

Sign up to receive awesome content in your inbox, every month.

We don’t spam! Read our privacy policy for more info.

Leave a Reply

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

ESP8266 Home Automation Projects

Leverage the power of this tiny WiFi chip to build exciting smart home projects