Advertisement
amir_eshaqy

esp8266_prtocol_espnow_analog.ino

Feb 4th, 2022
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.42 KB | None | 0 0
  1. //////tanzimat tempratare////////
  2.  
  3. #include <OneWire.h>
  4. #include <DallasTemperature.h>
  5.  
  6. // Data wire is plugged into digital pin 2 on the Arduino
  7. #define ONE_WIRE_BUS 5
  8.  
  9. // Setup a oneWire instance to communicate with any OneWire device
  10. OneWire oneWire(ONE_WIRE_BUS);  
  11.  
  12. // Pass oneWire reference to DallasTemperature library
  13. DallasTemperature sensors(&oneWire);
  14.  
  15. ///////////////////////////////////////////////////////
  16. ////////millis////////////////////////////////////////////
  17.  
  18. unsigned long int t1=0;
  19. int interval=5000;
  20. int x=0;
  21. /////////////////////
  22.  
  23. #include <ESP8266WiFi.h>
  24. #include <espnow.h>
  25.  
  26. uint8_t broadcastAddress[] = {0x94, 0xB9, 0x7E, 0xD9, 0xC2, 0xC0};    //MAC address of the receiver. In my case it is ESP32
  27.  
  28.  
  29. const int ledPin = 4;    // i.e. D2 pin
  30. const int ledPin2 =12;/////////2
  31.  
  32.  
  33. int receivedButtonValue = 0;
  34. int receivedButtonValue2 = 0;///////2
  35. bool isDataReceived = false;
  36. bool isDataReceived2 = false;/////////2
  37.  
  38.  
  39. typedef struct struct_message {    // Structure to send/receive data. Must match the receiver/sender structure
  40.   //char charPlaceHolder[32];
  41.   int intPlaceHolder;
  42.   int intPlaceHolder2;///////////2
  43.   //float floatPlaceHolder;
  44.   //String stringPlaceHolder;
  45.   //bool boolPlaceHolder;
  46. } struct_message;
  47.  
  48. struct_message myIncomingData, myOutgoingData;
  49.  
  50. // Callback when data is sent
  51. void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
  52.   Serial.print(sendStatus == 0 ? "Packet sent successfully with value: " : "Packet sending failed with values: ");
  53.   Serial.println(sensors.getTempCByIndex(0));
  54. }
  55.  
  56.  
  57.  
  58. // Callback function that will be executed when data is received
  59. void OnDataRecv(uint8_t * mac, uint8_t *incomingData, uint8_t len) {
  60.   memcpy(&myIncomingData, incomingData, sizeof(myIncomingData));
  61.   receivedButtonValue = myIncomingData.intPlaceHolder;    //Here I recieve the button press value [0, 1] from ESP32
  62.   isDataReceived = true;
  63.  
  64.   ////////////2
  65.    receivedButtonValue2 = myIncomingData.intPlaceHolder2;    //Here I recieve the button2 press value [0, 1] from ESP32
  66.     isDataReceived2 = true;
  67.    
  68. }
  69.  
  70.  
  71. void dama_1(void);
  72. void setup() {
  73.   Serial.begin(115200);
  74.   pinMode(ledPin, OUTPUT);
  75.   pinMode(ledPin2, OUTPUT);
  76.   WiFi.mode(WIFI_STA);
  77.   WiFi.disconnect();
  78.  
  79.   if (esp_now_init() != 0) {    // Init ESP-NOW
  80.     Serial.println("Error initializing ESP-NOW");
  81.     return;
  82.   }
  83.  
  84.   esp_now_set_self_role(ESP_NOW_ROLE_COMBO);    // setting as both sender and receiver. [ESP_NOW_ROLE_CONTROLLER, ESP_NOW_ROLE_SLAVE, ESP_NOW_ROLE_COMBO, ESP_NOW_ROLE_MAX]
  85.   esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_COMBO, 1, NULL, 0);    // Register peer
  86.  
  87.   esp_now_register_send_cb(OnDataSent);    // register the callback on datasent
  88.   esp_now_register_recv_cb(OnDataRecv);    // register the callback on receive
  89.    
  90.  
  91. /////////////tempratare///////////////////////////////
  92. sensors.begin();
  93. /////////////////////////////////////////////////
  94.  
  95. }
  96. void loop() {
  97.   if(millis() > t1+ interval){
  98.   t1=millis();
  99.   dama_1();///////ersal  mqdar dama har 5 saneye
  100.  }
  101.  
  102.   if (isDataReceived) {
  103.     isDataReceived = false;
  104.     Serial.print("[RECEIVED]>> ");
  105.     Serial.println(receivedButtonValue);
  106.     if(receivedButtonValue == 1) {
  107.       digitalWrite(ledPin, HIGH);
  108.     } else {
  109.      
  110.      digitalWrite(ledPin, LOW);
  111.     }
  112.   }
  113.   ///////////2
  114.   if (isDataReceived2) {
  115.     isDataReceived2 = false;
  116.     Serial.print("[RECEIVED2]>> ");
  117.     Serial.println(receivedButtonValue2);
  118.     if(receivedButtonValue2 == 1) {
  119.       digitalWrite(ledPin2, HIGH);
  120.     } else {  
  121.      
  122.       digitalWrite(ledPin2, LOW);
  123.     }
  124.   }
  125.   delay(40);
  126.  
  127. }
  128. //sensor void
  129. void dama_1() {
  130. // Send the command to get temperatures
  131.   sensors.requestTemperatures();
  132.  
  133.   //print the temperature in Celsius
  134.   Serial.print("Temperature: ");
  135.   Serial.print(sensors.getTempCByIndex(0));
  136.   Serial.print((char)176);//shows degrees character
  137.   Serial.print("C  |  ");
  138.    
  139.   //print the temperature in Fahrenheit
  140.   Serial.print((sensors.getTempCByIndex(0) * 9.0) / 5.0 + 32.0);
  141.   Serial.print((char)176);//shows degrees character
  142.   Serial.println("F");
  143.  
  144.   //کد برای ارسال مقدار با  پروتو کل ESPNOW
  145.       myOutgoingData.intPlaceHolder = sensors.getTempCByIndex(0);    // we MUST send the pot value in the range [0, 1023] because this has been configured at receiving ESP32
  146.     esp_now_send(broadcastAddress, (uint8_t *) &myOutgoingData, sizeof(myOutgoingData));
  147.  
  148.    
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement