├── .gitattributes ├── End-Node_ESP_2 └── End-Node_ESP_2.ino ├── End-Node_ESP_1 └── End-Node_ESP_1.ino ├── BLYNK-ESP └── BLYNK-ESP.ino └── Coordinator-ESP └── Coordinator-ESP.ino /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /End-Node_ESP_2/End-Node_ESP_2.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Code for END NODE ESP 2 3 | This will RECIEVE data from Coordinator ESP using ESP-NOW and Toggle BuiltIn LED 4 | * Developed by Jay Joshi 5 | * github.com/JayJoshi16 6 | */ 7 | 8 | 9 | #include 10 | #include 11 | uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; 12 | #include 13 | 14 | String recv_jsondata; 15 | String send_jsondata; 16 | 17 | 18 | StaticJsonDocument<256> doc_to_espnow; 19 | StaticJsonDocument<256> doc_from_espnow; // for data < 1KB 20 | //DynamicJsonDocument doc(1024); // for data > 1KB 21 | 22 | 23 | void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) { 24 | 25 | char* buff = (char*) incomingData; 26 | recv_jsondata = String(buff); 27 | Serial.print("Recieved "); Serial.println(recv_jsondata); 28 | DeserializationError error = deserializeJson(doc_from_espnow, recv_jsondata); 29 | if (!error) { 30 | String led_status = doc_from_espnow["v5"]; //values are 1 or 0 31 | Serial.print("led status : ");Serial.println(led_status); 32 | if(led_status=="v0_on") 33 | digitalWrite(2,HIGH); 34 | else if(led_status=="v0_off") 35 | digitalWrite(2,LOW); 36 | else 37 | Serial.println(led_status); 38 | 39 | } 40 | 41 | else { 42 | Serial.print(F("deserializeJson() failed: ")); 43 | Serial.println(error.f_str()); 44 | return; 45 | } 46 | 47 | } 48 | 49 | void setup() { 50 | 51 | pinMode(2,OUTPUT); 52 | 53 | Serial.begin(115200); 54 | WiFi.mode(WIFI_STA); 55 | if (esp_now_init() != ESP_OK) { 56 | Serial.println("Error initializing ESP-NOW"); 57 | return; 58 | } 59 | 60 | esp_now_register_recv_cb(OnDataRecv); 61 | } 62 | 63 | void loop() { 64 | 65 | } 66 | -------------------------------------------------------------------------------- /End-Node_ESP_1/End-Node_ESP_1.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Code for END NODE ESP1 3 | This ESP will SEND Temprature to Coordinator ESP using ESP-NOW 4 | Also it will RECIEVE data from Coordinator ESP using ESP-NOW 5 | 6 | * Developed by Jay Joshi 7 | * github.com/JayJoshi16 8 | 9 | */ 10 | #include 11 | #include 12 | uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; //Coordinator ESP MAC address 13 | 14 | #include 15 | #define DHT11PIN 5 // DHT11 on GPIO 5 16 | DHT dht(DHT11PIN, DHT11); 17 | 18 | #include 19 | String recv_jsondata; 20 | String send_jsondata; 21 | StaticJsonDocument<256> doc_to_espnow; 22 | StaticJsonDocument<256> doc_from_espnow; 23 | 24 | void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) { 25 | Serial.print("\r\nLast Packet Send Status:\t"); 26 | Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail"); 27 | } 28 | 29 | 30 | void setup() { 31 | 32 | dht.begin(); 33 | Serial.begin(115200); 34 | 35 | WiFi.mode(WIFI_STA); 36 | if (esp_now_init() != ESP_OK) { 37 | Serial.println("Error initializing ESP-NOW"); 38 | return; 39 | } 40 | esp_now_register_send_cb(OnDataSent); 41 | 42 | esp_now_peer_info_t peerInfo; 43 | memset(&peerInfo, 0, sizeof(peerInfo)); 44 | for (int ii = 0; ii < 6; ++ii ) 45 | { 46 | peerInfo.peer_addr[ii] = (uint8_t) broadcastAddress[ii]; 47 | } 48 | peerInfo.channel = 0; 49 | peerInfo.encrypt = false; 50 | 51 | // Add peer 52 | if (esp_now_add_peer(&peerInfo) != ESP_OK) { 53 | Serial.println("Failed to add peer"); 54 | return; 55 | } 56 | } 57 | 58 | void loop() { 59 | // Sending Tempreature Every 4 Seconds 60 | float temp = dht.readTemperature(); 61 | float hum = dht.readHumidity(); 62 | doc_to_espnow["v1"] = temp; // Creating JSON data. Here { v1 : 28.55 } 63 | doc_to_espnow["v2"] = hum; // Creating JSON data. Here { v2 : 34.35 } 64 | serializeJson(doc_to_espnow, send_jsondata); 65 | esp_now_send(broadcastAddress, (uint8_t *) send_jsondata.c_str(), send_jsondata.length()); 66 | // Sending it to Coordinater ESP 67 | Serial.println(send_jsondata); 68 | send_jsondata = ""; 69 | delay(4000); 70 | } 71 | -------------------------------------------------------------------------------- /BLYNK-ESP/BLYNK-ESP.ino: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | BLYNK ESP's Code. 4 | 5 | Paste this code in EXAMPLES > BLYNK > Blynk_edgent > Edgent_ESP32 6 | 7 | This ESP will recieve JSON data from Coordinator ESP's and Sends it to BLYNK Cloud and vise versa. 8 | Only this ESP is connected to Internet 9 | 10 | Developed by Jay Joshi 11 | github.com/JayJoshi16 12 | 13 | Modified by Sachin Soni 14 | 15 | developed for techiesms 16 | 17 | */ 18 | #define BLYNK_TEMPLATE_ID "TMPLkMNvOYYR" 19 | #define BLYNK_DEVICE_NAME "ESPNOW" 20 | 21 | #define BLYNK_FIRMWARE_VERSION "0.1.0" 22 | #define BLYNK_PRINT Serial 23 | 24 | #include "BlynkEdgent.h" 25 | #include 26 | 27 | String recv_str_jsondata; 28 | 29 | StaticJsonDocument<256> doc_send; 30 | StaticJsonDocument<256> doc_recv; 31 | 32 | #define RXD2 16 33 | #define TXD2 17 34 | 35 | 36 | BLYNK_WRITE(V0) // Read from Virtual pin V4 37 | { 38 | 39 | bool s0 = param.asInt(); // parameter as int 40 | if (s0 == HIGH) 41 | { 42 | doc_send["v0"] = "v0_on"; // Writing { "v4" : 1 } on Serial2 43 | Serial.println("Sended v0 : v0_on"); 44 | serializeJson(doc_send, Serial2); 45 | delay(10); 46 | } 47 | else if (s0 == LOW) 48 | { 49 | doc_send["v0"] = "v0_off"; // Writing { "v4" :0 } on Serial2 50 | Serial.println("Sended v0 : v0_off"); 51 | serializeJson(doc_send, Serial2); 52 | delay(10); 53 | } 54 | } 55 | 56 | void setup() 57 | { 58 | BlynkEdgent.begin(); 59 | 60 | Serial.begin(115200); 61 | Serial2.begin(115200, SERIAL_8N1, RXD2, TXD2); //Hardware Serial of ESP32 62 | 63 | } 64 | 65 | void loop() { 66 | 67 | BlynkEdgent.run(); 68 | 69 | // Recieving data (JSON) from Coordinator ESP 70 | if (Serial2.available()) 71 | { 72 | 73 | recv_str_jsondata = Serial2.readStringUntil('\n'); 74 | Serial.println(recv_str_jsondata); 75 | DeserializationError error = deserializeJson(doc_recv, recv_str_jsondata); 76 | 77 | if (!error) { // if not error in deserialization 78 | float temp = doc_recv["v1"]; // fetch temprature data from JSON . Here { v1 : 28.55 } 79 | float hum = doc_recv["v2"]; // fetch temprature data from JSON . Here { v2 :32.25 } 80 | 81 | if (temp > 0) { 82 | Blynk.virtualWrite(V1, temp); // writing temprature to BLYNK Cloud 83 | Serial.print("temp ="); Serial.println(temp); 84 | } 85 | if (hum > 0) { 86 | Blynk.virtualWrite(V2, hum); // writing temprature to BLYNK Cloud 87 | Serial.print("hum ="); Serial.println(hum); 88 | } 89 | 90 | } 91 | 92 | else { 93 | Serial.print(F("deserializeJson() failed: ")); 94 | Serial.println(error.f_str()); 95 | return; 96 | } 97 | recv_str_jsondata = ""; 98 | } 99 | 100 | } 101 | -------------------------------------------------------------------------------- /Coordinator-ESP/Coordinator-ESP.ino: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Coordiante ESP's Code. 4 | This ESP will recieve ESPNOW data from END Node ESP's and Serially trasnfers it to BLYNK ESP and vise versa. 5 | MAC Address of this ESP is Provided to ESD Node ESP. 6 | 7 | Developed by Jay Joshi 8 | github.com/JayJoshi16 9 | 10 | Modified by Sachin Soni 11 | 12 | Developed for techiesms 13 | https://www.youtube.com/techiesms 14 | */ 15 | 16 | #include 17 | #include 18 | #include //https://github.com/jfturcot/SimpleTimer 19 | #include //https://github.com/bblanchon/ArduinoJson 20 | #include // oled 21 | #include 22 | #include 23 | 24 | 25 | uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; // Universal Broadcast address 26 | 27 | 28 | StaticJsonDocument<256> doc_from_espnow; // JSON Doc for Receiving data from ESPNOW Devices 29 | StaticJsonDocument<256> doc_to_espnow; // JSON Doc for Transmitting data to ESPNOW Devices 30 | 31 | String recv_jsondata; // recieved JSON string 32 | 33 | int temperature = 0; 34 | int humidity = 0; 35 | 36 | // Hardware Serial 2 37 | #define RXD2 16 38 | #define TXD2 17 39 | 40 | 41 | // OLED screen Configurations 42 | #define SCREEN_WIDTH 128 43 | #define SCREEN_HEIGHT 64 44 | Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); 45 | 46 | 47 | 48 | // ESPNOW Send Callback Function 49 | void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) 50 | { 51 | Serial.print("\r\nLast Packet Send Status:\t"); 52 | Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail"); 53 | } 54 | 55 | //ESPNOW Receive Callback Function 56 | void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) 57 | { 58 | 59 | char* buff = (char*) incomingData; 60 | recv_jsondata = String(buff); 61 | Serial.print("Recieved from ESPNOW: "); Serial.println(recv_jsondata); 62 | DeserializationError error = deserializeJson(doc_from_espnow, recv_jsondata); 63 | if (!error) { 64 | Serial.print("Serilising to Serial2: "); 65 | Serial.println(recv_jsondata); 66 | temperature = doc_from_espnow["v1"]; // Storing Temperature Data 67 | humidity = doc_from_espnow["v2"] // Storing Humidity Data 68 | serializeJson(doc_from_espnow, Serial2); // Writing Data to Serial2 69 | } 70 | 71 | else 72 | { 73 | Serial.print(F("deserializeJson() failed: ")); 74 | Serial.println(error.f_str()); 75 | return; 76 | } 77 | 78 | } 79 | 80 | // Function that Displays tempreature on OLED 81 | void displayText() 82 | { 83 | display.clearDisplay(); 84 | display.setTextSize(1); 85 | display.setTextColor(SSD1306_WHITE); 86 | display.setCursor(2, 10); 87 | display.print("Temperature :"); 88 | display.println(temperature); 89 | display.print("Humidity :"); 90 | display.println(humidity); 91 | display.display(); 92 | } 93 | 94 | void setup() { 95 | 96 | //ONBOARD LED WILL GLOW IN CASE OF RESET 97 | pinMode(2, OUTPUT); 98 | digitalWrite(2, HIGH); 99 | delay(2000); 100 | digitalWrite(2, LOW); 101 | delay(2000); 102 | 103 | // Initialising UART Communication 104 | Serial.begin(115200); 105 | Serial2.begin(115200, SERIAL_8N1, RXD2, TXD2); 106 | 107 | // Initialising OLED (I2C) Communication 108 | if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) 109 | { // Address 0x3D for 128x64 110 | Serial.println(F("SSD1306 allocation failed")); 111 | for (;;); 112 | } 113 | delay(200); 114 | display.display(); 115 | display.clearDisplay(); 116 | delay(200); 117 | 118 | 119 | 120 | WiFi.mode(WIFI_STA); 121 | 122 | // Initialising ESPNOW Communication 123 | if (esp_now_init() != ESP_OK) 124 | { 125 | Serial.println("Error initializing ESP-NOW"); 126 | return; 127 | } 128 | 129 | // Defining Callback Functions 130 | esp_now_register_recv_cb(OnDataRecv); 131 | esp_now_register_send_cb(OnDataSent); 132 | 133 | // Adding Peer Devices 134 | esp_now_peer_info_t peerInfo; 135 | memset(&peerInfo, 0, sizeof(peerInfo)); 136 | for (int ii = 0; ii < 6; ++ii ) 137 | { 138 | peerInfo.peer_addr[ii] = (uint8_t) broadcastAddress[ii]; 139 | } 140 | peerInfo.channel = 0; 141 | peerInfo.encrypt = false; 142 | 143 | if (esp_now_add_peer(&peerInfo) != ESP_OK) { 144 | Serial.println("Failed to add peer"); 145 | return; 146 | } 147 | 148 | timer.setTimeout(2000, displayText); // One Shot Timer to display data on OLED 149 | } 150 | 151 | void loop() 152 | { 153 | 154 | if (Serial2.available()) 155 | { 156 | // Recieving data (JSON) from BLYNK ESP 157 | String recv_str_jsondata = Serial2.readStringUntil('\n'); 158 | 159 | //Serializing JSON 160 | serializeJson(doc_to_espnow, recv_str_jsondata); 161 | Serial.println(recv_str_jsondata); 162 | 163 | // Broadcasting data (JSON) via ESP-NOW 164 | esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) recv_str_jsondata.c_str(), sizeof(recv_str_jsondata) * recv_str_jsondata.length()); 165 | 166 | if (result == ESP_OK) 167 | { 168 | Serial.println("Sent with success"); 169 | } 170 | else 171 | { 172 | Serial.println(result); 173 | } 174 | delay(20); 175 | } 176 | } 177 | --------------------------------------------------------------------------------