Add UART Comms Example

This commit is contained in:
Alauddin Maulana Hirzan 2024-05-10 18:41:15 +07:00
parent e319bbd89b
commit a3d0606c15
Signed by: maulanahirzan
GPG key ID: 484DAC952787FA13
3 changed files with 62 additions and 0 deletions

View file

@ -0,0 +1,5 @@
# UART Communication Example Script #
These scripts contains peer-to-peer communication using UART protocol. See usage below:
* **UART_Receive.py** is a Raspberry Pi 3B+ script as Receiver. Please adjust the port **/dev/ttyAMA0** accordingly.
* **UART_SEND.py** is an ESP8266 script as Sender. Please adjust accordingly

View file

@ -0,0 +1,22 @@
import serial
import time
import ast
ser = serial.Serial(
port='/dev/ttyAMA0',
baudrate = 115200,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)
print("Receiving")
while True:
data = (ser.readline())
enc_data = data.decode("utf-8")
if len(enc_data) != 0:
data = "{"+enc_data+"}"
data = ast.literal_eval(data)
print(data)

View file

@ -0,0 +1,35 @@
from machine import UART
from machine import Pin
import utime as time
from dht import DHT11
# LED
led = Pin(25, Pin.OUT)
# Relay
# relay = Pin(27, Pin.OUT)
# UART
uart = UART(0,baudrate = 115200)
# Sensor
pin = Pin(28, Pin.IN)
sensor = DHT11(pin)
led.off()
print("=> Starting Temp and Humid Monitor")
while True:
time.sleep(1)
led.on()
t = (sensor.temperature())
h = (sensor.humidity())
sensor.measure()
print("==> Temperature: {}".format(t))
print("==> Humidity: {}".format(h))
print()
message = "'temp':{},'hum':{}".format(t,h)
uart.write(message.encode('utf-8'))
time.sleep(1)
led.off()