prjbrook

Sender8 ESP Now

Jul 26th, 2022
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. /* Send ESP now packets to receiver3 */
  2. //Sender 2. Cut down delay from 3 to 1 sec. Tue Jul 19 15:47:45 NZST 2022
  3. //Sender3 counting and sending packet count.
  4. //Sender7 put in blink to see while outside.Worked
  5. //Sender8. Added LDResistor loop to send real data on desk before trying outside. Wed Jul 27 13:23:39 NZST 2022
  6. //Try #include <arduino-timer.h>. Lower pause to 500ms.No, promlems
  7. //Try unsigned long previousMillis = 0; // will store last time LED was updated
  8.  
  9.  
  10. //const long interval = 1000;
  11.  
  12. #include<ESP8266WiFi.h>
  13. #include<espnow.h>
  14. //#include <arduino-timer.h> //might need this
  15.  
  16. #define MY_NAME "CONTROLLER_NODE"
  17. #define MY_ROLE ESP_NOW_ROLE_CONTROLLER // set the role of this device: CONTROLLER, SLAVE, COMBO
  18. #define RECEIVER_ROLE ESP_NOW_ROLE_SLAVE // set the role of the receiver
  19. #define WIFI_CHANNEL 1
  20.  
  21. //uint8_t receiverAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; // please update this with the MAC address of the receiver
  22. uint8_t receiverAddress[] = {0x98, 0xF4, 0xAB, 0xBF, 0xEC, 0xCC}; // Mac adress of Wemos Mini 2
  23.  
  24. struct __attribute__((packed)) dataPacket {
  25. int sensor1;
  26. int sensor2;
  27. float sensor3;
  28. };
  29. int count=0; //count the number of packets being sent
  30. unsigned long previousMillis = 0; // will store last time LED was updated
  31. unsigned long time_ms;
  32. float er=0, se=0 , elapsed=0;
  33. float suc = 0;
  34. int t =0;
  35.  
  36.  
  37. void transmissionComplete(uint8_t *receiver_mac, uint8_t transmissionStatus) {
  38. if(transmissionStatus == 0) {
  39. // Serial.println("Data sent successfully");
  40. Serial.println(se++); //number of successful attemps to send
  41. Serial.println();
  42.  
  43. //Blink the LED
  44. t++; if(t==2) t=0;
  45. Serial.println(t);
  46. if (t==1) digitalWrite(LED_BUILTIN, HIGH);
  47. if (t==0) digitalWrite(LED_BUILTIN, LOW);
  48.  
  49. } else {
  50. Serial.print(er++);
  51. Serial.print(" Err code: ");
  52.  
  53. Serial.println(transmissionStatus);
  54. }
  55. suc= float(se/(se+er));
  56. Serial.println("se,er,suc ");
  57. Serial.println(se);
  58. Serial.println(er);
  59. //Serial.print(suc);
  60. Serial.println(suc);
  61. }
  62.  
  63. void setup() {
  64. Serial.begin(115200); // initialize serial port
  65.  
  66. Serial.println();
  67. Serial.println();
  68. Serial.println();
  69. Serial.print("Initializing...");
  70. Serial.println(MY_NAME);
  71. Serial.print("My MAC address is: ");
  72. Serial.println(WiFi.macAddress());
  73.  
  74. WiFi.mode(WIFI_STA);
  75. WiFi.disconnect(); // we do not want to connect to a WiFi network
  76.  
  77. if(esp_now_init() != 0) {
  78. Serial.println("ESP-NOW initialization failed");
  79. return;
  80. }
  81.  
  82. esp_now_set_self_role(MY_ROLE);
  83. esp_now_register_send_cb(transmissionComplete); // this function will get called once all data is sent
  84. esp_now_add_peer(receiverAddress, RECEIVER_ROLE, WIFI_CHANNEL, NULL, 0);
  85.  
  86. Serial.println("Initialized.");
  87. pinMode(LED_BUILTIN, OUTPUT); //added
  88. }
  89.  
  90. void loop() {
  91. //read LDR. Tuck ADC reading 0..1024 into packet.sensor2
  92. int sensorValue = analogRead(A0); // read analog input pin 0. The LDR.
  93. Serial.print(sensorValue, DEC); // prints the value read
  94. Serial.print(" Mum 2\n"); // prints a space between the numbers
  95. delay(1000); // wait 100ms for next reading
  96.  
  97. dataPacket packet;
  98.  
  99. packet.sensor1 = count++; //124;
  100. packet.sensor2 = sensorValue; //456;
  101. packet.sensor3 = 2.141;
  102.  
  103. previousMillis = millis();
  104. esp_now_send(receiverAddress, (uint8_t *) &packet, sizeof(packet));
  105. /* time_ms = millis();
  106. Serial.println(previousMillis);
  107. Serial.println(time_ms);
  108. elapsed= time_ms-previousMillis;
  109. Serial.print("elapsed=");
  110. Serial.println(elapsed); */
  111. delay(500);
  112. }
Advertisement
Add Comment
Please, Sign In to add comment