ESP32-C3 Super Mini+AHT10+Ra-08H LoRaWAN MicroPython Node

Somsak Lima
2 min readFeb 25, 2024

ต่ออุปกรณ์ลงบน Breadboard ตาม Diagram

  1. Blink.py
from machine import Pin
import machine
from time import sleep
led = Pin(8, Pin.OUT)
while True:
led.value(1)
sleep(0.5)
led.value(0)
sleep(0.5)

2. ScanI2C.py

import machine
i2c = machine.SoftI2C(scl=machine.Pin(9), sda=machine.Pin(8), freq=20000)
print('Scan i2c bus...')
devices = i2c.scan()
if len(devices) == 0:
print("No i2c device !")
else:
print('i2c devices found:',len(devices))

for device in devices:
print("Decimal address: ",device," | Hexa address: ",hex(device))

ผลการ RUN

>>> %run -c $EDITOR_CONTENT
Scan i2c bus...
i2c devices found: 1
Decimal address: 56 | Hexa address: 0x38

3. AT_UART.py

from machine import UART
import time
from time import sleep
uart = UART(1,baudrate=9600, rx=20, tx=21, timeout=1000)
rstr=""
def sendATcommand(ATcommand):
print("Command: {0}\r\n".format(ATcommand))
uart.write("{0}\r\n".format(ATcommand))
rstr=uart.read().decode('utf-8')
print(rstr)
return (rstr)


sendATcommand ("AT+CDEVEUI?")
sendATcommand ("AT+CAPPKEY?")
sendATcommand ("AT+CAPPEUI?")
sendATcommand ("AT+CJOIN=1,0,8,1")

4. PrintAHT0.py

import machine
import utime
from machine import Pin, SoftI2C
import ahtx0
i2c = machine.SoftI2C(scl=machine.Pin(9), sda=machine.Pin(8), freq=20000)
sensor = ahtx0.AHT10(i2c)

#print(bme.values)
print("Temp=",round((sensor.temperature),2))
print("Humid=",round((sensor.relative_humidity),2))

ผลการ run

MicroPython v1.22.1 on 2024-01-05; ESP32C3 module with ESP32C3

Type "help()" for more information.

>>> %run -c $EDITOR_CONTENT
Temp= 28.06
Humid= 33.11

5. ESP32C3_AHT10.py

# Write # By Somsak Lima and Itti  Srisumalai
import machine
import ahtx0
import time
import ubinascii
from machine import UART, Pin, SoftI2C
from cayennelpp import CayenneLPP

stop = False
motion = 0
led = Pin(8, Pin.OUT)

i2c = machine.SoftI2C(scl=machine.Pin(9), sda=machine.Pin(8), freq=20000)
uart = UART(1,baudrate=9600, rx=20, tx=21, timeout=1000)

def sendATcommand(ATcommand):
rstr = ""
print("Command: {0}\r\n".format(ATcommand))
uart.write("{0}\r\n".format(ATcommand))
rstr = uart.read().decode('utf-8')
print(rstr)
return (rstr)

sendATcommand('AT+CADR=0')
sendATcommand('AT+CDATARATE=2')
sendATcommand('AT+CULDLMODE=1')

sendATcommand("AT+CDEVEUI?")
sendATcommand("AT+CAPPKEY?")
sendATcommand("AT+CAPPEUI?")
sendATcommand("AT+CJOIN=1,0,8,1")
time.sleep(20.0)
arstr = ''

arstr = sendATcommand('AT+CSTATUS?')

while '+CSTATUS:04' not in arstr:
print('Retry OTAA Continue')
sendATcommand('AT+CJOIN=1,0,8,2')
time.sleep(20.0)
arstr = ''
arstr = sendATcommand('AT+CSTATUS?')

if '+CSTATUS:04' in arstr:
print('++++OTAA OK+++++')
break

if '+CSTATUS:04' in arstr:
print('++++OTAA OK+++++')
#Send Sample data
sendATcommand('AT+DTRX=0,2,6,445566')

cnt = 1
relay1 = Pin(12, Pin.OUT)
button = Pin(4, Pin.IN)
button.value()
sensor = ahtx0.AHT10(i2c)
while True:
print("----------Wait 10 Second---------")
time.sleep(2.0)
print("\r\n\r\nPacket No #{}".format(cnt))
print("-----------------------------------")
print("AHT10 values:")
print("Temp=",round((sensor.temperature),2))
print("Humid=",round((sensor.relative_humidity),2))
print("-----------------------------------")

c = CayenneLPP()
c.addTemperature(1, round(sensor.temperature,2)) # Add temperature read to channel 1
c.addRelativeHumidity(2, round(sensor.relative_humidity,2)) # Add relative humidity read to channel 2 c.addDigitalOutput(4, button.value() )
b = (ubinascii.hexlify(c.getBuffer()))
led.value(1)
print('')
print('************ Sending Data Status **************')
sendATcommand('AT+DTRX=0,1,{0},{1}'.format(int(len(b)),(b.decode('utf-8'))))
time.sleep(4.0)
arstr=sendATcommand('AT+CSTATUS?')
if "+CSTATUS:08" in arstr:
arstr=sendATcommand('AT+DRX?')
print(arstr)
if "010203" in arstr:
print("Command1 Detected: On LED =============>")
print("Command1 Detected: On LED =============>")
print("Command1 Detected: On LED =============>")
relay1.value(1)
#time.sleep(5)
if "040506" in arstr:
print("Command1 Detected: Off LED =============>")
print("Command1 Detected: Off LED =============>")
print("Command1 Detected: Off LED =============>")
relay1.value(0)
#time.sleep(5)

print('********Finish Sending & Receiving Data Status******')

led.value(0)
cnt = cnt + 1

--

--

Somsak Lima

สนับสนุนและส่งเสริมให้ผู้สนใจสามารถใช้งานเทคโนโลยี LoRa และ LoRaWAN ได้ โดยนำความรู้ที่ได้ไปต่อยอดเพื่อใช้งาน