Wiznet makers

viktor

Published July 18, 2023 ©

81 UCC

14 WCC

33 VAR

0 Contests

0 Followers

0 Following

How to Connect Raspberry Pi Pico to Twitter using Ethernet HAT and IFTTT

This projects aims to guide how to connect Rpi Pico to Twitter with help of WIZnet Ethernet HAT, IFTTT and Micropython

COMPONENTS Hardware components

Raspberry Pi - Raspberry Pi Pico

x 1


WIZnet - WIZnet Ethernet HAT

x 1

Software Apps and online services

micropython - MicroPython

x 1


ifttt - Maker service

x 1


PROJECT DESCRIPTION

1. Introduction

To continue my micropython journey started in previous project, this time I decided to try connecting Raspberry Pi Pico and Twitter using WIZnet Ethernet HAT and Micropython.

As WIZnet Ethernet HAT is designed to be pin-compatible with Raspberry Pi Pico, connecting these boards is simple. For more details about the board, refer to docs page and youtube video

Pico PinDescription
GPIO16Connected to MISO on W5100S
GPIO17Connected to CSn on W5100S
GPIO18Connected to SCLK on W5100S
GPIO19Connected to MOSI on W5100S
GPIO20Connected to RSTn on W5100S
GPIO21Connected to INTn on W5100S

Please note that this project can be replicated using W5100S-EVB-Pico or W5500-EVB-Pico boards. In this case you can skip 3rd step with "Customizing Micropython firmware".

2. Setup IFTTT

IFTTT, or If This, Then That, acts as the bridge connecting Raspberry Pi Pico to Twitter. It's a free service that facilitates the integration of various data services. I'll create an applet that responds to a webhook, sending data to IFTTT. Subsequently, IFTTT will include this data in a message sent to Twitter.

(a) Visit IFTTT, create account and login

(b) Click "Create" button

(c) Click on "Add" button to create a trigger event.

(d) Search for "webhook" and click on the tile

(e) Select "Receive a web request"

(f) Create an Event name and press "Create trigger" button

(g) "If" is now configured, now let's set "Then That". Click "Add"

(h) Type "Twitter" in the search bar and click on the icon

(i) Select "Post a tweet"

(j) Type in your Twitter account (make sure it is correct). Customize your tweet text, press "Add ingredient" to add variable

(k) Click "Continue" button

(l) Review the applet, toggle the button to opt in for notifications and click "Finish" button

(m) Visit webhook page and click "Documentation" button

(n) Copy the post link.

https://maker.ifttt.com/trigger/{event}/json/with/key/YOUR API KEY HERE

Remove/json, change {event} to your trigger name (in my case "post-tweet") and insert your API key.

https://maker.ifttt.com/trigger/post-tweet/with/key/YOUR API KEY HERE

3. Custom Micropython firmware

As I am using Raspberry Pi Pico, the basic Micropython firmware doesn't have WIZNET5K library. Therefore I need to build a firmware that would contain this library.

Disclaimer: It is my second time using Micropython, there might be better and easier way to add network library to Pico. Below changes are effective as of July 2023.

(a) Clone micropython repository

(b) Navigate to "ports/rp2/boards/PICO" folder and make following changes

(c) In mpconfigboard.h add

// Board and hardware specific configuration
#define MICROPY_HW_BOARD_NAME                   "Raspberry Pi Pico"
#define MICROPY_HW_FLASH_STORAGE_BYTES          (1408 * 1024)

#define MICROPY_PY_NETWORK                      (1)

(d) In mpconfigboard.cmake add

# cmake file for Raspberry Pi Pico

set(MICROPY_PY_NETWORK_WIZNET5K W5100S)
set(MICROPY_PY_LWIP 1)
set(MICROPY_FROZEN_MANIFEST ${MICROPY_BOARD_DIR}/manifest.py)

(e) Create new manifest.py file and copy following

include("$(PORT_DIR)/boards/manifest.py")

require("bundle-networking")

(f) Build and flash firmware to the Pico board

4. The code

(a) Import modules and libraries

from usocket import socket
from machine import Pin,SPI
import network
import time
import urequests

(b) Initialize WIZnet chip. In my case I used static IP, make sure to update according to your environment.

#W5x00 chip init
def w5x00_init():
    spi=SPI(0,2_000_000, mosi=Pin(19),miso=Pin(16),sck=Pin(18))
    nic = network.WIZNET5K(spi,Pin(17),Pin(20)) #spi,cs,reset pin
    nic.active(True)
    nic.ifconfig(('192.168.11.30','255.255.255.0','192.168.11.1','8.8.8.8'))
    while not nic.isconnected():
        time.sleep(1)
        print(nic.regs())
    print(nic.ifconfig())

(c) Post the message to Twitter using the urequests.post

    message = "https://maker.ifttt.com/trigger/post-tweet/with/key/YOUR_API_KEY_HERE?value1=Hello%20World"
    urequests.post(message)

The complete code:

from usocket import socket
from machine import Pin,SPI
import network
import time
import urequests

#W5x00 chip init
def w5x00_init():
    spi=SPI(0,2_000_000, mosi=Pin(19),miso=Pin(16),sck=Pin(18))
    nic = network.WIZNET5K(spi,Pin(17),Pin(20)) #spi,cs,reset pin
    nic.active(True)
    nic.ifconfig(('192.168.11.30','255.255.255.0','192.168.11.1','8.8.8.8'))
    while not nic.isconnected():
        time.sleep(1)
        print(nic.regs())
    print(nic.ifconfig())
        
def main():
    w5x00_init()

    message = "https://maker.ifttt.com/trigger/post-tweet/with/key/YOUR_API_KEY_HERE?value1=Hello%20World"
    urequests.post(message)
    
if __name__ == "__main__":
    main()

Note: IFTTT has a rate limit of 25 tweets per day, so make sure to add sleep interval when posting in a loop

5. Result

After running the code, you will get an email from IFTTT (if opted for notifications) and see the corresponding post in your Twitter account.

6. Next plan

I might use this to automate my SNS posting. Stay tuned for next project :)

Documents
  • post-tweet.py

  • Pico Custom firmware

Comments Write