Add UART Comms Example
This commit is contained in:
parent
e319bbd89b
commit
a3d0606c15
3 changed files with 62 additions and 0 deletions
5
Example - UART Comms/README.md
Normal file
5
Example - UART Comms/README.md
Normal 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
|
22
Example - UART Comms/UART_Receive.py
Normal file
22
Example - UART Comms/UART_Receive.py
Normal 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)
|
35
Example - UART Comms/UART_Send.py
Normal file
35
Example - UART Comms/UART_Send.py
Normal 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()
|
Loading…
Reference in a new issue