prjbrook

Receiver3WithBlink.ino

Jul 24th, 2022
944
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.04 KB | None | 0 0
  1. //Receiver3WithBlink. Cutting out serial.prints
  2. /****************************************************************************************************************************************************
  3.  *  TITLE: ESP-NOW Getting Started Examples
  4.  *
  5.  *  By Frenoy Osburn
  6.  *  YouTube Video: https://youtu.be/_cNAsTB5JpM
  7.  ****************************************************************************************************************************************************/
  8.  
  9.  /********************************************************************************************************************
  10.   * Please make sure that you install the board support package for the ESP8266 boards.
  11.   * You will need to add the following URL to your Arduino preferences.
  12.   * Boards Manager URL: http://arduino.esp8266.com/stable/package_esp8266com_index.json
  13.  ********************************************************************************************************************/
  14.  
  15.  /********************************************************************************************************************
  16.  *  Board Settings:
  17.  *  Board: "WeMos D1 R1 or Mini"
  18.  *  Upload Speed: "921600"
  19.  *  CPU Frequency: "80MHz"
  20.  *  Flash Size: "4MB (FS:@MB OTA:~1019KB)"
  21.  *  Debug Port: "Disabled"
  22.  *  Debug Level: "None"
  23.  *  VTables: "Flash"
  24.  *  IwIP Variant: "v2 Lower Memory"
  25.  *  Exception: "Legacy (new can return nullptr)"
  26.  *  Erase Flash: "Only Sketch"
  27.  *  SSL Support: "All SSL ciphers (most compatible)"
  28.  *  COM Port: Depends *On Your System*
  29.  *********************************************************************************************************************/
  30.  #include<ESP8266WiFi.h>
  31. #include<espnow.h>
  32.  
  33. #define MY_NAME   "SLAVE_NODE"
  34.  
  35. struct __attribute__((packed)) dataPacket {
  36.   int sensor1;
  37.   int sensor2;
  38.   float sensor3;
  39. };
  40. int t=0;
  41. // pinMode(LED_BUILTIN, OUTPUT);
  42. void dataReceived(uint8_t *senderMac, uint8_t *data, uint8_t dataLength) {
  43.   char macStr[18];
  44.   dataPacket packet;  
  45.   //int t=0;
  46.  
  47.   snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x", senderMac[0], senderMac[1], senderMac[2], senderMac[3], senderMac[4], senderMac[5]);
  48.  
  49.   Serial.println();
  50.   //Serial.print("Received data from: ");
  51.   Serial.println(macStr);
  52.    /*  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  53.      delay(100);                       // wait for a second
  54.      digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  55.      delay(100);                       // wait for a second */
  56.      t++; if(t==2) t=0;
  57.     // Serial.print("--------t=");
  58.      Serial.println(t);
  59.      //Serial.print(LED_BUILTIN);
  60.      
  61. if (t==1) digitalWrite(LED_BUILTIN, HIGH);
  62. if (t==0) digitalWrite(LED_BUILTIN, LOW);
  63.      
  64.   /*
  65. }
  66.  
  67.  
  68. }
  69.  
  70. // the loop function runs over and over again forever
  71.  
  72.   digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  73.   delay(100);                       // wait for a second
  74.   digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  75.   delay(1000);                       // wait for a second
  76. }
  77.   */
  78.   memcpy(&packet, data, sizeof(packet));
  79.  
  80.   //Serial.print("sensor1: ");
  81.   Serial.println(packet.sensor1);
  82.   //Serial.print("sensor2: ");
  83.   Serial.println(packet.sensor2);
  84.   Serial.print("sensor3: ");
  85.   Serial.println(packet.sensor3);
  86. }
  87.  
  88. void setup() {
  89.  // int t =0; //toggle for led
  90.   pinMode(LED_BUILTIN, OUTPUT); //added
  91.   Serial.begin(115200);     // initialize serial port
  92.  
  93.   Serial.println();
  94.   Serial.println();
  95.   Serial.println();
  96.   Serial.print("Initializing...");
  97.   Serial.println(MY_NAME);
  98.   Serial.print("My MAC address is: ");
  99.   Serial.println(WiFi.macAddress());
  100.  
  101.   WiFi.mode(WIFI_STA);
  102.   WiFi.disconnect();        // we do not want to connect to a WiFi network
  103.  
  104.   if(esp_now_init() != 0) {
  105.     Serial.println("ESP-NOW initialization failed");
  106.     return;
  107.   }
  108.  
  109.   esp_now_register_recv_cb(dataReceived);   // this function will get called once all data is sent
  110.  
  111.   Serial.println("Initialized.");
  112. }
  113.  
  114. void loop() {
  115.  
  116. }
Advertisement
Add Comment
Please, Sign In to add comment