Writing Your Lua Script
Don’t forget that first you need to flash your ESP with NodeMCU firmare. Copy and paste the code below into ESPlorer. Then edit line 5 with your network credentials and line 13 with your API KEY.
-- Rui Santos -- Complete project details at http://randomnerdtutorials.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 the ESPlorer you should see a window similar to the preceding Figure, follow these instructions to send commands to your ESP8266: Connect your FTDI programmer to your computer
- Set bad raute as 9600
- Select your FTDI programmer port (COM3, for example)
- Press Open/Close
- Select NodeMCU+MicroPtyhon tab
- Copy the your Lua script into ESPlorer
Then you simply click the button Send to ESP. And you should see a a print saying “Posted Tweet” in the ESPlorer window. Everything that you need to worry about or change is highlighted in red box in the following Figure.
Limitations and Taking it Further
Twitter doesn’t allow you to post the same tweet over and over again. So you can use the following script (by changing line 5 with your network credentials and line 13 with your API KEY) to add parameters to your POST request.
In my example below I’m using the parameter “value1” and sending the value “True”. IFTTT supports three parameters called “value1”, “value2” and “value3” to customize your Tweets and make them unique.
-- Rui Santos -- Complete project details at http://randomnerdtutorials.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')