This blog is a record of what I do, what I think and what I read. If you are interested to know about my professional experience, you can refer to the other blog of mine. http://admi2camac.blogspot.com/
Thursday, 9 February 2023
Morse Code - SOS
from machine import Pin
from time import sleep
# the message to send in morse-code
messages = ["sos sos sos sos sos sos"]
#timing for encoding
beatLen = 0.15
#length of dits and dahs
ditLen = beatLen * 1
dahLen = beatLen * 3
#assign the signal lamp pin and amke sure its off
signalLamp = Pin(15, Pin.OUT)
signalLamp.off()
#according to stories I've heard...
hellNotFrozen = True
#table to transform readable letters into morse code
code = {'A':'.-', 'B':'-...',
'C':'-.-.', 'D':'-..', 'E':'.',
'F':'..-.', 'G':'--.', 'H':'....',
'I':'..', 'J':'.---', 'K':'-.-',
'L':'.-..', 'M':'--', 'N':'-.',
'O':'---', 'P':'.--.', 'Q':'--.-',
'R':'.-.', 'S':'...', 'T':'-',
'U':'..-', 'V':'...-', 'W':'.--',
'X':'-..-', 'Y':'-.--', 'Z':'--..',
'1':'.----', '2':'..---', '3':'...--',
'4':'....-', '5':'.....', '6':'-....',
'7':'--...', '8':'---..', '9':'----.',
'0':'-----', ', ':'--..--', '.':'.-.-.-',
'?':'..--..', '/':'-..-.', '-':'-....-',
'(':'-.--.', ')':'-.--.-'}
while hellNotFrozen:
#loop through the list of messages
for message in messages:
# more code only has uppercase
message = message.upper().strip()
#loop through each character in the message
for xmitChr in message:
print(xmitChr, end = '') # show off the character on
if xmitChr == ' ': # space means end of word
# end of word - wait 4 more beats (7 total)
sleep(beatLen * 4)
else:# encode a character
#loop through the ditdah code for this character
for ditDah in code[xmitChr]:
#turn the signal lamp on and count beats
signalLamp.on()
if ditDah == ".":
sleep(ditLen)
else:
sleep(dahLen)
# turn off signal lamp and 1 beat pause between
signalLamp.off()
sleep(beatLen)
# end of character - wait for 2 more beats (3 in total)
sleep(beatLen * 2)
#end of message - wait 4 more beats (7 in total)
sleep(beatLen * 4)
print()
#https://www.youtube.com/watch?v=cCSYFUeO-CU
Observe the video for 'short short short', 'long long long', 'short short short'
Wednesday, 8 February 2023
Control an LED through Wi-Fi with a Raspberry Pi PICO W
import network
import socket
import time
import secret1
from machine import Pin
led = Pin(15, Pin.OUT)
ssid = secret1.SSID
password = secret1.PASSWORD
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
html = """
Raspberry Pi Pico
LED Status Indicator
%s
""" # Wait for connect or fail max_wait = 10 while max_wait > 0: if wlan.status() < 0 or wlan.status() >= 3: break max_wait -= 1 print('waiting for connection...') time.sleep(1) # Handle connection error if wlan.status() != 3: raise RuntimeError('network connection failed') else: print('connected') status = wlan.ifconfig() print( 'ip = ' + status[0] ) # Open socket addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1] s = socket.socket() s.bind(addr) s.listen(1) print('listening on', addr) # Listen for connections while True: try: cl, addr = s.accept() print('client connected from', addr) request = cl.recv(1024) print(request) request = str(request) led_on = request.find('/light/on') led_off = request.find('/light/off') print( 'led on = ' + str(led_on)) print( 'led off = ' + str(led_off)) if led_on == 6: print("led on") led.value(1) stateis = "LED is ON" if led_off == 6: print("led off") led.value(0) stateis = "LED is OFF" response = html % stateis cl.send('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n') cl.send(response) cl.close() except OSError as e: cl.close() print('connection closed') #https://www.youtube.com/watch?v=51m7umIEjd4Monday, 6 February 2023
Traffic Light Controller with Raspberry Pi Pico & MicroPython
from machine import Pin
from time import sleep
import tm1637
tm = tm1637.TM1637(clk=Pin(4), dio=Pin(5))
Led_R = Pin(0, Pin.OUT)
Led_Y = Pin(1, Pin.OUT)
Led_G = Pin(2, Pin.OUT)
while True:
num = 30
Led_R.value(1)
for i in range(30):
num=num-1
tm.number(num)
sleep(1)
Led_R.value(0)
for i in range(5):
Led_Y.value(1)
sleep(0.3)
Led_Y.value(0)
sleep(0.3)
Led_G.value(1)
sleep(10)
Led_G.value(0)
#https://how2electronics.com/traffic-light-controller-with-raspberry-pi-p
Feb05 Display time and date using f-string
#Libraries
from machine import Pin, I2C #,RTC , WDT
from ssd1306 import SSD1306_I2C
import utime
#set up
WIDTH =128
HEIGHT= 64
i2c=I2C(0,scl=Pin(17),sda=Pin(16),freq=200000)
oled = SSD1306_I2C(WIDTH,HEIGHT,i2c)
week=["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
while True:
#time.sleep(1)
date_time_now = utime.time()
date_time_now=utime.localtime(date_time_now)
print(date_time_now)
#date_time_now is in this tuple format
#(2022, 12, 15, 14, 54, 27, 3, 349)
# 0 1 2 3 4 5 6 7
#local_date_time=str(date_time_now[3]) + ':' + str(date_time_now[4]) + ':' + str(date_time_now[5])
local_year_day=f"{date_time_now[0]}/{date_time_now[1]:02}/{date_time_now[2]:02}"
print(local_year_day)
local_date_time=f"{date_time_now[3]:02}:{date_time_now[4]:02}:{date_time_now[5]:02}"
print(local_date_time)
local_wk_day=f"{week[date_time_now[6]]}"
print(local_wk_day)
#display in ssd1306
oled.fill(0) #clear display
oled.text("Today's date is ",0,0)
oled.text(local_year_day, 0, 14)
oled.text(local_wk_day,0,28)
oled.text("Current time is ", 0,42)
oled.text(local_date_time, 0, 56)
oled.show()
utime.sleep(5)
Subscribe to:
Posts (Atom)