prjbrook

Sender7. ESP-NOW

Jul 24th, 2022
885
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.72 KB | None | 0 0
  1. //Sender 2. Cut down delay from 3 to 1 sec. Tue Jul 19 15:47:45 NZST 2022
  2. //Sender3 counting and sending packet count.
  3. //Sender7 put in blink to see while outside.
  4. //Try #include <arduino-timer.h>. Lower pause to 500ms.No, promlems
  5. //Try unsigned long previousMillis = 0;        // will store last time LED was updated
  6.  
  7.  
  8. //const long interval = 1000;      
  9. /****************************************************************************************************************************************************
  10.  *  TITLE: ESP-NOW Getting Started Examples
  11.  *
  12.  *  By Frenoy Osburn
  13.  *  YouTube Video: https://youtu.be/_cNAsTB5JpM
  14.  ****************************************************************************************************************************************************/
  15.  
  16.  /********************************************************************************************************************
  17.   * Please make sure that you install the board support package for the ESP8266 boards.
  18.   * You will need to add the following URL to your Arduino preferences.
  19.   * Boards Manager URL: http://arduino.esp8266.com/stable/package_esp8266com_index.json
  20.  ********************************************************************************************************************/
  21.  
  22.  /********************************************************************************************************************
  23.  *  Board Settings:
  24.  *  Board: "WeMos D1 R1 or Mini"
  25.  *  Upload Speed: "921600"
  26.  *  CPU Frequency: "80MHz"
  27.  *  Flash Size: "4MB (FS:@MB OTA:~1019KB)"
  28.  *  Debug Port: "Disabled"
  29.  *  Debug Level: "None"
  30.  *  VTables: "Flash"
  31.  *  IwIP Variant: "v2 Lower Memory"
  32.  *  Exception: "Legacy (new can return nullptr)"
  33.  *  Erase Flash: "Only Sketch"
  34.  *  SSL Support: "All SSL ciphers (most compatible)"
  35.  *  COM Port: Depends *On Your System*
  36.  *********************************************************************************************************************/
  37.  #include<ESP8266WiFi.h>
  38. #include<espnow.h>
  39. //#include <arduino-timer.h> //might need this
  40.  
  41. #define MY_NAME         "CONTROLLER_NODE"
  42. #define MY_ROLE         ESP_NOW_ROLE_CONTROLLER         // set the role of this device: CONTROLLER, SLAVE, COMBO
  43. #define RECEIVER_ROLE   ESP_NOW_ROLE_SLAVE              // set the role of the receiver
  44. #define WIFI_CHANNEL    1
  45.  
  46. //uint8_t receiverAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};   // please update this with the MAC address of the receiver
  47. uint8_t receiverAddress[] = {0x98, 0xF4, 0xAB, 0xBF, 0xEC, 0xCC};   // Mac adress of Wemos Mini 2
  48.  
  49. struct __attribute__((packed)) dataPacket {
  50.   int sensor1;
  51.   int sensor2;
  52.   float sensor3;
  53. };
  54. int count=0;    //count the number of packets being sent
  55.  unsigned long previousMillis = 0;        // will store last time LED was updated
  56.  unsigned long time_ms;
  57.  float er=0, se=0 , elapsed=0;
  58.  float suc = 0;
  59.  int t =0;
  60.  
  61.  
  62. void transmissionComplete(uint8_t *receiver_mac, uint8_t transmissionStatus) {
  63.   if(transmissionStatus == 0) {
  64. //    Serial.println("Data sent successfully");
  65.     Serial.println(se++);   //number of successful attemps to send
  66.     Serial.println();
  67.    
  68.     //Blink the LED
  69.     t++; if(t==2) t=0;
  70.     Serial.println(t);
  71.     if (t==1) digitalWrite(LED_BUILTIN, HIGH);
  72.     if (t==0) digitalWrite(LED_BUILTIN, LOW);
  73.    
  74.   } else {
  75.     Serial.print(er++);
  76.     Serial.print(" Err code: ");
  77.    
  78.     Serial.println(transmissionStatus);
  79.   }
  80.   suc= float(se/(se+er));
  81.   Serial.println("se,er,suc ");
  82.   Serial.println(se);
  83.   Serial.println(er);
  84.   //Serial.print(suc);
  85.   Serial.println(suc);
  86. }
  87.  
  88. void setup() {
  89.   Serial.begin(115200);     // initialize serial port
  90.  
  91.   Serial.println();
  92.   Serial.println();
  93.   Serial.println();
  94.   Serial.print("Initializing...");
  95.   Serial.println(MY_NAME);
  96.   Serial.print("My MAC address is: ");
  97.   Serial.println(WiFi.macAddress());
  98.  
  99.   WiFi.mode(WIFI_STA);
  100.   WiFi.disconnect();        // we do not want to connect to a WiFi network
  101.  
  102.   if(esp_now_init() != 0) {
  103.     Serial.println("ESP-NOW initialization failed");
  104.     return;
  105.   }
  106.  
  107.   esp_now_set_self_role(MY_ROLE);  
  108.   esp_now_register_send_cb(transmissionComplete);   // this function will get called once all data is sent
  109.   esp_now_add_peer(receiverAddress, RECEIVER_ROLE, WIFI_CHANNEL, NULL, 0);
  110.  
  111.   Serial.println("Initialized.");
  112.    pinMode(LED_BUILTIN, OUTPUT); //added
  113. }
  114.  
  115. void loop() {
  116.   dataPacket packet;
  117.    
  118.   packet.sensor1 = count++; //124;
  119.   packet.sensor2 = 456;
  120.   packet.sensor3 = 2.141;
  121.  
  122. previousMillis = millis();
  123.   esp_now_send(receiverAddress, (uint8_t *) &packet, sizeof(packet));
  124. /*  time_ms = millis();
  125.   Serial.println(previousMillis);
  126.   Serial.println(time_ms);
  127.   elapsed= time_ms-previousMillis;
  128.   Serial.print("elapsed=");
  129.   Serial.println(elapsed); */
  130.   delay(200);
  131. }
Advertisement
Add Comment
Please, Sign In to add comment