Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4616

Automation, sensing and robotics • Micro-Python security system with Pico W and the cloud

$
0
0
Set up:

- I am using a Pico W GPIO input to monitor door magnet sensors and room motion sensors. (Actually I have 8 security inputs, but I am simplifying the issue by only showing 1 sensor for this topic.)

- I Send Data to the cloud every 30 seconds (Using Mathworks/ThingSpeak) (I can send data quicker, but the limitation is on the receiving end.)

Goal:
- This is an experiment to see if this can be a poor person's security system, since ThingSpeak can be programmed to send an email to my Gmail if a "field" changes state.

Status so Far:
- It works fine, except that the program misses quick door openings and quick motion sensing.

Details:
- My ThingSpeak account is a free one, limiting, by only accepting my data (for up to eight fields) once every 30 seconds. (A paid account reduces that to seconds.)

- Within the 30 second window, (or even the paid account shorter window), a quick door opening and closing can be missed since the data sent is that status at the very moment of data transmission.

Question:
- I can either accept the possibility of a door opening or motion sensing being missed, or I can try to edit my micro-python program to latch an alarm for 45 seconds (less for a paid account) to be able to send the alarm with surety.

Expectations Here:
- I have enough experience to try to solve this problem myself, and if I do, I will post results here. But, I also, shamelessly, will accept help with my homework, so to speak.

Code is here, edited for only one sensor for brevity: (except logins are secreted out.) (the email portion of the issue is internal in my ThingSpeak account and does not enter into the micro-python program.)

The actual detail for you to see on my ThingSpeak display, only for my kitchen sensor, is in this link https://thingspeak.com/channels/2392138.

(Of course, I will lock the link to "private" only in the future.

Code:

import machineimport urequests from machine import Pin,Timerimport network, timeimport utimeimport mathd7 = Pin(26, Pin.IN, Pin.PULL_UP)#kitchen_pir_58#######led = Pin("LED", Pin.OUT)  #pico w led flashertim = Timer()HTTP_HEADERS = {'Content-Type': 'application/json'} THINGSPEAK_WRITE_API_KEY = 'KDYsecretPCCOXR'   ssid = 'bsecret27'password = 'pgsecret56'# Configure Pico W as Stationsta_if=network.WLAN(network.STA_IF)sta_if.active(True) for _ in range(10):        print('connecting to network...')         sta_if.connect(ssid, password)        time.sleep(1)        if sta_if.isconnected():            print('Connected.')            break        time.sleep(11) print('network config:', sta_if.ifconfig()) def tick(timer):    global led    led.toggle()tim.init(freq=1, mode=Timer.PERIODIC, callback=tick)while True:    print("Getting data to send")    time.sleep(5)         D7 = d7.value()        print(" ")    print("kitchen_pir_status:           ", D7)    print(" ")        readings = {'field7':D7}       for retries in range(60):     # 60 second reboot timeout        if sta_if.isconnected():            print("Connected, sending")            try:                request = urequests.post( 'http://api.thingspeak.com/update?api_key=' + THINGSPEAK_WRITE_API_KEY, json = readings, headers = HTTP_HEADERS )                  request.close()                time.sleep(20)                print("Write Data to ThingSpeak ",readings)                print(" Successful  ")                break            except:                print("Send failed")                time.sleep(1)         else:                print(" waiting for wifi to come back.....")                time.sleep(1)    else:        print("Rebooting")        time.sleep(1)        machine.reset()   print("Sent, waiting awhile")time.sleep(10) 

Statistics: Posted by shore — Thu May 09, 2024 1:57 pm — Replies 0 — Views 14



Viewing all articles
Browse latest Browse all 4616

Trending Articles