From a3d0606c15fdb68f1d6d695108f04e8fb6edfb28 Mon Sep 17 00:00:00 2001 From: Alauddin Maulana Hirzan Date: Fri, 10 May 2024 18:41:15 +0700 Subject: [PATCH] Add UART Comms Example --- Example - UART Comms/README.md | 5 ++++ Example - UART Comms/UART_Receive.py | 22 +++++++++++++++++ Example - UART Comms/UART_Send.py | 35 ++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 Example - UART Comms/README.md create mode 100644 Example - UART Comms/UART_Receive.py create mode 100644 Example - UART Comms/UART_Send.py diff --git a/Example - UART Comms/README.md b/Example - UART Comms/README.md new file mode 100644 index 0000000..87226a3 --- /dev/null +++ b/Example - UART Comms/README.md @@ -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 diff --git a/Example - UART Comms/UART_Receive.py b/Example - UART Comms/UART_Receive.py new file mode 100644 index 0000000..0d3f82b --- /dev/null +++ b/Example - UART Comms/UART_Receive.py @@ -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) diff --git a/Example - UART Comms/UART_Send.py b/Example - UART Comms/UART_Send.py new file mode 100644 index 0000000..e848b6c --- /dev/null +++ b/Example - UART Comms/UART_Send.py @@ -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() \ No newline at end of file