Codigo correspondiente a la placa conectada a un led:
#include <Arduino.h>
#include <WiFi.h>
#include <esp_now.h>
uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
String estado;
String orden;
const int led=14;
// Structure example to send data
// Must match the receiver structure
typedef struct struct_message {
String mensaje;
} struct_message;
// Create a struct_message called myData
struct_message myDataSen;
// Create a struct_message called myData
struct_message myDataRec;
esp_now_peer_info_t peerInfo;
// callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
Serial.print("\r\nLast Packet Send Status:\t");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
memcpy(&myDataRec, incomingData, sizeof(myDataRec));
orden=myDataRec.mensaje;
if (orden=="encender"){
digitalWrite(led, HIGH);
} else if (orden=="apagar")
{
digitalWrite(led, LOW);
}
}
void setup() {
// Init Serial Monitor
Serial.begin(9600);
WiFi.mode(WIFI_STA);
pinMode(led, OUTPUT);
// Init ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Once ESPNow is successfully Init, we will register for Send CB to
// get the status of Trasnmitted packet
esp_now_register_send_cb(OnDataSent);
// Once ESPNow is successfully Init, we will register for recv CB to
// get recv packer info
esp_now_register_recv_cb(OnDataRecv);
// Register peer
//Este apartado del peer es util para enviar el paquete
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
// Add peer
if (esp_now_add_peer(&peerInfo) != ESP_OK){
Serial.println("Failed to add peer");
return;
}
}
void loop() {
if(orden=="encender"){
myDataSen.mensaje="led encendido";
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &myDataSen, sizeof(myDataSen));
orden=Serial.read();
}else if (orden=="apagar")
{
myDataSen.mensaje="led apagado";
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &myDataSen, sizeof(myDataSen));
orden=Serial.read();
}
}
Codigo correspondiente a la placa conectada al ordenador por rs485:
#include <Arduino.h>
#include <SoftwareSerial.h>
#include <WiFi.h>
#include <esp_now.h>
SoftwareSerial mySerial(1, 3);
uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
String estado;
// Structure example to send data
// Must match the receiver structure
typedef struct struct_message {
String mensaje;
} struct_message;
// Create a struct_message called myData
struct_message myDataSen;
// Create a struct_message called myData
struct_message myDataRec;
esp_now_peer_info_t peerInfo;
// callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
Serial.print("\r\nLast Packet Send Status:\t");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
memcpy(&myDataRec, incomingData, sizeof(myDataRec));
estado=myDataRec.mensaje;
}
void setup()
{
mySerial.begin(9600);
WiFi.mode(WIFI_STA);
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Once ESPNow is successfully Init, we will register for Send CB to
// get the status of Trasnmitted packet
esp_now_register_send_cb(OnDataSent);
// Once ESPNow is successfully Init, we will register for recv CB to
// get recv packer info
esp_now_register_recv_cb(OnDataRecv);
// Register peer
//Este apartado del peer es util para enviar el paquete
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
// Add peer
if (esp_now_add_peer(&peerInfo) != ESP_OK){
Serial.println("Failed to add peer");
return;
}
pinMode(25, OUTPUT);
pinMode(26, OUTPUT);
delay(100);
}
void loop() // run over and over
{
String receivedData = "";
bool wordReceived = false;
while (!wordReceived)
{
mySerial.flush();
digitalWrite(25, LOW);
digitalWrite(26, LOW);
delay(100);
if (mySerial.available())
{
String receivedString = mySerial.readString();
receivedData = receivedString;
receivedString=Serial.read();
receivedData.toLowerCase();
if (receivedData=="apagar" || receivedData=="encender")
{
myDataSen.mensaje=receivedData;
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &myDataSen, sizeof(myDataSen));
wordReceived = true;
}
}
receivedData=Serial.read();
}
digitalWrite(25, HIGH);
digitalWrite(26, HIGH);
delay(100);
mySerial.println(estado);
estado=Serial.read();
delay(100);
}
Contenido del archivo platformio.ini para importar la libreria de softwareserial.h para manejar el protocolo rs485:
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[env:esp32doit-devkit-v1]
platform = espressif32
board = esp32doit-devkit-v1
framework = arduino
lib_deps = https://github.com/plerup/espsoftwareserial.git
Deja una respuesta Cancelar la respuesta