php

Wac Tutorial.


"; ?>

pico w

Button Example:

from machine import Pin
import time
from time import sleep

print('Buttons!')

led = Pin(16, Pin.OUT)    # 14 number in is Output // + of LED goes to 14

# Negative of led goes to resistor, then ground
# Button gets 3.3v on one side and thats all on high side of button
# Low side of button gets GPIO 13, and then pull down resistor 10k to ground
# Again, GPIO 13 connects beween the 10k resistor and the button low side

push_button = Pin(17, Pin.IN)  # 13 number pin is input // 

while True:
  
  logic_state = push_button.value()
  
  if logic_state == True:     # if push_button pressed
      time.sleep(3)
      led.toggle()             # led will turn ON
      print('Button s')
      #time.sleep(1)
      
#  else:                       # if push_button not pressed
      
#      led.value(0)             # led will turn OFF

Blink GPIO 16 and 17 Alternately:

# blink 16-17.py
import machine
import time
from machine import Timer
from machine import Pin
#from time import sleep
redled = machine.Pin(16, machine.Pin.OUT)
greenled = machine.Pin(17, machine.Pin.OUT)
led25 = machine.Pin("LED", machine.Pin.OUT)
#relaypin = Pin(20, Pin.OUT)
redled.off()
redled.on()
timer=Timer()
greenled.off()
#rtc = machine.RTC()
#rtc.datetime((2024, 10, 18, 2, 4, 33, 0, 0))
# (year, month, day, weekday, hours, minutes, seconds, subseconds)
#print(rtc.datetime())


def blink(timer):
    redled.toggle()
    greenled.toggle()
#relaypin.value(1)
    print("0")
#sleep(5)
#relaypin.value(0)

#sleep(5)
    print("1")  
#timer.init(freq=0.5, mode=Timer.PERIODIC, callback=blink)

while (True):
    redled.toggle()
    greenled.toggle()
    time.sleep(4)
    
    redled.toggle()
    greenled.toggle()
    time.sleep(0.5)
    
    redled.toggle()
    greenled.toggle()
    time.sleep(0.5)

Wifi 1

import network
import socket
from time import sleep
from machine import Pin
import machine
import rp2
import sys
ssid = 'COGECO-26520 - 2.4'
password = 'H0Me2024!'
powerled = machine.Pin("LED", machine.Pin.OUT)

def connect():
    #Connect to WLAN
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    wlan.connect(ssid, password, channel=2)
    sleep(14)
    while wlan.isconnected() == False:
        print('Waiting for connection...')
        powerled.on()
        sleep(0.2)
        powerled.off()
        sleep(14)
    # connected!
    #print(wlan.ifconfig())
    powerled.on()

connect()

Wifi 2 (RTC)

import network
#from mywifi import networksetting
from machine import RTC
import time
import rp2
import sys
import utime as time
import usocket as socket
import ustruct as struct
from machine import Pin

#ssid, password = networksetting()
ssid = 'COGECO-26520 - 2.4'
password = 'H0Me2024!'

piezo = machine.Pin(15, machine.Pin.OUT)
redled = machine.Pin(16, machine.Pin.OUT)
greenled = machine.Pin(17, machine.Pin.OUT)
powerled = machine.Pin("LED", machine.Pin.OUT)
greenled.off()
redled.off()
powerled.on()
time.sleep(1)
powerled.off()
time.sleep(1)

def hourfix(fixedhour):
    if fixedhour == 16:
        return 11
    if fixedhour == 17:
        return 12
    if fixedhour == 18:
        return 1
    if fixedhour == 19:
        return 2
    if fixedhour == 20:
        return 3
    if fixedhour == 21:
        return 4
    if fixedhour == 22:
        return 5
    if fixedhour == 23:
        return 6
    if fixedhour == 0:
        return 7
    if fixedhour == 1:
        return 8
    if fixedhour == 2:
        return 9
    if fixedhour == 3:
        return 10
    if fixedhour == 4:
        return 11
    if fixedhour == 5:
        return 12
    if fixedhour == 6:
        return 1
    if fixedhour == 7:
        return 2
    if fixedhour == 8:
        return 3
    if fixedhour == 9:
        return 4
    if fixedhour == 10:
        return 5
def alarmf(counter):    
    count=0
    while count < counter:
        print(f"\n## Alarm - {count} ## \n")
        redled.on()
        piezo.on()
        time.sleep(0.1)
        redled.off()
        piezo.off()
        time.sleep(1)
        count = count + 1
    
# wintertime / Summerzeit
petawawa_offset = 3200 * 6
GMT_OFFSET = 3600 * 1 # 3600 = 1 h (wintertime)
#GMT_OFFSET = 3600 * 2 # 3600 = 1 h (summertime)

# NTP-Host
NTP_HOST = 'pool.ntp.org'

# Funktion: get time from NTP Server
def getTimeNTP():
    NTP_DELTA = 2208988800
    NTP_QUERY = bytearray(48)
    NTP_QUERY[0] = 0x1B
    addr = socket.getaddrinfo(NTP_HOST, 123)[0][-1]
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    try:
        s.settimeout(1)
        res = s.sendto(NTP_QUERY, addr)
        msg = s.recv(48)
    finally:
        s.close()
    ntp_time = struct.unpack("!I", msg[40:44])[0]
    return time.gmtime(ntp_time - NTP_DELTA)

# Funktion: copy time to PI pico´s RTC
def setTimeRTC():
    tm = getTimeNTP()
    rtc.datetime((tm[0], tm[1], tm[2], tm[6], tm[3], tm[4], tm[5], 0))
    

  
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password, channel=2)
    
max_wait = 10
print('Waiting for connection')
while max_wait > 10:
    if wlan.status() < 0 or wlan.status() >= 3:
        break
    max_wait -= 1    
    sleep(1)
status = None
if wlan.status() != 3:
    raise RuntimeError('Connections failed')
else:
    status = wlan.ifconfig()
    print('connection to', ssid,'\nSuccesfull connection established!', sep=' ')
    print('IP-address: ' + status[0])
    powerled.on()
ipAddress = status[0]


rtc = RTC()  

# Zeit setzen
setTimeRTC()

# Aktuelles Datum ausgeben
print()
print(rtc.datetime())
while True:
    print(rtc.datetime())
    datetuple = rtc.datetime()
    hour = datetuple[4]
    # 4am was a 9
    print (f"{hour}:{datetuple[5]}:{datetuple[6]}")
    print (hour)
    print(f"hourfix function -> {hourfix(hour)}:{datetuple[5]}\n")
    #redled.toggle()
    if hour == 9 and datetuple[5] == 2:
        alarmf(30)
    time.sleep(40)

"; echo"

© 2024

"; ?>