![]() |
Forum Index : Microcontroller and PC projects : playing with ESPNOW
Author | Message | ||||
Quazee137![]() Guru ![]() Joined: 07/08/2016 Location: United StatesPosts: 593 |
Better to start here. ------------------------- get mac ------------------- #include "ESP8266WiFi.h" void setup(){ Serial.begin(115200); Serial.println(); Serial.println(WiFi.macAddress()); } void loop(){} --------------------------- My sender code ---------------------- #include <ESP8266WiFi.h> #include <espnow.h> const int LED = 2; // playing with these MAC's // A. 80:7D:3A:75:CC:C3 // B. 80:7D:3A:75:D4:BD // C. 80:7D:3A:75:D4:C4 // D. 80:7D:3A:75:D4:C5 // E. A4:CF:12:C8:D9:F1 // MAC address to broadcast data to uint8_t broadcastAddress[] = {0xA4, 0xCF, 0x12, 0xC8, 0xD9, 0xF1}; // Message to send char msg[] = "pump on"; // Callback function to handle data send status void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) { // Serial.print("\r\nDelivery Status: "); // Serial.println(sendStatus == 0 ? "Delivered Successfully" : "Delivery Fail"); if (sendStatus == 0){ Serial.println("Delivery success"); digitalWrite(2, HIGH); digitalWrite(1, LOW); } else{ Serial.println("Delivery fail"); digitalWrite(2, LOW); digitalWrite(1, HIGH); } } void setup() { Serial.begin(115200); digitalWrite(0, HIGH); digitalWrite(2, HIGH); pinMode(0, OUTPUT); pinMode(2, OUTPUT); // Set WiFi mode to Station mode WiFi.mode(WIFI_STA); // Initialize ESP-NOW if (esp_now_init() != 0) { Serial.println("Error initializing ESP-NOW"); return; } // Set the role of this device as a controller esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER); // Register callback for data send status esp_now_register_send_cb(OnDataSent); // Add a peer (slave) with the specified broadcast address esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_SLAVE, 1, NULL, 0); } void loop() { // Send the message to the specified broadcast address esp_now_send(broadcastAddress, (uint8_t *) &msg, sizeof(msg)); // Delay for 4 seconds before sending the next message delay(4000); } ----------------------- My receiver code ---------------------------- #include <ESP8266WiFi.h> #include <espnow.h> const int RELAY = 0; const int LED = 2; static bool toggle = false; unsigned long lastPacketTime = 0; const int PACKET_TIMEOUT = 5000; // milliseconds (e.g., 5 seconds) // Structure to hold the received message typedef struct struct_message { char a[32]; } struct_message; // Create an instance of the struct_message struct_message myData; // Callback function to handle received data void OnDataRecv(uint8_t * mac, uint8_t *incomingData, uint8_t len) { // Copy the incoming data to the myData structure memcpy(&myData, incomingData, sizeof(myData)); // Print the received message Serial.println(myData.a); if (strcmp(myData.a, "pump on") == 0) { // Execute code if "myData.a" matches "target_string" digitalWrite(RELAY, LOW); // Turn RELAY on // Serial.println("RELAY on"); digitalWrite(LED,LOW); // LED on solid lastPacketTime = millis(); // Update last packet time } } void setup() { //Initialize the serial monitor Serial.begin(115200); // Make RELAY high digitalWrite(0, HIGH); digitalWrite(2, HIGH); pinMode(0, OUTPUT); pinMode(2, OUTPUT); // Set WiFi mode to Station mode WiFi.mode(WIFI_STA); // Initialize ESP-NOW if (esp_now_init() != 0) { Serial.println("Error initializing ESP-NOW"); return; } // Set the role of this device as a slave esp_now_set_self_role(ESP_NOW_ROLE_SLAVE); // Register callback for received data esp_now_register_recv_cb(OnDataRecv); } void loop(){ if (millis() - lastPacketTime > PACKET_TIMEOUT) { // Signal lost connection Serial.println("Lost signal/ Controller off so turning pump off"); digitalWrite(RELAY, HIGH); // Turn RELAY off digitalWrite(LED,toggle); // LED blinking toggle = !toggle; delay(4000); //later I should do this without delay } } ------------------- where I got my information ----------------- electronicwings.com randomnerdtutorials.com Have FUN good to learn new things. Quazee137 |
||||
PhenixRising Guru ![]() Joined: 07/11/2023 Location: United KingdomPosts: 1361 |
Oh cool stuff ![]() I haven't touched it yet and it's hard to resist but I hope to soon. Randomnerd got me going with ESP32 in a matter of minutes ![]() Using ESP32 only as a BT device for the PicoMites right now but I want to use ESP-Now and possibly the WhatsApp and Messenger messaging. Perfect slave device. Do you have experience with ESPHome? I have only glanced at it but it looks pretty friendly. |
||||
matherp Guru ![]() Joined: 11/12/2012 Location: United KingdomPosts: 10246 |
Spent half a day playing with it as integrated into home-assistant and failed miserably to make any sense of it |
||||
PhenixRising Guru ![]() Joined: 07/11/2023 Location: United KingdomPosts: 1361 |
That means I have no chance ![]() |
||||
PhenixRising Guru ![]() Joined: 07/11/2023 Location: United KingdomPosts: 1361 |
![]() I couldn't resist at this price and they have lots of sample code on their site. |
||||
Quazee137![]() Guru ![]() Joined: 07/08/2016 Location: United StatesPosts: 593 |
I spent that last month reading on using ESP modules and was happy to find about ESPNOW as I do not want my devices talk to or through the internet. Many of the youtube videos controlling relays want you to do it through apps and or cloud servers. I want a closed environment. So many things go through connections we do control or see what data is being collected. So many WiFi IOT go through China servers. I am playing with WiFi networks that are isolated. I just don't see the need for my phone to connect to cloud servers just to turn on/off lights. ESP-NOW has the functions to have my things only talk to my things. Working on how it works in "one to one", "one to many", "many to one" and having them just "talk to each other". As matherp said not easy to understand the whole of it. So for now I'll be crawling till I can walk. For me I had to strip the code I found down to just what I need for now. Because there is a lot to learn and when those that know go max use makes it hard to grip. Now time to make a few pcbs with one, two and four 30Amp relays for field testing. Split on relays with snubbers or SSRs. FUN is FUN hope every one is having some. Quazee137 Odd question When internet drops out can you still ask --fill in blank -- to do as request or do they not hear it? Edited 2024-11-29 20:47 by Quazee137 |
||||
JohnS Guru ![]() Joined: 18/11/2011 Location: United KingdomPosts: 4038 |
Not sure I understand. Depends what you mean by the internet "drops out", e.g. briefly? how briefly? If you mean will the request just be forever retried, not usually. (And depends on protocol: TCP usually retries a few times, UDP doesn't, for example.) Give more detail please. John |
||||
Volhout Guru ![]() Joined: 05/03/2018 Location: NetherlandsPosts: 5059 |
Thanks Quazee137 Volhout PicomiteVGA PETSCII ROBOTS |
||||
Quazee137![]() Guru ![]() Joined: 07/08/2016 Location: United StatesPosts: 593 |
JohnS Smart-home systems you talk to like Siri, Cortana, Alexa and others. 1. Do they need internet access to work? 2. Is there a non internet Smart-home that is truly stand alone? After having the throttle on my E-Trike lose ground connection making me crash. Yes when it loses ground you end up full throttle. In my case from 5MPH to 48MPH. Just in front of a speed bump sent me airborne for over 100 feet. Broke my right shoulder two places along with two ribs and my right hand. Looked online at a lot of senior electric bike clubs and found that abrupt acceleration was an on going problem. Working on a fix. The throttle signal feeds a capacitor based circuit changes the slope of acceleration. Idea is to allow a slower rise in speed. I have been thinking of doing a Smart-home system. Having it turn on lights and such. Monitor me and calling my son if I do not respond to it in time. With time on my hand LOL looking to keep my mind busy at useful things. Quazee137 Edited 2024-11-29 23:56 by Quazee137 |
||||
Mixtel90![]() Guru ![]() Joined: 05/10/2019 Location: United KingdomPosts: 7871 |
The nRF24L01 is intended for closed local networks (one master, 6 nodes). In fact it wouldn't connect to the internet even if you threatened it with a rabid PDP-11. Mick Zilog Inside! nascom.info for Nascom & Gemini Preliminary MMBasic docs & my PCB designs |
||||
lizby Guru ![]() Joined: 17/05/2016 Location: United StatesPosts: 3360 |
. . . I just don't see the need for my phone to connect to cloud servers just to turn on/off lights. Absolutely. Thanks for boiling this down--you're right--it can be hard to see the essence when looking at a program where someone has included all the bells and whistles (however useful you may ultimately find those bells and whistles). Please keep us posted as you progress. My particular annoyance is with the pan/tilt cameras which require you to use cloud services. I'm happy with fixed cameras on openWrt, Raspberry Pi, and ESP32-Cam devices, but pan and tilt is not as easy with those. PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed |
||||
JohnS Guru ![]() Joined: 18/11/2011 Location: United KingdomPosts: 4038 |
1. Do they need internet access to work? 2. Is there a non internet Smart-home that is truly stand alone? I expect they do need one but easy to test any you already have (turn off router/etc). I believe so far the remote (google/apple/...) system needs to get your enquiry (voice etc via data packets) to figure it out and do whatever it decides to do. Sorry to hear about your nightmare with the bike!! John |
||||
PhenixRising Guru ![]() Joined: 07/11/2023 Location: United KingdomPosts: 1361 |
Quazee137 Sounds like you need a lanyard switch: ![]() Have the strap around your wrist. Insert the plug to enable power. Pull your hand away, the plug pops out and power is removed. |
||||
Volhout Guru ![]() Joined: 05/03/2018 Location: NetherlandsPosts: 5059 |
I am done, The Arduino 1.8.19 in my PC (ubuntu snap package, sandboxed) does not accept ESP8266 libraries (python mismatch). The Ubuntu Arduino 2.15 package cannot read the json file with ESP8266. The Arduino install for 2.23 from their website crashes on the serial monitor (it did compile and upload) and has removed itself autonomously from my PC (leaving a lot of garbage). Arduino always worked for me, but not anymore apparently.... I will install the 1.8 back, that always worked. But ESP8266 will have to wait until I find a solution. Volhout PicomiteVGA PETSCII ROBOTS |
||||
matherp Guru ![]() Joined: 11/12/2012 Location: United KingdomPosts: 10246 |
If you think Arduino is hard with the ESPs. Try Visual Studio Code. Had to run it is a virtual machine to avoid b...ring my Pico dev environment but so confusing, so many bits to install and no docs that make any sense. Edited 2024-12-02 00:26 by matherp |
||||
Mixtel90![]() Guru ![]() Joined: 05/10/2019 Location: United KingdomPosts: 7871 |
I keep telling you, the Arduino IDE is a curse brought up from the depths of Dante's imagination. ;) The best thing about the Arduino is the wonky PCB shape of the UNO, with an idiotic pin and mounting hole layout that are about as far from 0.1" matrix as you can get. :) Something that could have so easily been right. Mick Zilog Inside! nascom.info for Nascom & Gemini Preliminary MMBasic docs & my PCB designs |
||||
stanleyella![]() Guru ![]() Joined: 25/06/2022 Location: United KingdomPosts: 2542 |
The best thing about the Arduino is the wonky PCB shape of the UNO, with an idiotic pin and mounting hole layout that are about as far from 0.1" matrix as you can get. :) Something that could have so easily been right. the nano 328 is vero friendly |
||||
Mixtel90![]() Guru ![]() Joined: 05/10/2019 Location: United KingdomPosts: 7871 |
Maybe, but the IDE isn't. lol Not on tiny fanless computers with not a lot of RAM and a very small C: drive anyway. Mick Zilog Inside! nascom.info for Nascom & Gemini Preliminary MMBasic docs & my PCB designs |
||||
PhenixRising Guru ![]() Joined: 07/11/2023 Location: United KingdomPosts: 1361 |
OK I'll book an appointment with a shrink because I'm actually having fun with the latest IDE....and I have next to no patience ![]() |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |