Advertisement
amir_eshaqy

blynk_iot_cloud_v2.ino

Feb 4th, 2022
457
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.12 KB | None | 0 0
  1. //////tanzimat tempratare////////
  2.  
  3. #include <OneWire.h>
  4. #include <DallasTemperature.h>
  5. #define ONE_WIRE_BUS 23
  6. OneWire oneWire(ONE_WIRE_BUS);
  7. DallasTemperature sensors(&oneWire);
  8.  
  9.  
  10. ///////////////////////////////
  11. //////////wifi//////////////////
  12. #include <WiFi.h>
  13.  
  14. ////////////////////////////////
  15. //////////ESP NOW//////////////
  16. #include <esp_now.h>
  17.  
  18. #define LEDC_CHANNEL_0 0
  19. #define LEDC_TIMER_13_BIT 13
  20. #define LEDC_BASE_FREQ 5000
  21. ///////////////data////////////////
  22. uint8_t broadcastAddress[] = {0xE8, 0x68, 0xE7, 0x80, 0x5E, 0x11};    // MAC address of the receiver. In my case it is ESP8266
  23. #define mahtabi1 21
  24. #define mahtabi2 22
  25.  
  26. const int ledPin = 16;
  27.  int buttonPin;//suich esp now
  28.  
  29. int currButtonVal = 0, prevButtonVal = 0;
  30. int brightness = 0;
  31. bool isDataReceived = false;
  32. int receivedPotValue = 0;
  33.  
  34. typedef struct struct_message {    // Structure to send/receive data. Must match the receiver/sender structure
  35.   //char charPlaceHolder[32];
  36.   int intPlaceHolder;
  37.   //float floatPlaceHolder;
  38.   //String stringPlaceHolder;
  39.   //bool boolPlaceHolder;
  40. } struct_message;
  41.  
  42. struct_message myOutGoingData, myIncomingData;
  43.  
  44. /////////////////////////
  45.  
  46. // Fill-in information from your Blynk Template here
  47.  
  48. #define BLYNK_TEMPLATE_ID "TMPLADtP2oUc"
  49. #define BLYNK_DEVICE_NAME "SZA room blynk"
  50.  
  51. #define BLYNK_FIRMWARE_VERSION        "0.1.0"
  52.  
  53. #define BLYNK_PRINT Serial
  54. #define BLYNK_DEBUG
  55.  
  56. //#define APP_DEBUG
  57.  
  58. // Uncomment your board, or configure a custom board in Settings.h
  59. //#define USE_WROVER_BOARD
  60. //#define USE_TTGO_T7
  61.  
  62.  
  63. #include "BlynkEdgent.h"
  64. ////////////////////////////////////////
  65. BLYNK_WRITE(V0){
  66.  
  67.   int pinvalue=param.asInt();
  68.   digitalWrite(mahtabi1,pinvalue);
  69.  
  70.   }
  71. ///////////////////////////////////////  
  72. BLYNK_WRITE(V2){
  73.   int pinvalue2=param.asInt();
  74.    digitalWrite(mahtabi2,pinvalue2);
  75.   }
  76. ///////////////////////////////////////////
  77.  
  78. BLYNK_WRITE(V3){
  79.   int pinvalue3=param.asInt();
  80.   buttonPin=pinvalue3;
  81.   }
  82. ///////////////////////////////////////////
  83. void dama_1(void);
  84. void setup()
  85. {
  86.  
  87.   pinMode(ledPin, OUTPUT);
  88.   pinMode(mahtabi2,OUTPUT);
  89.   pinMode(mahtabi1,OUTPUT);
  90.   Serial.begin(115200);
  91.   delay(100);
  92.  
  93.   BlynkEdgent.begin();
  94.   //////////////esp now/////////////////
  95.   if (esp_now_init() != ESP_OK) {
  96.     Serial.println("Error initializing ESP-NOW");
  97.     return;
  98.   }
  99.   esp_now_register_send_cb(OnDataSent);    // register the ondatasent callback
  100.  
  101.   // Register peer
  102.   esp_now_peer_info_t peerInfo;
  103.   memcpy(peerInfo.peer_addr, broadcastAddress, 6);
  104.   peerInfo.channel = 0;
  105.   peerInfo.encrypt = false;
  106.  
  107.   // Add peer
  108.   if (esp_now_add_peer(&peerInfo) != ESP_OK){
  109.     Serial.println("Failed to add peer");
  110.     return;
  111.   }
  112.    esp_now_register_recv_cb(OnDataRecv);    // Register for a callback function that will be called when data is received
  113.  
  114.   // Setup timer and attach timer to a led pin
  115.   ledcSetup(LEDC_CHANNEL_0, LEDC_BASE_FREQ, LEDC_TIMER_13_BIT);
  116.   ledcAttachPin(ledPin, LEDC_CHANNEL_0);
  117.  
  118.   //////////////////////////////////////
  119.  
  120. }
  121.  
  122. void loop() {
  123.   BlynkEdgent.run();
  124.     dama_1();
  125.     espcon1();
  126.    
  127.    
  128. }
  129. //sensor void
  130. void dama_1() {
  131.   sensors.requestTemperatures();
  132.   Blynk.virtualWrite(V1,sensors.getTempCByIndex(0));
  133. }
  134.  
  135.  
  136. // Callback when data is sent
  137. void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
  138.   Serial.print(status == ESP_NOW_SEND_SUCCESS ? "Packet sent successfully with value: " : "Packet sending failed with values: ");
  139.   Serial.println(currButtonVal);
  140.  
  141. }
  142.  
  143. // Callback when data is received
  144. void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
  145.   memcpy(&myIncomingData, incomingData, sizeof(myIncomingData));    // copying the incoming data into our structure
  146.   receivedPotValue = myIncomingData.intPlaceHolder;    // Here I recieve potentiometer value from ESP8266 in the range [0, 1023]
  147.   receivedPotValue = receivedPotValue < 0 ? 0 : receivedPotValue > 1023 ? 1023 : receivedPotValue;    // making sure the raneg [0, 1023]
  148.   isDataReceived = true;
  149. }
  150.  
  151. // Arduino like analogWrite
  152. // value has to be between 0 and valueMax
  153. void IMHESP_analogWrite(uint8_t channel, uint32_t value, uint32_t valueMax = 255) {
  154.   uint32_t duty = (8191 / valueMax) * min(value, valueMax);    // calculate duty, 8191 from 2 ^ 13 - 1
  155.   ledcWrite(channel, duty);    // write duty to LEDC
  156. }
  157. /////////esp now loop//////////
  158. void espcon1(){
  159.  
  160.  currButtonVal = buttonPin ;
  161.   if(currButtonVal != prevButtonVal) {
  162.     prevButtonVal = currButtonVal;
  163.     myOutGoingData.intPlaceHolder = currButtonVal;
  164.     esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &myOutGoingData, sizeof(myOutGoingData));
  165.   }
  166.   if(isDataReceived) {
  167.     //isDataReceived = false;
  168.     Serial.print("[RECEIVED]>> ");
  169.     Serial.println(receivedPotValue);
  170.     Serial.println("fhsdh412572542453257cgsdhsdh");
  171.     //receivedPotValue = receivedPotValue <= 15 ? 0 : receivedPotValue;    // Just making all less values to zero
  172.     //brightness = int(map(receivedPotValue, 0, 1023, 0, 255));
  173.     //IMHESP_analogWrite(LEDC_CHANNEL_0, brightness);
  174.     Blynk.virtualWrite(V4,receivedPotValue);
  175.   }
  176.   delay(40);
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement