I wanted to be able to control a light (or anything really) using tweets. Below are the instructions/scripts I wrote to get it working. First comes the prerequisites:
- ESP32 (duh)
- A VPS, Pi, or really any computer acting as a flask server (it just needs internet access)
- A wifi network for the ESP32 to connect to, I just used the hotspot on my phone as a PoC
- Twitter API credentials (really easy to get, just fill out the forms)
Your ESP32 will query your flask server for a trigger word to enable the LED. The Flask server will query twitter for your latest top tweet, if it has a trigger word in it, relay that to the esp32 client. Boom, tweet causes LED to turn on.
The first step is to get your ESP32 setup running the micropython environment. I followed this excellent guide.
Once you get your ESP32 configured to run python code, go ahead and transfer the following script to act as the client. You just need to change the wifi details and target flask server:
Connect the LED to Pin 2 on the ESP32 and it's all set to go. Now onto the flask server...
On your VPS/Pi/whatever, install flask and tweepy and create a directory to hold your script files. Grab the Access Token, Access Secret, Consumer Secret, Consumer Key from your Twitter Dev console that you set up earlier and place them in a "twitter_creds.py" file like the following:
Then paste the following into "tweepy-top.py"
Now create your main flask app by pasting the following into "flaskhello.py":
There you can see 'light' is used as the trigger word. Using this setup, every 2 seconds the esp32 will make a request to your flask server, which causes the flask server to query twitter for the user's top tweet, if the top tweet contains the word "light" in it, it returns the string "yes". The ESP32 recognizes the "yes" and turns on pin 2.
This is a very simple PoC and gets the job done. You can take this and expand in a thousand directions with it, some ideas:
- A desktop counter that keeps track of your followers, retweets, likes, etc
- A LED scroller that outputs your latest mentions
- Or simply use twitter as the control for some device
The options are endless...enjoy :D