Skip to content

Instantly share code, notes, and snippets.

@hekras
Last active September 18, 2022 10:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hekras/38009f6801a8526ce59bc7d46f6136e2 to your computer and use it in GitHub Desktop.
Save hekras/38009f6801a8526ce59bc7d46f6136e2 to your computer and use it in GitHub Desktop.
Code for remotecontrol
#include <ESP8266WiFi.h>
#include <espnow.h>
int a0 = 5;
int a1 = 4;
int a2 = 0;
int a3 = 2;
int a4 = 10;
int a5 = 13;
int a6 = 12;
int a7 = 14;
// REPLACE WITH RECEIVER MAC Address
uint8_t broadcastAddress[] = {0x84, 0xF3, 0xEB, 0x80, 0xF2, 0x42};
// Structure example to send data
// Must match the receiver structure
typedef struct struct_message {
int a0;
int a1;
int a2;
int a3;
int a4;
int a5;
int a6;
int a7;
} struct_message;
// Create a struct_message called myData
struct_message myData;
unsigned long lastTime = 0;
unsigned long timerDelay = 200; // send readings timer
// Callback when data is sent
void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
Serial.print("Last Packet Send Status: ");
if (sendStatus == 0){
Serial.println("Delivery success");
}
else{
Serial.println("Delivery fail");
}
}
void setup() {
// Init Serial Monitor
Serial.begin(115200);
pinMode(a0, INPUT);
pinMode(a1, INPUT);
pinMode(a2, INPUT);
pinMode(a3, INPUT);
pinMode(a4, INPUT);
pinMode(a5, INPUT);
pinMode(a6, INPUT);
pinMode(a7, INPUT);
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
// Init ESP-NOW
if (esp_now_init() != 0) {
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_set_self_role(ESP_NOW_ROLE_CONTROLLER);
esp_now_register_send_cb(OnDataSent);
// Register peer
esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_SLAVE, 1, NULL, 0);
}
void loop() {
if ((millis() - lastTime) > timerDelay) {
// Set values to send
myData.a0 = digitalRead(a0);
myData.a1 = digitalRead(a1);
myData.a2 = digitalRead(a2);
myData.a3 = digitalRead(a3);
myData.a4 = digitalRead(a4);
myData.a5 = digitalRead(a5);
myData.a6 = digitalRead(a6);
myData.a7 = digitalRead(a7);
// Send message via ESP-NOW
esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));
lastTime = millis();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment