Advertisement
amir_eshaqy

esp32_prtocol_espnow_switch.ino

Feb 4th, 2022
532
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.75 KB | None | 0 0
  1. #include <esp_now.h>
  2. #include <WiFi.h>
  3. #define LEDC_CHANNEL_0 0
  4. #define LEDC_TIMER_13_BIT 13
  5. #define LEDC_BASE_FREQ 5000
  6.  
  7. uint8_t broadcastAddress[] = {0xE8, 0x68, 0xE7, 0x80, 0x5E, 0x11};    // MAC address of the receiver. In my case it is ESP8266
  8.  
  9. const int ledPin = 16;
  10. const int buttonPin = 21;
  11. const int buttonPin2 = 22;////////2
  12.  
  13.  
  14. int currButtonVal = 0, prevButtonVal = 0;
  15. int currButtonVal2 = 0, prevButtonVal2 = 0;///////////////////2
  16. int brightness = 0;
  17. bool isDataReceived = false;
  18. int receivedPotValue = 0;
  19.  
  20. typedef struct struct_message {    // Structure to send/receive data. Must match the receiver/sender structure
  21.   //char charPlaceHolder[32];
  22.   int intPlaceHolder;
  23.   int intPlaceHolder2;//////////////////2
  24.   //float floatPlaceHolder;
  25.   //String stringPlaceHolder;
  26.   //bool boolPlaceHolder;
  27. } struct_message;
  28.  
  29. struct_message myOutGoingData, myIncomingData;
  30.  
  31. // Callback when data is sent
  32. void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
  33.   Serial.print(status == ESP_NOW_SEND_SUCCESS ? "Packet sent successfully with value: " : "Packet sending failed with values: ");
  34.   Serial.println(currButtonVal);
  35.     ////////////////2
  36.     Serial.print(status == ESP_NOW_SEND_SUCCESS ? "Packet sent successfully with value2: " : "Packet sending failed with values2: ");
  37.     Serial.println(currButtonVal2);
  38. }
  39.  
  40.  
  41.  
  42. // Callback when data is received
  43. void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
  44.   memcpy(&myIncomingData, incomingData, sizeof(myIncomingData));    // copying the incoming data into our structure
  45.   receivedPotValue = myIncomingData.intPlaceHolder;    // Here I recieve potentiometer value from ESP8266 in the range [0, 1023]
  46.   isDataReceived = true;
  47. }
  48.  
  49. // Arduino like analogWrite
  50. // value has to be between 0 and valueMax
  51. void IMHESP_analogWrite(uint8_t channel, uint32_t value, uint32_t valueMax = 255) {
  52.   uint32_t duty = (8191 / valueMax) * min(value, valueMax);    // calculate duty, 8191 from 2 ^ 13 - 1
  53.   ledcWrite(channel, duty);    // write duty to LEDC
  54. }
  55.  
  56. void setup() {
  57.   Serial.begin(115200);
  58.   pinMode(buttonPin, INPUT);
  59.   pinMode(buttonPin2, INPUT);
  60.   pinMode(ledPin, OUTPUT);
  61.   WiFi.mode(WIFI_STA);
  62.   WiFi.disconnect();
  63.   if (esp_now_init() != ESP_OK) {
  64.     Serial.println("Error initializing ESP-NOW");
  65.     return;
  66.   }
  67.  
  68.   esp_now_register_send_cb(OnDataSent);    // register the ondatasent callback
  69.   //esp_now_register_send_cb(OnDataSent2); ////////////2
  70.  
  71.   // Register peer
  72.   esp_now_peer_info_t peerInfo;
  73.   memcpy(peerInfo.peer_addr, broadcastAddress, 6);
  74.   peerInfo.channel = 0;
  75.   peerInfo.encrypt = false;
  76.  
  77.   // Add peer
  78.   if (esp_now_add_peer(&peerInfo) != ESP_OK){
  79.     Serial.println("Failed to add peer");
  80.     return;
  81.   }
  82.  
  83.   esp_now_register_recv_cb(OnDataRecv);    // Register for a callback function that will be called when data is received
  84.  
  85.   // Setup timer and attach timer to a led pin
  86.   ledcSetup(LEDC_CHANNEL_0, LEDC_BASE_FREQ, LEDC_TIMER_13_BIT);
  87.   ledcAttachPin(ledPin, LEDC_CHANNEL_0);
  88. }
  89.  
  90. void loop() {
  91.   currButtonVal = digitalRead(buttonPin);
  92.   if(currButtonVal != prevButtonVal) {
  93.     prevButtonVal = currButtonVal;
  94.     myOutGoingData.intPlaceHolder = currButtonVal;
  95.     esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &myOutGoingData, sizeof(myOutGoingData));
  96.   }
  97.   currButtonVal2 = digitalRead(buttonPin2);
  98.   if(currButtonVal2 != prevButtonVal2) {
  99.     prevButtonVal2 = currButtonVal2;
  100.     myOutGoingData.intPlaceHolder2 = currButtonVal2;
  101.     esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &myOutGoingData, sizeof(myOutGoingData));
  102.   }
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.   if(isDataReceived) {
  111.     isDataReceived = false;
  112.     Serial.print("[RECEIVED]>> ");
  113.     Serial.println(receivedPotValue);  }
  114.   delay(40);
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement