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

MicroPython • Re: Micro-Python security system with Pico W and the cloud

$
0
0
Thanks again to all, and hello to Hippy.
I am exposing my poor skills at python...... surely I can clean this up, but here is what got from your advice.....

I decided to use the 15 second cloud acceptance limitation and wrap it around the "NEXT" transition to capture the transition definitely, even at the expense of losing another field's event happening during that acceptance delay window. I then decided to write to the cloud immediately upon the first transition in queue. Then look for the next transition. If no transition occurred, then no writing to the cloud occurred. I feel I am saving electronic "ink" because now, my program may go days without writing anything if no transitions occur, say, when we are gone. And there are two doors that are alarmed but seldom used, so no cloud reporting will occur unless those-doors are transitioned. (Note: My program now captures any transition, motion sensor or door contact, that is 0.8 seconds or more in opening. ) I have no bounce issues, so I did not need to address bounce.

Here are fields 7,4,3 and 2 which are the four passive infrared sensors for motion.

Image Image Image Image

Here is the 15 or 16 second time stamps from my cloud transmissions. Note the interesting feature that each contact transition is exclusive to the logged time slot. The time stamps correspond to the above graphs.

Code:

    Time Field 7    4    3    2  21:18:1621:18:32121:18:48021:19:53121:20:10021:20:47121:21:04021:21:33121:21:51021:22:08121:22:26021:22:5021:23:0621:23:25121:23:41021:24:09121:24:25021:24:50121:25:07021:27:40121:27:57021:28:12121:28:28021:28:45121:29:01021:29:18121:29:34021:29:49121:30:06021:30:22121:30:39021:30:58121:31:13121:31:30021:31:46121:32:02021:32:18021:33:23121:33:40121:33:55021:34:12121:34:28021:34:45021:35:02121:35:17121:35:34021:35:49021:36:06121:36:22021:36:37121:36:54021:37:20121:37:37121:37:53021:38:09021:39:40121:39:56021:40:33121:40:49021:42:06121:42:22021:43:13121:43:42021:44:16121:44:32021:44:48121:45:05021:45:27121:45:44021:47:04121:47:27021:47:45121:48:02021:48:24121:48:53021:49:52121:50:12021:50:42121:51:060    
Here is my very verbose Raspberry Pi Pico W micro-python code. It can be cleaned up, but it is a good step for me to do it this way.

Please criticise as you see fit. Thanks again. (ps- I am using the Pico W more and more on many projects!)

Code:

#        Channel ID: 2392138        Author: ShoreNice#  thanks to Hippy for auto-internet connection program lines     import machineimport urequests from machine import Pin,Timerimport network, timeimport utimeimport mathd1 = Pin(14, Pin.IN, Pin.PULL_UP) # switchd2 = Pin(16, Pin.IN, Pin.PULL_UP)#front_stair_pir_46d3 =Pin(17, Pin.IN, Pin.PULL_UP)#laundry_pir_551d4 =Pin(18, Pin.IN, Pin.PULL_UP)#den_pir_58_& 47d5 =Pin(28, Pin.IN, Pin.PULL_UP)#laundry_2_doors_48 &_53d6 = Pin(27, Pin.IN, Pin.PULL_UP)#sunroom_door_57d7 = Pin(26, Pin.IN, Pin.PULL_UP)#kitchen_pir_58d8 =Pin(22, Pin.IN, Pin.PULL_UP)#small_garage_door_50saved_D1 = 0saved_D2 = 0saved_D3 = 0saved_D4 = 0saved_D5 = 0saved_D6 = 0saved_D7 = 0saved_D8 = 0#######led = Pin("LED", Pin.OUT)  #pico w led flashertim = Timer()HTTP_HEADERS = {'Content-Type': 'application/json'} THINGSPEAK_WRITE_API_KEY = 'H845secretP897J4'   ssid = 'blsecret27'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("Checking for digital input change of state")   time.sleep(1)    ################################   D1 = d1.value()      # Typical of all fields:  # If this D1 value does not match our saved_D1, then  # a transition has occurred and we should save the D1  # This works both for open-to-close transitions and close-to-open transitions     if ( D1 != saved_D1):        saved_D1 = D1        print("Switch Status 1 = Arm System: ", D1)        readings = {'field1':D1}        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()                                        print("Write Data to ThingSpeak ",readings)                    print(" Successful  ")                    time.sleep(15)                    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()             D2 = d2.value()    if ( D2 != saved_D2):        saved_D2 = D2        print("front_stair_pir_status:       ", D2)        readings = {'field2':D2}        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(1)                    print("Write Data to ThingSpeak ",readings)                    print(" Successful  ")                    time.sleep(15)                    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()        D3 = d3.value()    if ( saved_D3 != D3):        saved_D3 = D3        print("laundry_pir_status:             ", D3)        readings = {'field3':D3}        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()                    print("Write Data to ThingSpeak ",readings)                    print(" Successful  ")                    time.sleep(15)                    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()        D4 = d4.value()    if ( saved_D4 != D4):        saved_D4 = D4        print("den_pir_status:           ", D4)        readings = {'field4':D4}        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()                    print("Write Data to ThingSpeak ",readings)                    print(" Successful  ")                    time.sleep(15)                    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()        D5 = d5.value()    if ( saved_D5 != D5):        saved_D5 = D5        print("laundry_2_doors_status:        ", D5)        readings = {'field5':D5}        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()                    print("Write Data to ThingSpeak ",readings)                    print(" Successful  ")                    time.sleep(15)                    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()        D6 = d6.value()    if ( saved_D6 != D6):        saved_D6 = D6        print("sunroom_door_status           ", D6)        readings = {'field6':D6}        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()                    print("Write Data to ThingSpeak ",readings)                    print(" Successful  ")                    time.sleep(15)                    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()        D7 = d7.value()    if ( saved_D7 != D7):        saved_D7 = D7        print("kitchen_pir_status:          ", D7)        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()                    print("Write Data to ThingSpeak ",readings)                    print(" Successful  ")                    time.sleep(15)                    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()        D8 = d8.value()    if ( saved_D8 != D8):        saved_D8 = D8        print("small_garage_door_status:       ", D8)        readings = {'field8':D8}        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()                    print("Write Data to ThingSpeak ",readings)                    print(" Successful  ")                    time.sleep(15)                    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(1) 

Statistics: Posted by shore — Sat May 11, 2024 3:10 am



Viewing all articles
Browse latest Browse all 5384

Trending Articles