U-BLOX NINA W106 SENDO PROGRAMADO EM BASIC ANNEX RDS - ESP-NOW
ESP-NOW
FUNÇÕES/COMANDOS | DESCRIÇÃO |
Ret = EspNow.Begin | Inicia as comunicações com ESP-NOW |
Ret = EspNow.Stop | Finaliza as comunicações com ESP-NOW |
Ret = EspNow.Add_Peer(MAC_add$) | Adiciona um peer (módulo) para a lista de Recepção |
Ret = EspNow.Del_Peer | Remove um peer (módulo) da lista de Recepção |
Ret = EspNow.Write(msg$) | Escreve uma mensagem para o peer definido na lista |
Ret = EspNow.Write(msg$, MAC_add$) | Escreve uma mensagem para um peer específico definido pelo seu MAC Adrress |
Todas essas funções retornam um valor; um valor diferente de zero indica que ocorreu um erro.
Se o valor retornado não for necessário, as funções podem ser usadas como comandos sem leitura em uma variável..
Exemplo :
Ret = EspNow.Write(msg$) utilizado como função
EspNow.Write msg$ utilizado como comando (sem parênteses)
FUNÇÕES STRING | DESCRIÇÃO |
Msg$ = EspNow.READ$ | Lê a mensagem recebida |
MAC_add$ = ESPNow.REMOTE$ | Lê o MAC Address do emissor da mensagem recebida |
Err$ = ESPNow.ERROR$ | Lê o MAC Adress do(s) dispositvos que não receberam as mensagens. |
EVENTOS | DESCRIÇÃO |
OnEspNowMsg label |
|
OnEspNowError label |
Isso acontece, em particular, quando o dispositivo receptor não recebeu a mensagem |
Introdução ao ESP-NOW
Apresentando o ESP-NOW
Comunicação unidirecional ESP-NOW
- Uma placa u-BLOX NINA W106 enviando dados para outra placa u-BLOX NINA W106
- Um u-BLOX NINA W106 "mestre" enviando dados para vários "escravos" do u-BLOX NINA W106
- Um "escravo" do u-BLOX NINA W106 recebendo dados de vários "mestres"
Comunicação bidirecional ESP-NOW
u-BLOX ninaW106: Obtendo o endereço MAC da placa
Print MAC$
62:01:94:5E:37:8D
Comunicação ponto a ponto unidirecional ESP-NOW
CÓDIGO DO MÓDULO RECEIVER
'ESP-NOW receiver example
print "init " ; espnow.begin ' should print 0 if all OK
onEspNowMsg message ' set the place where jump in case of message reception
wait
message:
print "RX:"; espnow.read$; " from "; espnow.remote$ ' print the message
return
CÓDIGO DO MÓDULO SENSOR
'ESP-NOW sensor example
RECEIVER_MAC$ = "60:01:94:51:D0:7D" ' MAC address of the receiver
print "init " ; espnow.begin ' should print 0 if all OK
espnow.add_peer RECEIVER_MAC$ ' set the address of the receiver
onEspNowError status ' set the place where jump in case of TX error
timer0 1000, sendMessage ' trigger a message at each second
wait
sendMessage:
espnow.write "Sensor 1 : " + str$(rnd(1000)) ' send the message
return
status:
print "TX error on "; espnow.error$ ' print the error
returnOs módulos sensores enviarão uma mensagem a cada segundo para o receptor.
O receptor imprimirá essas mensagens no console.
Código para o módulo SENSOR1
'ESP-NOW sensor1 example
RECEIVER_MAC$ = "60:01:94:51:D0:7D" ' MAC address of the receiver
print "init " ; espnow.begin ' should print 0 if all OK
espnow.add_peer RECEIVER_MAC$ ' set the address of the receiver
onEspNowError status ' set the place where jump in case of TX error
timer0 1000, sendMessage ' trigger a message at each second
wait
sendMessage:
espnow.write "Sensor 1 : " + str$(rnd(1000)) ' send the message
return
status:
print "TX error on "; espnow.error$ ' print the error
return
Código para o módulo SENSOR2
'ESP-NOW sensor2 example
RECEIVER_MAC$ = "60:01:94:51:D0:7D" ' MAC address of the receiver
print "init " ; espnow.begin ' should print 0 if all OK
espnow.add_peer RECEIVER_MAC$ ' set the address of the receiver
onEspNowError status ' set the place where jump in case of TX error
timer0 1000, sendMessage ' trigger a message at each second
wait
sendMessage:
espnow.write "Sensor 1 : " + str$(rnd(1000)) ' send the message
return
status:
print "TX error on "; espnow.error$ ' print the error
return
Código para o módulo RECEIVER
'ESP-NOW receiver example
print "init " ; espnow.begin ' should print 0 if all OK
onEspNowMsg message ' set the place where jump in case of message reception
wait
message:
print "RX:"; espnow.read$; " from "; espnow.remote$ ' print the message
return
Vamos ver outro exemplo sempre com 2 sensores e um receptor.
Neste caso, o receptor retransmitirá de volta ao sensor 1 a mensagem recebida.
Isso significa que o sensor 1 receberá a mensagem vinda do sensor 2 e também sua própria mensagem.
O sensor 1 e o receptor irão imprimir as mensagens recebidas no console
Código para o módulo SENSOR1
'ESP-NOW sensor example
RECEIVER_MAC$ = "60:01:94:51:D0:7D"
print "init " ; espnow.begin ' should print 0 if all OK
espnow.add_peer RECEIVER_MAC$ ' set the address of the receiver
onEspNowMsg message ' set the place where jump in case of message reception
onEspNowError status ' set the place where jump in case of TX error
timer0 1000, sendMessage ' trigger a message at each second
wait
sendMessage:
espnow.write "Sensor 1 : " + str$(rnd(1000)) ' send the message
return
message:
print "RX:"; espnow.read$; " from "; espnow.remote$ ' print message
return
status:
print "TX error on "; espnow.error$ ' print the error
return
Código para o módulo SENSOR2
'ESP-NOW sensor2 example
RECEIVER_MAC$ = "60:01:94:51:D0:7D" ' MAC address of the receiver
print "init " ; espnow.begin ' should print 0 if all OK
espnow.add_peer RECEIVER_MAC$ ' set the address of the receiver
onEspNowError status ' set the place where jump in case of TX error
timer0 1000, sendMessage ' trigger a message at each second
wait
sendMessage:
espnow.write "Sensor 1 : " + str$(rnd(1000)) ' send the message
return
status:
print "TX error on "; espnow.error$ ' print the error
return
Código para o módulo RECEIVER
'ESP-NOW receiver example #2
SENSOR1_MAC$ = "68:C6:3A:C3:06:DB"
print "init " ; espnow.begin ' should print 0 if all OK
espnow.add_peer SENSOR1_MAC$ ' set the address of the sensor 1
onEspNowMsg message ' set the place where jump in case of message reception
wait
message:
msg$ = espnow.read$
rem$ = espnow.remote$
print "RX:"; msg$; " from "; rem$
ret = espnow.write( msg$ + " Transferred from " + rem$ ) ' transfer the message received
return
status:
print "TX error on "; espnow.error$ ' print the message
return
Vejamos agora outro exemplo em que um sensor enviará uma mensagem para 2 destinatários:
Código para o módulo SENSOR
'ESP-NOW sensor example
RECEIVER1_MAC$ = "60:01:94:51:D0:7D"
RECEIVER2_MAC$ = "68:C6:3A:C3:06:DB"
print "init " ; espnow.begin ' should print 0 if all OK
espnow.add_peer RECEIVER1_MAC$ ' set the address of the receiver 1
espnow.add_peer RECEIVER2_MAC$ ' set the address of the receiver 2
onEspNowError status ' set the place where jump in case of TX error
timer0 1000, sendMessage ' trigger a message at each second
wait
sendMessage:
espnow.write "Sensor : " + str$(rnd(1000)) ' send the message
return
status:
print "TX error on "; espnow.error$ 'print the error
return
Código para o módulo RECEIVER 1
'ESP-NOW receiver example
print "init " ; espnow.begin ' should print 0 if all OK
onEspNowMsg message ' set the place where jump in case of message reception
wait
message:
print "RX:"; espnow.read$; " from "; espnow.remote$ ' print the message
return
Código para o módulo RECEIVER 2
'ESP-NOW receiver example
print "init " ; espnow.begin ' should print 0 if all OK
onEspNowMsg message ' set the place where jump in case of message reception
wait
message:
print "RX:"; espnow.read$; " from "; espnow.remote$ ' print the message
return
Nenhum comentário:
Postar um comentário