IoT motion sensor light: Integrated W5500-EVB-Pico, Adafruit IO, PIR sensor and IFTTT

Smart lighting system using W5500-EVB-Pico, Adafruit IO and PIR sensors. Using circuit python. The system detects motion to control lighting and records events.

Forward: IoT Motion Sensing Lights: Integrating W5500-EVB-Pico, Adafruit IO, PIR Sensor, and IFTTT

Project Introduction

Introduction

Personally, I find it useful to use motion recognition LED sensors. When motion is detected, the LED turns on and off for a certain period of time.

The lighting button for my house is further away from the front door, so when I come in late at night, I have to be careful about entering a room with no lights. After purchasing and installing a sensor light, the sensor light turns on through motion recognition, allowing you to enter more safely.

I decided to implement a similar functionality using a Pico board and leave logs by linking additional services.

Accessories

  • W5500-EVB-Pico
  • M5Stack PIR sensor
  • Picobricks (LED): You can only use the necessary modules individually, but this time I used them as is.
  • Adafruit.io

If you’re using an expansion board like Picobricks, you can use Grove connectors to make connection easy. All you have to do is match the GPIO pin numbers. I connected like this:

Adafruit IO

In the example of wiznet5k support, there is a project called aio, I see there is an API supported by Adafruit, and it supports http (not SSL).

Additionally, IFTTT also supports Adafruit IO, so we decided to use it to link external services.

Get started

  • IO – Adafruit

You simply enter your information on the website to register, and registration is complete after verification via email.

Since the on-screen UI is different from the documentation, people are confused whether the manual was written a long time ago.

If you enter the My Account page of the IO page, there is a yellow key-shaped button.

When you press the button, a key is issued, ready for immediate use.

You can now upload sources using Pico, with a free subscription allowing you to create up to 10 sources.

The initial feed is empty.

Unfortunately, wiznet5k doesn’t support SSL yet, but Adafruit IO supports http, so you can transfer data easily.

First, I tested the API operation using Curl as an example.

I ran it from Windows CMD but the request failed, so I ran it from Linux Shell (WSL) and it worked fine.

curl -X POST -H "Content-Type: application/json" -H "X-AIO-Key: <key>" --data '{"feed": {" name": "New Feed"}}' http://io.adafruit.com/api/v2/<username>/feeds

Now write the Circuitpython code.

In addition to the main code code.py, private information is also written separately in the secrets.py file.

secrets = {
    'aio_username' : "your-aio-username",
    'aio_key' : 'your-aio-key',
}

The published feed looks like this:

Next, let’s connect IFTTT.

IFTTT

IFTTT (If This Then That) is a web-based platform that allows users to connect various applications and services to create automated tasks.

Here we will use Adafruit IO as a trigger and connect a service to receive alerts as actions.

Trigger (If): Monitor the feed on Adafruit IO

Adafruit – Monitor a feed on Adafruit IO

Adafruit services support the following triggers.

When you click the “Connect” button, a permission grant popup will appear.

For reference, I tried it with an existing Google account but it kept redirecting to the existing APPLET and the permissions were not set, so I created a new account and continued. Works fine on new account.

Created a separate function to create the feed.

def create_feed(name):
    data = {
        "feed": {"name": name}
    }
    endpoint = f'http://io.adafruit.com/api/v2/{secrets["aio_username"]}/feeds'
    headers = {"X-AIO-KEY": secrets["aio_key"]}
    response = requests.post(endpoint, json=data, headers=headers)
    print(response.json())
    response.close()

The feed is created as described above.

When you enter the feed you create, the data appears in a form that can be accumulated in the feed, as shown below.

I send the data here and see if IFTTT is triggered.

For reference, if you use Postman to make the request, you can see the data in the following format.

Create Data API

When sending data in the same feed, you can use the Create Data API.

You can see the data being received into the feed in the form of a time series as shown below.

The page is updated in real time without refreshing the page, making monitoring convenient.

The following updated functionality has been added to the main code. The sensor value will be updated through this function.

def create_data(feed_name, data):
    endpoint = f'http://io.adafruit.com/api/v2/{secrets["aio_username"]}/feeds/{feed_name}/data'
    headers = {"X-AIO-KEY": secrets["aio_key"]}
    response = requests.post(endpoint, json=data, headers=headers)
    print(response.json())
    response.close()

If the execution and request are successful, you can receive the following response.

Action (then)

Where can the data sent by the trigger be sent?

There are many options.

  • Twitter
  • Telegram
  • Discord
  • SMS
  • IFTTT notifications

I’m trying to link a relatively simple project called Notifications and Slack that I’m currently using.

IFTTT notifications

IFTTT offers a smartphone app where you can receive push notifications.

As mentioned above, the setup is complete.

Receive alerts as follows.

Slack

I also tried connecting to the Slack service and posting a message.

To send messages to Slack, you must grant IFTTT permissions. (first)

After setting it up and testing it, the alarm works fine.

PIR Sensors and LEDs

I use a PIR sensor to identify motion and since there is an M5Stack unit module I use it by connecting it to the Picobricks using a GROVE cable. Easier to connect than jumper cables.

The LED used is a WS2812 from Picobricks.

Next, I wrote and tested the motion recognition and LED control code separately, and then merged the codes.

Motion Sensor Code

import board
import digitalio

PIR_PIN = board.GP17 # Pin number connected to PIR sensor output wire.

# Setup digital input for PIR sensor:
pir = digitalio.DigitalInOut(PIR_PIN)
pir.direction = digitalio.Direction.INPUT
print('Check')

# Main loop that will run forever:
old_value = pir.value
while True:
    pir_value = pir.value
    if pir_value:
        if not old_value:
            print('Motion detected!')
    else:
        if old_value:
            print('Motion ended!')
    old_value = pir_value

LED control code

import board
import neopixel
import time

pixel = neopixel.NeoPixel(board.GP6, 1)
pixel.brightness = 0.1

pixel.fill((0, 255, 0))
time.sleep(3)
pixel.fill((0, 0, 0))

Code

The entire code, including each base code, can be referenced at the Github link below.

  • https://github.com/renakim/W5x00-EVB-Pico-Circuitpython/tree/main/motionLedAdafruitIO

Question

There is a problem with swarming alerts.

I was wondering if the reason was that both applets were using the same trigger, so I disconnected one of them and tested it, but the delay was the same. I need to research this further.

Reference

Circuitpython code samples and libraries

https://github.com/adafruit/Adafruit_CircuitPython_Bundle

Documents

  • Main code

    Includes every base code