Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Send ESP now packets to receiver3 */
- //Sender 2. Cut down delay from 3 to 1 sec. Tue Jul 19 15:47:45 NZST 2022
- //Sender3 counting and sending packet count.
- //Sender7 put in blink to see while outside.Worked
- //Sender8. Added LDResistor loop to send real data on desk before trying outside. Wed Jul 27 13:23:39 NZST 2022
- //Try #include <arduino-timer.h>. Lower pause to 500ms.No, promlems
- //Try unsigned long previousMillis = 0; // will store last time LED was updated
- //const long interval = 1000;
- #include<ESP8266WiFi.h>
- #include<espnow.h>
- //#include <arduino-timer.h> //might need this
- #define MY_NAME "CONTROLLER_NODE"
- #define MY_ROLE ESP_NOW_ROLE_CONTROLLER // set the role of this device: CONTROLLER, SLAVE, COMBO
- #define RECEIVER_ROLE ESP_NOW_ROLE_SLAVE // set the role of the receiver
- #define WIFI_CHANNEL 1
- //uint8_t receiverAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; // please update this with the MAC address of the receiver
- uint8_t receiverAddress[] = {0x98, 0xF4, 0xAB, 0xBF, 0xEC, 0xCC}; // Mac adress of Wemos Mini 2
- struct __attribute__((packed)) dataPacket {
- int sensor1;
- int sensor2;
- float sensor3;
- };
- int count=0; //count the number of packets being sent
- unsigned long previousMillis = 0; // will store last time LED was updated
- unsigned long time_ms;
- float er=0, se=0 , elapsed=0;
- float suc = 0;
- int t =0;
- void transmissionComplete(uint8_t *receiver_mac, uint8_t transmissionStatus) {
- if(transmissionStatus == 0) {
- // Serial.println("Data sent successfully");
- Serial.println(se++); //number of successful attemps to send
- Serial.println();
- //Blink the LED
- t++; if(t==2) t=0;
- Serial.println(t);
- if (t==1) digitalWrite(LED_BUILTIN, HIGH);
- if (t==0) digitalWrite(LED_BUILTIN, LOW);
- } else {
- Serial.print(er++);
- Serial.print(" Err code: ");
- Serial.println(transmissionStatus);
- }
- suc= float(se/(se+er));
- Serial.println("se,er,suc ");
- Serial.println(se);
- Serial.println(er);
- //Serial.print(suc);
- Serial.println(suc);
- }
- void setup() {
- 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_set_self_role(MY_ROLE);
- esp_now_register_send_cb(transmissionComplete); // this function will get called once all data is sent
- esp_now_add_peer(receiverAddress, RECEIVER_ROLE, WIFI_CHANNEL, NULL, 0);
- Serial.println("Initialized.");
- pinMode(LED_BUILTIN, OUTPUT); //added
- }
- void loop() {
- //read LDR. Tuck ADC reading 0..1024 into packet.sensor2
- int sensorValue = analogRead(A0); // read analog input pin 0. The LDR.
- Serial.print(sensorValue, DEC); // prints the value read
- Serial.print(" Mum 2\n"); // prints a space between the numbers
- delay(1000); // wait 100ms for next reading
- dataPacket packet;
- packet.sensor1 = count++; //124;
- packet.sensor2 = sensorValue; //456;
- packet.sensor3 = 2.141;
- previousMillis = millis();
- esp_now_send(receiverAddress, (uint8_t *) &packet, sizeof(packet));
- /* time_ms = millis();
- Serial.println(previousMillis);
- Serial.println(time_ms);
- elapsed= time_ms-previousMillis;
- Serial.print("elapsed=");
- Serial.println(elapsed); */
- delay(500);
- }
Advertisement
Add Comment
Please, Sign In to add comment