How to Connect Your Raspberry Pi Pico W to Twitter via IFTTT
Connecting our projects to the internet has become much easier and cheaper! $ 6 Raspberry Pi Pico WThe upgrade to the original Raspberry Pi Pico has brought us one great update called Wi-Fi. With this upgrade and a $ 6 price tag, manufacturers will be able to harness the power of the RP2040 SoC for many Internet of Things applications.Pico W is also at the top of our list The best RP2040 board.
We have already explained how to exchange data with and from. From Raspberry Pi Pico W to another web service Anvil. But what if you just want to collect the data and send it to the world? There are many ways to do this, but the easiest one should be IFTTT.
IFTTT (If This Then That) is a web service that allows you to retrieve data from various inputs (Twitter, home automation, Google, Facebook, etc.) and send it to other services.
This project uses the most basic service, webhook, to send an HTTP POST request from Raspberry PiPicoW to the IFTTT service. IFTTT creates an applet that intercepts webhooks, triggers tweets and sends them to Twitter.
For this project you will need:
Temperature sensor circuit
Our circuit is simple and consists only of a Raspberry Pi Pico W and a temperature sensor. The DHT11 is a low-cost 4-pin temperature sensor that is widely used in the manufacturer community. Seen from the front (blue “cage”), connect to the Raspberry Pi Pico W as follows:
Pin 1: (Red line) Connect VCC to 3V3 (OUT) of Pico W.
Pin 2: (Yellow wire) Connect the data to GP4 of PicoW.
Pin 3: No connection.
Pin 4: (Black line) Connect GND to GND of Pico W.
IFTTT settings
IFTTT (If This, Then That) is a conduit that links Raspberry Pi Pico W to Twitter. IFTTT is a free service that provides a myriad of ways to connect different data services to each other.
Create an applet that responds to a webhook (custom URL) that sends data to IFTTT. IFTTT then adds the data to the message and sends it to your followers on Twitter.
1.1. In the browser Access IFTTT Create a login / account.
2.2. [作成]Click.
3.3. [追加]Click. This is the trigger event. When this happens, the applet will launch. In our case, if the webhook was sent from Raspberry Pi Pico W.
4.4. Search for the webhook and click the webhook icon.
5.5. [Webリクエストの受信]Click. This is a request sent by Raspberry Pi Pico W.
6.6. Set the event name to Post_Tweet and[トリガーの作成]Click.
7.[ThenThatAdd]Click. This is the result of the trigger. In this case, the tweet will be sent.
8.8. Search for Twitter and click the Twitter icon.
9.9. [ツイートを投稿]Choose.
10.10. Make sure the correct Twitter account is selected. You may need to link your Twitter account to IFTTT. Replace the tweet text with your own custom message. Linking your Twitter account to IFTTT is an automated process that IFTTT triggers and asks for confirmation.
11.11. [材料の追加]Click to[値1]Choose. Then finish the rest of the message. In this case, state that the temperature is in degrees Celsius.[アクションの作成]Click to save. Ingredients are IFTTT’s way of adding additional information to a tweet by retrieving data from a custom URL.
12.12. [続行]Click.
13. Check the applet details, check the notification option on and when you’re ready[完了]Click. Notifications allow you to debug your applet in the event of a problem.
The IFTTT applet has been created, but there is no webhook URL yet. This URL sends temperature data from the Raspberry Pi Pico W to the IFTTT service. The URL requires a trigger (Post_Tweet) and an API key to unlock IFTTT access.
1.1.visit This link In a new browser tab / window.
2.2. [POSTまたはGETWebリクエストの作成]Copy the link to Notepad or a text editor. For security reasons, I edited the text so that the text shows the correct API key.
https://maker.ifttt.com/trigger/{event}/json/with/key/YOUR API KEY HERE
3.3. Remove / json and change {event} to Post_Tweet.
https://maker.ifttt.com/trigger/Post_Tweet/with/key/YOUR API KEY HERE
You now have a valid webhook URL that links your Raspberry Pi Pico W to Twitter via IFTTT.
Raspberry Pi Pico W setup
The Raspberry Pi Pico W acts as a data acquisition device. Connect to Wi-Fi and read the temperature every hour. This is then sent to IFTTT using a custom webhook URL. IFTTT then sends a message to Twitter according to the applet you created.
Our goal is to install the latest MicroPython firmware release for the Raspberry Pi Pico W, write a few lines of MicroPython code to connect to Wi-Fi, read the temperature and send that data to IFTTT.
1.1. follow This guide Download and install the MicroPython firmware and set up Thonny.
2.2. Open Thonny and create a new file.
3.3. Import the three modules of code. The network allows PicoW to connect to a Wi-Fi network. Urequests is a version of the request for MicroPython, a way to manipulate web data. DHT refers to the DHT11 temperature sensor used in the project.
import network
import urequests
import dht
4.4. Import two more code modules. Import the sleep function from the Time module. Use this to add a pause to your project. From the machine module’s import pin, this allows your code to interact with GPIOs.
from time import sleep
from machine import Pin
5.5. Create an object “sensor” that connects the cord to the DHT11 connected via GPIO. You can use this object to query the temperature data by interacting with the sensor connected to pin 2.
sensor = dht.DHT11(Pin(2))
6.6. Create an object “wlan” and use it to connect your code to the Raspberry Pi Pico W’s Wi-Fi and turn it on.
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
7. Connect PicoW to your Wi-Fi access point using your SSID and password.
wlan.connect("SSID","PASSWORD")
8.8. Pause for 5 seconds before proceeding. This is not strictly necessary as previous Wi-Fi connections are blocking calls that release blocks after connecting or failing. This pause is there to give you some leeway.
sleep(5)
9.9. Prints the current status of your Wi-Fi connection. This will be True if the connection was successful and False if the connection was unsuccessful.
print(wlan.isconnected())
10.10. Add a loop to continuously execute the following code. The while True loop runs the code continuously until PicoW is turned off. Note that the code following this line is indented to indicate that it is part of the loop.
while True:
11.11. Read on DHT11.. You need to do this to get the raw data.
sensor.measure()
12.12. Saves the current temperature to the object’s temperature.
temperature = sensor.temperature()
13.13. Output the temperature to PythonShell (REPL). This will help you debug the problem. The temperature is displayed each time the loop is repeated.
print(temperature)
14.14. Create an object, a message, that stores the unique Webhook URL you created earlier. At the end of the URL? Add value1 = and make sure it is included between the quotation marks “”... Don’t forget to add your own IFTTT API key.
message = "https://maker.ifttt.com/trigger/Post_Tweet/with/key/YOUR IFTTT API KEY HERE?value1="
15. 15. On the same line, add the current temperature stored in the Temperature object. Note that you need to use the str () function to convert the temperature from integer / floating point to a string. The message line looks like this:
message = "https://maker.ifttt.com/trigger/Post_Tweet/with/key/YOUR IFTTT API KEY HERE?value1="+str(temperature)
16.16. Post a message on Twitter using urequests.post. It basically sends the data to IFTTT via the webhook URL, which IFTTT sends to Twitter.
urequests.post(message)
17.17. Pause the code for 1 hour (3600 seconds) before repeating the loop. IFTTT has a rate limit of 25 tweets per day, so you should limit your tweets to one per hour to feed your data consistently. If this limit is exceeded, IFTTT will disable the applet until the next day.
sleep(3600)
18.18. Save the code as tweet_temp.py in RaspberryPiPicoW
Complete code list
import network
import urequests
import dht
from time import sleep
from machine import Pin
sensor = dht.DHT11(Pin(2))
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect("SSID","PASSWORD")
sleep(5)
print(wlan.isconnected())
while True:
sensor.measure()
temperature = sensor.temperature()
print(temperature)
message = "https://maker.ifttt.com/trigger/Post_Tweet/with/key/YOUR IFTTT API KEY HERE?value1="+str(temperature)
urequests.post(message)
sleep(3600)
Code testing and execution
You should test your code to work before you can deploy your project properly.
1. In the upper left corner of Thonny[実行]Click the button.
2.2. Check the output. True is displayed and you can confirm that you are connected to Wi-Fi. Then the temperature is displayed.
3.3. Please visit your Twitter account and make sure the message is posted. It takes about 10 to 20 seconds to be displayed.
4.4. [停止]Click to stop the running code.
Now that the code has been successfully tested, you can set the code to run automatically when you start the Raspberry Pi Pico W.
1.1. Save it as main.py in RaspberryPiPicoW as code. MicroPython (any device) looks for main.py when it is powered on. If it exists, the code in this file will be executed automatically.
2.2. Unplug the micro USB lead from PicoW, reinsert it and force a reboot.
3.3. Check the corresponding tweet in your Twitter account.