Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Receiver3WithBlink. Cutting out serial.prints
- /****************************************************************************************************************************************************
- * TITLE: ESP-NOW Getting Started Examples
- *
- * By Frenoy Osburn
- * YouTube Video: https://youtu.be/_cNAsTB5JpM
- ****************************************************************************************************************************************************/
- /********************************************************************************************************************
- * Please make sure that you install the board support package for the ESP8266 boards.
- * You will need to add the following URL to your Arduino preferences.
- * Boards Manager URL: http://arduino.esp8266.com/stable/package_esp8266com_index.json
- ********************************************************************************************************************/
- /********************************************************************************************************************
- * Board Settings:
- * Board: "WeMos D1 R1 or Mini"
- * Upload Speed: "921600"
- * CPU Frequency: "80MHz"
- * Flash Size: "4MB (FS:@MB OTA:~1019KB)"
- * Debug Port: "Disabled"
- * Debug Level: "None"
- * VTables: "Flash"
- * IwIP Variant: "v2 Lower Memory"
- * Exception: "Legacy (new can return nullptr)"
- * Erase Flash: "Only Sketch"
- * SSL Support: "All SSL ciphers (most compatible)"
- * COM Port: Depends *On Your System*
- *********************************************************************************************************************/
- #include<ESP8266WiFi.h>
- #include<espnow.h>
- #define MY_NAME "SLAVE_NODE"
- struct __attribute__((packed)) dataPacket {
- int sensor1;
- int sensor2;
- float sensor3;
- };
- int t=0;
- // pinMode(LED_BUILTIN, OUTPUT);
- void dataReceived(uint8_t *senderMac, uint8_t *data, uint8_t dataLength) {
- char macStr[18];
- dataPacket packet;
- //int t=0;
- snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x", senderMac[0], senderMac[1], senderMac[2], senderMac[3], senderMac[4], senderMac[5]);
- Serial.println();
- //Serial.print("Received data from: ");
- Serial.println(macStr);
- /* digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
- delay(100); // wait for a second
- digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
- delay(100); // wait for a second */
- t++; if(t==2) t=0;
- // Serial.print("--------t=");
- Serial.println(t);
- //Serial.print(LED_BUILTIN);
- if (t==1) digitalWrite(LED_BUILTIN, HIGH);
- if (t==0) digitalWrite(LED_BUILTIN, LOW);
- /*
- }
- }
- // the loop function runs over and over again forever
- digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
- delay(100); // wait for a second
- digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
- delay(1000); // wait for a second
- }
- */
- memcpy(&packet, data, sizeof(packet));
- //Serial.print("sensor1: ");
- Serial.println(packet.sensor1);
- //Serial.print("sensor2: ");
- Serial.println(packet.sensor2);
- Serial.print("sensor3: ");
- Serial.println(packet.sensor3);
- }
- void setup() {
- // int t =0; //toggle for led
- pinMode(LED_BUILTIN, OUTPUT); //added
- Serial.begin(115200); // initialize serial port
- Serial.println();
- Serial.println();
- Serial.println();
- Serial.print("Initializing...");
- Serial.println(MY_NAME);
- Serial.print("My MAC address is: ");
- Serial.println(WiFi.macAddress());
- WiFi.mode(WIFI_STA);
- WiFi.disconnect(); // we do not want to connect to a WiFi network
- if(esp_now_init() != 0) {
- Serial.println("ESP-NOW initialization failed");
- return;
- }
- esp_now_register_recv_cb(dataReceived); // this function will get called once all data is sent
- Serial.println("Initialized.");
- }
- void loop() {
- }
Advertisement
Add Comment
Please, Sign In to add comment