Skip to content
This repository was archived by the owner on Jan 28, 2025. It is now read-only.

Commit a33ffb4

Browse files
committed
Implemented HTTP Server OTA (website based OTA)
With http server based ota user can update firmware over internet (no need to connect to same wifi network). You can now easily update firmware anywhere without having physical acess to esp8266 board.
1 parent a00141c commit a33ffb4

File tree

3 files changed

+64
-12
lines changed

3 files changed

+64
-12
lines changed

Blynk-PZEM-004T-Phase-Failure-Automation/Blynk-PZEM-004T-Phase-Failure-Automation.ino

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,11 @@
3939
#include <ModbusMaster.h>
4040
#include <ESP8266WiFi.h>
4141

42-
42+
#include <ESP8266HTTPClient.h>
43+
#include <ESP8266httpUpdate.h>
44+
4345
#include <SoftwareSerial.h> //(NODEMCU ESP8266)
46+
4447
SoftwareSerial pzemSerial(RX_PIN_NODEMCU, TX_PIN_NODEMCU); //(RX,TX) NodeMCU connect to (TX,RX) of PZEM
4548

4649
/*
@@ -111,6 +114,8 @@ int auto_mode_state_1 = HIGH;
111114
int ReCnctFlag; // Reconnection Flag
112115
int ReCnctCount = 0; // Reconnection counter
113116

117+
int firmwarestate = HIGH; // Used for firmware update process
118+
114119
void setup()
115120
{
116121
Serial.begin(115200);
@@ -200,7 +205,7 @@ void setup()
200205
timer.setInterval(AUTO_MODE_TIME, auto_mode);
201206
timer.setInterval(PHYSICAL_BUTTON_TIME, checkPhysicalButton); // Setup a Relay function to be called every 100 ms
202207
timer.setInterval(SEND_TO_BLYNK_TIME, sendtoBlynk); // Send PZEM values blynk server every 10 sec
203-
}
208+
}
204209

205210
void sendtoBlynk() // Here we are sending PZEM data to blynk
206211
{
@@ -234,7 +239,9 @@ void sendtoBlynk() //
234239
Blynk.virtualWrite(vPIN_SUM_ACTIVE_ENERGY, sum_of_active_energy);
235240
Blynk.virtualWrite(vPIN_SUM_FREQUENCY, sum_of_frequency);
236241
Blynk.virtualWrite(vPIN_SUM_POWER_FACTOR, sum_of_power_factor);
237-
}
242+
243+
Blynk.virtualWrite(VPIN_FIRMWARE_VERSION, FIRMWARE_VERSION);
244+
}
238245

239246
void pzemdevice1() // Function to get PZEM device 1 data
240247
{
@@ -461,13 +468,9 @@ BLYNK_CONNECTED() { // Every time we c
461468
Blynk.syncVirtual(VPIN_BUTTON_3);
462469
Blynk.syncVirtual(VPIN_BUTTON_4);
463470
Blynk.syncVirtual(VPIN_AUTO_MODE_BUTTON_1);
464-
465-
/* Alternatively, you could override server state using:
466-
Blynk.virtualWrite(VPIN_BUTTON_1, relay1State);
467-
Blynk.virtualWrite(VPIN_BUTTON_2, relay2State);
468-
Blynk.virtualWrite(VPIN_BUTTON_3, relay3State);
469-
Blynk.virtualWrite(VPIN_BUTTON_4, relay4State);
470-
*/
471+
472+
Blynk.virtualWrite(VPIN_UPDATE_LED, 0); // Turn off FOTA Led
473+
Blynk.virtualWrite(VPIN_FIRMWARE_UPDATE, HIGH); // Turn off Firmware update button on app
471474
}
472475

473476
/* When App button is pushed - switch the state */
@@ -491,6 +494,12 @@ BLYNK_WRITE(VPIN_BUTTON_4) {
491494
BLYNK_WRITE(VPIN_AUTO_MODE_BUTTON_1) { // Get auto mode button status value
492495
auto_mode_state_1 = param.asInt();
493496
}
497+
BLYNK_WRITE(VPIN_FIRMWARE_UPDATE) { // Get update button enabled or not
498+
firmwarestate = param.asInt();
499+
if(firmwarestate == LOW){
500+
checkforupdate();
501+
}
502+
}
494503

495504
void checkPhysicalButton() // Here we are going to check push button pressed or not and change relay state
496505
{
@@ -567,7 +576,7 @@ void get_pzem_data() // Function to che
567576
pzemdevice2();
568577
pzemdevice3();
569578
sumofpzem();
570-
}
579+
}
571580

572581
void swith_off() // Function to check if voltage low condition occurs if occurs, switch off relays
573582
{
@@ -599,6 +608,43 @@ void auto_mode() // Function to check if au
599608
Serial.println("RELAY 2 Turned ON");
600609
}
601610
}
611+
612+
void checkforupdate()
613+
{
614+
Serial.println( "OTA Update Request Received" );
615+
Serial.print( "Firmware URL: " );
616+
Serial.println( FIRMWARE_URL );
617+
618+
HTTPClient httpClient;
619+
httpClient.begin( FIRMWARE_URL );
620+
int httpCode = httpClient.GET();
621+
if( httpCode == 200 ) {
622+
Serial.println( "Update file found, starting update" );
623+
Blynk.virtualWrite(VPIN_UPDATE_LED, 1023);
624+
625+
t_httpUpdate_return ret = ESPhttpUpdate.update( FIRMWARE_URL );
626+
627+
switch(ret) {
628+
case HTTP_UPDATE_FAILED:
629+
Serial.println("[update] Update failed.");
630+
break;
631+
case HTTP_UPDATE_NO_UPDATES:
632+
Serial.println("[update] Update no Update.");
633+
break;
634+
case HTTP_UPDATE_OK:
635+
Serial.println("[update] Update ok."); // may not called we reboot the ESP
636+
break;
637+
}
638+
} else {
639+
Serial.print( "Firmware check failed, got HTTP response code " );
640+
Serial.println( httpCode );
641+
}
642+
httpClient.end();
643+
644+
Blynk.virtualWrite(VPIN_UPDATE_LED, 0);
645+
Blynk.virtualWrite(VPIN_FIRMWARE_UPDATE, HIGH);
646+
647+
}
602648

603649
void loop()
604650
{

Blynk-PZEM-004T-Phase-Failure-Automation/secret.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,5 @@
2424
#define SERVER "xxx.xxx.xx.xxx" // Comment-out if use Blynk hosted cloud service
2525

2626
#define PORT 8080
27+
28+
#define FIRMWARE_URL "http://example.com/Blynk-PZEM-004T-Phase-Failure-Automation.ino.nodemcu.bin" // Enter the firmware URL location. Only http Protocol is supported. https not supported. Used for FOTA

Blynk-PZEM-004T-Phase-Failure-Automation/settings.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Settings //
33
/////////////////////////////////////////////////////////////////
44

5+
#define FIRMWARE_VERSION 02.01 // Version number. Increase each version by 1 number. Used for FOTA
56

67
/***************************************************
78
NodeMCU Pin Assignment
@@ -36,7 +37,7 @@
3637
How often you would like to call function? Enter time in milliseconds
3738
**************************************************/
3839

39-
#define GET_PZEM_DATA_TIME 1000L // How often you wish to get PZEM data. Enter time in milliseconds
40+
#define GET_PZEM_DATA_TIME 2000L // How often you wish to get PZEM data. Enter time in milliseconds
4041
#define SEND_TO_BLYNK_TIME 1000L // How often you would like to send data to blynk app. Enter time in milliseconds
4142
#define AUTO_MODE_TIME 20000L
4243
#define PHYSICAL_BUTTON_TIME 500L
@@ -61,6 +62,9 @@ How often you would like to call function? Enter time in milliseconds
6162
#define VPIN_BUTTON_4 V4
6263

6364
#define VPIN_AUTO_MODE_BUTTON_1 V5 // This button is to switch on motor in auto mode
65+
#define VPIN_FIRMWARE_UPDATE V6
66+
#define VPIN_FIRMWARE_VERSION V7
67+
#define VPIN_UPDATE_LED V8
6468

6569
/* Blynk Labeled Value vpin */
6670

0 commit comments

Comments
 (0)