-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
2,722 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# WSN-MICROPYTHON | ||
This is a basic repository to form a network of nodes using the NRF24L01 transceiver with esp8266 and send Payloads to the RPI Gateway and then be sent to the cloud through MQTT |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
este debe ser su clave generada por AWS - ESTE DOCUMENTO ES DE EJEMPLO ESTA ES LA CLAVEBINARIZADA |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
CLAVE GENERADA POR AWS IOT CORE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ESTE DEBE SER SU CLAVE PRIVADA CONVERTIDA EN NUMEROS - DEBE SER CONVERTIDA POR MEDIO DE OTRA COSA |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
CLAVE GENERADA POR AWS CORE CERTIFICADO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# WSN-MICROPYTHON | ||
This is a basic repository to form a network of nodes using the NRF24L01 transceiver with esp8266 and send Payloads to the RPI Gateway and then be sent to the cloud through MQTT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
MicroPython | ||
|
||
Herramienta de CLI para flash esp8266 esp32 | ||
> sudo pip3 install esptool | ||
|
||
Herramienta shell para ejecutar con micropython | ||
> sudo pip3 install rshell | ||
|
||
Obtener puerto usb del dispositivo con su CONTROLADOR UART | ||
> dmesg | grep ttyUSB | ||
|
||
Obtener caracteristicas de la placa ESP | ||
> esptool.py --port /dev/ttyUSB0 flash_id | ||
|
||
Borrar memoria flash de Microcontrolador | ||
> esptool.py --port /dev/ttyUSB0 erase_flash | ||
|
||
Deploy the new firmware | ||
Primera Forma | ||
> esptool.py --port /dev/ttyUSB0 --baud 115200 write_flash --flash_size=detect 0 esp8266-20191220-v1.12.bin | ||
|
||
Segunda Forma FlashRom -fm dio | ||
> esptool.py --port /dev/ttyUSB0 --baud 115200 write_flash --flash_size=detect -fm dio 0 esp8266-20191220-v1.12.bin | ||
|
||
|
||
Conectarse por medio de comandos shell a la esp8266 | ||
> rshell --buffer-size=30 -p /dev/ttyUSB0 | ||
|
||
Ver las boards disponibles -> Tiene que estar dentro del RSHELL | ||
> boards | ||
|
||
Ejecutar REPL - SALIR CTRL + X | ||
> REPL | ||
|
||
Prender y Apagar LED Con REPL -- Con RSHELL ACTIVADO | ||
>>> import machine | ||
>>> import time | ||
>>> pin = machine.Pin(2, machine.Pin.OUT) | ||
>>> pin.off() | ||
>>> pin.on() | ||
>>> while(1): | ||
... pin.on() | ||
... time.sleep(1) | ||
... pin.off() | ||
... time.sleep(1) | ||
... | ||
... | ||
... | ||
>>> ---- SALIR CON CTROL + X | ||
|
||
|
||
Importar SCRIPT al board para ser ejecutado | ||
Dentro de RSHELL Se ejecuta lo siguiente, teniendo un script ya realizado | ||
> rshell --buffer-size=30 -p /dev/ttyUSB0 | ||
> ls /pyboard | ||
> cp test.py /pyboard | ||
> REPL | ||
>>> import test | ||
El script se ejecuta ciando se llama al import | ||
|
||
|
||
|
||
Comunicacion serial con minicom a la esp8266 | ||
> sudo apt-get install minicom | ||
|
||
Iniciar minicom | ||
> minicom -s | ||
|
||
Los comandos de minicom deben estar configurados de la siguiente manera | ||
> Serial device /dev/ttyUSB0 | ||
> lockfile Location /var/lock | ||
> Bps/Par/Bits 115200 SN1 | ||
> Hardware Flow COntrol No | ||
> Software Flow Control No | ||
Para Iniciar Minicom se guarda el documento y luego se dirige a EXIT | ||
Allí comienza la comunicacion con la BOARD | ||
Para salir de minicom y finalizar la conexion | ||
> CTRL + A + Q | ||
|
||
|
||
|
||
Para mirar todos los archivos que estan dentro del board | ||
> import os | ||
> os.listdir() | ||
|
||
|
||
|
||
Para guardar en memoria de la esp8266 un Script este se debe llamar main.py | ||
SE EJECUTARA DESPUES DE BOOT.PY | ||
> rshell --buffer-size=30 -p /dev/ttyUSB0 | ||
> ls /pyboards | ||
> cp main.py /pyboards/main.py | ||
|
||
|
||
|
||
PARA CARGAR NUEVAS APLICACIONES AL BOARD UTILIZANDO RSHELL sin tener que ingrasar a la terminal de rshell y luego salir de la herramienta | ||
> rshell --port /dev/ttyUSB0 cp main.py /pyboard/main.py | ||
|
||
PARA BORRAR LA APLICACION DE LA BOARD Y/O BORRAR OTRO DOCUMENTO DENTRO DE LA BOARD | ||
> rshell --port /dev/ttyUSB0 rm /pyboard/main.py | ||
|
||
|
||
|
||
|
||
HERRAMIENTA PYBOARD PARA GESTION DE LA TARGETA | ||
Herramienta de Mycropython | ||
PYBOARD.PY | ||
Cargar script solo en ROM, NO LO ESCRIBE EN LOS ARCHIVOS | ||
> ./pyboard.py --device /dev/ttyUSB0 main.py | ||
Para detener CTRL+C pero el board seguirá ejecutando hasta que re reinicie | ||
|
||
|
||
Filesystem access - PYBOARD | ||
-f ...para acceder al directorio | ||
|
||
-cat path | ||
|
||
-ls [path] | ||
|
||
-rm path | ||
|
||
-mkdir path | ||
|
||
-rmdir path | ||
|
||
|
||
El cpcomando utiliza una sshconvención similar para referirse a archivos locales y remotos. Cualquier ruta que comience con a :se interpretará como en el dispositivo; de lo contrario, será local. Entonces: | ||
> pyboard.py --device /dev/ttyUSB0 -f cp main.py :main.py | ||
|
||
------------------------------------------------------------------------- | ||
------------------------------------------------------------------------- | ||
|
||
copiará main.py del directorio actual en la PC a un archivo llamado main.py en el dispositivo. El nombre del archivo se puede omitir, por ejemplo: | ||
> pyboard.py --device/dev/ttyUSB0 -f cp main.py: | ||
|
||
------------------------------------------------------------------------- | ||
------------------------------------------------------------------------- | ||
|
||
Copie tres archivos al dispositivo, manteniendo sus nombres | ||
y rutas (nota: `lib` debe existir en el dispositivo) | ||
> pyboard.py --device /dev/ttyUSB0 -f cp main.py app.py lib/foo. py: | ||
|
||
------------------------------------------------------------------------- | ||
------------------------------------------------------------------------- | ||
|
||
Eliminar un archivo del dispositivo. | ||
> pyboard.py --device /dev/ttyUSB0 -f rm util.py | ||
|
||
------------------------------------------------------------------------- | ||
------------------------------------------------------------------------- | ||
|
||
Imprime el contenido de un archivo en el dispositivo | ||
> pyboard.py --device /dev/ttyUSB0 -f cat boot.py | ||
|
||
|
||
////////////MICIOM///////////////////////////////////////////// | ||
|
||
Para acceder al minicon sin mas unidades USB en serial | ||
minicon -s | ||
|
||
|
||
Si hay varios dispositivos conectados se debe especificar el puerto | ||
minicom -D /dev/ttyUSB0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
from umqtt.robust import MQTTClient | ||
import machine | ||
import time | ||
import network | ||
import urequests | ||
import utime | ||
import rf24 | ||
import ujson | ||
|
||
|
||
|
||
#import os | ||
#os.listdir() | ||
#print(os.listdir()) | ||
|
||
clientId="piId", | ||
thingName="GREENHOUSE-GATEWAY" | ||
awsHost = 'a2ejpqeqsr9chb-ats.iot.us-east-1.amazonaws.com' #EndPoint | ||
awsPort = 8883 #Port No | ||
#caPem = "/AmazonRootCA1.pem" # Root_CA_Certificate_Name | ||
#keyPath = "/4f44f5eed4-private.pem.key" # <Thing_Name>.pem.key | ||
keyPath = "/4f44f5eed4-private.key.der" # <Thing_Name>.pem.key | ||
#certPath = "/4f44f5eed4-certificate.pem.crt" # <Thing_Name>.pem.crt | ||
certPath = "/4f44f5eed4-certificate.cert.der" # <Thing_Name>.pem.crt | ||
|
||
|
||
with open(certPath, 'rb') as f: | ||
certData = f.read() | ||
#print(certData) | ||
|
||
with open(keyPath, 'rb') as f: | ||
keyData = f.read() | ||
#print(keyData) | ||
|
||
#mqtt_server = '192.168.1.50' | ||
topic_sub = 'telemetry/data' | ||
#messagge = 'holamundo' | ||
#userId = 'greenhouse' | ||
#passId = 'Telemetry20.' | ||
|
||
|
||
url = "http://worldtimeapi.org/api/timezone/America/Bogota" | ||
#http://worldtimeapi.org/timezones | ||
rtc = machine.RTC() | ||
|
||
|
||
sendNode01 = False | ||
sendNode02 = False | ||
sendNode03 = False | ||
sendNode04 = False | ||
sendNode05 = False | ||
|
||
|
||
def netWorkConnect(SSID, PASSWORD): | ||
nic = network.WLAN(network.STA_IF) | ||
ap = network.WLAN(network.AP_IF) | ||
if not nic.isconnected(): | ||
print('connecting to network...') | ||
nic.active(True) | ||
|
||
nic.connect(SSID, PASSWORD) | ||
while not nic.isconnected(): | ||
#print(".", end="") | ||
pass | ||
ap.active(False) | ||
print('network config:', nic.ifconfig()) | ||
return True | ||
|
||
print("Iniciando......") | ||
|
||
if netWorkConnect('YOUR_SSID','YOUR_PASSWORD'): | ||
#print('Connected') | ||
response = urequests.get(url) | ||
if response.status_code == 200: | ||
#print("JSON response:\n", response.text) | ||
parsed = response.json() | ||
|
||
datetime_str = str(parsed["datetime"]) | ||
year = int(datetime_str[0:4]) | ||
month = int(datetime_str[5:7]) | ||
day = int(datetime_str[8:10]) | ||
hour = int(datetime_str[11:13]) | ||
minute = int(datetime_str[14:16]) | ||
second = int(datetime_str[17:19]) | ||
subsecond = int(round(int(datetime_str[20:26]) / 10000)) | ||
#update internal RTC | ||
rtc.datetime((year, month, day, 0, hour, minute, second, subsecond)) | ||
update_time = utime.ticks_ms() | ||
print("RTC updated\n") | ||
|
||
date_str = "Date: {1:02d}/{2:02d}/{0:4d}".format(*rtc.datetime()) | ||
time_str = "Time: {4:02d}:{5:02d}:{6:02d}".format(*rtc.datetime()) | ||
print('Date: ' + date_str) | ||
print('Hour: ' + time_str) | ||
##Connect to mqtt | ||
try: | ||
client = MQTTClient(thingName, server=awsHost, port=awsPort, ssl=True, keepalive=10000, ssl_params={'key':keyData,'cert':certData,"server_side":False}) | ||
client.connect() | ||
except: | ||
pass | ||
print('Conctado a AWS') | ||
#client.publish(topic_sub, "{ 'testFuncional': 'true' }") | ||
|
||
#NodoDir 1 d2 D2 NODO05 invernadero -- 01 -- 21 | ||
#NodoDir 2 d4 D4 NODO05 airelibre -- 02 -- 12 | ||
#NodoDir 3 d6 D6 NODO05 invernadero -- 03 -- 23 | ||
#NodoDir 4 d8 D8 NODO05 airelibre -- 04 -- 14 | ||
#NodoDir 5 db DB NODO05 Invernadero -- 05 -- 25 | ||
pipesa = (b"\xe1\xf0\xf0\xf0\xf0", b"\xd2\xf0\xf0\xf0\xf0") # -- NODO05 D2 | ||
pipesb = (b"\xe1\xf0\xf0\xf0\xf0", b"\xd4\xf0\xf0\xf0\xf0") # -- NODO05 D4 | ||
pipesc = (b"\xe1\xf0\xf0\xf0\xf0", b"\xd6\xf0\xf0\xf0\xf0") # -- NODO05 D6 | ||
pipesd = (b"\xe1\xf0\xf0\xf0\xf0", b"\xd8\xf0\xf0\xf0\xf0") # -- NODO05 D8 | ||
pipese = (b"\xe1\xf0\xf0\xf0\xf0", b"\xdb\xf0\xf0\xf0\xf0") # -- NODO05 DB | ||
#pipes = (b"\xe1\xf0\xf0\xf0\xf0", b"\xd2\xf0\xf0\xf0\xf0") | ||
#------------ slave --------------- master ------------ | ||
|
||
body={} | ||
errs = 0 | ||
send = False | ||
algomas = True | ||
while True: | ||
#try: | ||
#time.sleep(1) | ||
|
||
currentMinute = int("{5:02d}".format(*rtc.datetime())) | ||
if currentMinute%5==0: | ||
send = True | ||
|
||
if algomas: | ||
sendMqtt = True | ||
|
||
if sendMqtt: | ||
algomas = False | ||
|
||
if not sendNode01: | ||
try: | ||
packG = rf24.slave(pipesa,1) | ||
except: | ||
pass | ||
isidA = packG.find("'S':'21'") | ||
if isidA > 0: | ||
sendNode01 = True | ||
body['node01'] = str(packG) | ||
print(body) | ||
|
||
elif not sendNode02: | ||
|
||
packB = rf24.slave(pipesb,2) | ||
isidb = packB.find("'S':'12'") | ||
if isidb > 0: | ||
sendNode02 = True | ||
body['node02'] = packB | ||
print(body) | ||
|
||
elif not sendNode03: | ||
|
||
packC = rf24.slave(pipesc,3) | ||
isidc = packC.find("'S':'23'") | ||
if isidc > 0: | ||
sendNode03 = True | ||
body['node03'] = packC | ||
print(body) | ||
|
||
elif not sendNode04: | ||
|
||
packD = rf24.slave(pipesd,4) | ||
isidd = packD.find("'S':'14'") | ||
if isidd > 0: | ||
sendNode04 = True | ||
body['node04'] = packD | ||
print(body) | ||
|
||
elif not sendNode05: | ||
|
||
packE = rf24.slave(pipese,0) | ||
iside = packE.find("'S':'25'") | ||
if iside > 0: | ||
sendNode05 = True | ||
body['node05'] = packE | ||
print(body) | ||
#sendNode01 = False | ||
|
||
else: | ||
print("All get package") | ||
try: | ||
jsonBody = ujson.dumps(body) | ||
#print(jsonBody) | ||
client.publish(topic_sub, jsonBody) | ||
except: | ||
pass | ||
del body | ||
body = {} | ||
sendMqtt = False | ||
else: | ||
algomas = True | ||
sendNode01 = False | ||
sendNode02 = False | ||
sendNode03 = False | ||
sendNode04 = False | ||
sendNode05 = False | ||
time.sleep(3) |
Oops, something went wrong.