/* Rui Santos Complete project details at https://RandomNerdTutorials.com/esp-now-one-to-many-esp8266-nodemcu/ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. */ #include #include // REPLACE WITH RECEIVER MAC Address uint8_t broadcastAddress1[] = {0xDC, 0x4F, 0x22, 0x6E, 0x35, 0x3A}; // Structure example to send data // Must match the receiver structure typedef struct test_struct { int red; int green; int blue; }; struct test_struct myMessage; const int analogInPin = 0; int sensorValue = 0; // value read from the pot int outputValue = 0; // Create a struct_message called test to store variables to be sent // test_struct test; unsigned long lastTime = 0; unsigned long timerDelay = 500; // send readings timer // Callback when data is sent void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) { char macStr[18]; Serial.print("Packet to:"); snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x", mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]); Serial.print(macStr); Serial.print(" send status: "); if (sendStatus == 0){ Serial.println("Delivery success"); } else{ Serial.println("Delivery fail"); } } void setup() { // Init Serial Monitor Serial.begin(115200); // Set device as a Wi-Fi Station WiFi.mode(WIFI_STA); WiFi.disconnect(); // Init ESP-NOW if (esp_now_init() != 0) { Serial.println("Error initializing ESP-NOW"); return; } esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER); // Once ESPNow is successfully Init, we will register for Send CB to // get the status of Trasnmitted packet esp_now_register_send_cb(OnDataSent); // Register peer esp_now_add_peer(broadcastAddress1, ESP_NOW_ROLE_SLAVE, 1, NULL, 0); } void loop() { if ((millis() - lastTime) > timerDelay) { // Set values to send // test.x = random(1, 50); // test.y = random(1, 50); sensorValue = analogRead(analogInPin); outputValue = map(sensorValue, 0, 1024, 0, 255); Serial.println(outputValue); myMessage.red = random(0, 254); myMessage.green = random(0, 254); myMessage.blue = outputValue;//random(0, 254); // Send message via ESP-NOW esp_now_send(0, (uint8_t *) &myMessage, sizeof(myMessage)); lastTime = millis(); } }