Have you ever been shocked by a suddenly high electricity bill? This DIY IoT ESP32 prepaid energy Meter project puts you back in control of your energy usage and costs. Monitoring and controlling electricity consumption are essential for cost efficiency and safety.
In this project, we will build an IoT-based Prepaid Energy Meter using ESP32, PZEM-004T, a relay module, and a TFT display. The system monitors real-time voltage, current, power, and energy usage, deducts a balance based on consumption, and disconnects power when the balance is low. Users can also control and recharge their balance via a web interface. Checkout latest project: Build an IoT Energy Meter with ESP32 & PZEM-004T
This smart device allows you to:
- Real-time power usage (voltage, current, power, and energy)
- Load credits into a prepaid account
- Automatically cut power when the balance runs out or below the threshold
- Access all data through a web dashboard
- Get alerts for electrical problems like voltage spikes
Required Components
- ESP32 BOARD ( Amazon / Aliexpress )
- PZEM-004T V3.0 Module ( Amazon / Aliexpress )
- 1.8″ TFT LCD DISPLAY ( Amazon / Aliexpress )
- 5V RELAY MODULE ( Amazon / Aliexpress )
- PUSH BUTTON
- JUMPER WIRES
- BREADBOARD OR PCB
Software Requirements:
- Arduino IDE
- Required Libraries:
- PZEM004Tv30
- Adafruit GFX Library
- Adafruit ST7735 Library
PZEM-004T Energy Meter Module
The PZEM-004T is an AC energy monitoring module for measuring voltage, current, active power, frequency, power factor, and energy consumption. It communicates via RS485 using the Modbus-RTU protocol.
Key Features:
- Voltage 80–260V, ±0.5% accuracy.
- Current: Up to 10A (built-in shunt) or 100A (external CT), ±0.5% accuracy.
- Power : Up to 2.3kW (10A) or 23kW (100A), ±0.5% accuracy.
- Energy : 0–9999.99kWh, resettable via software.
- Communication RS485 (TTL), 9600 baud rate, 8N1.
Pinout:
- L (Live): Connect to AC mains
- N (Neutral): Connect to AC neutral
- CT+ & CT-: Connect to the external current transformer
- 5V & GND: Power the module
- TX & RX: Serial communication
Applications:
- Home energy monitoring.
- Industrial automation.
- Renewable energy systems.
Working Principle | IoT ESP32 Prepaid Energy Meter With PZEM-004T
- Measurement: The PZEM sensor safely connects to your electrical line and measures voltage, current, power, and energy usage.
- Processing: The ESP32 reads this data, calculates your remaining balance based on energy used, and controls everything.
- Display: You can view all information on the attached TFT screen and a web dashboard from any device on your home network.
- Control: When your balance is too low or there’s an electrical problem, the system automatically cuts power using the relay.
- Management: You can add credit, change settings, and manually control power through the web interface.
Connect the Components
1. PZEM-004T Connections
- Live Wire (V+) → AC Live Input
- Neutral Wire (V-) → AC Neutral Input
- Current Input (I+) → External CT (for 100A model) or internal shunt (for 10A model)
- RS485 Communication :
- RX → ESP32 TX (e.g., GPIO17 for TX2)
- TX → ESP32 RX (e.g., GPIO16 for RX2)
- Power Supply :
- 5V → ESP32 5V
- GND → ESP32 GND
-
Load (Bulb) Connections
- Bulb One Terminal → AC Live Output (from PZEM-004T)
- Bulb Other Terminal → Relay COM (Common) Pin
- Relay NO (Normally Open) → AC Neutral Output (from PZEM-004T)
2. Relay Connections
- Relay VCC → ESP32 5V
- Relay GND → ESP32 GND
- Relay IN → ESP32 GPIO Pin (e.g., GPIO4)
3. TFT Display to ESP32 Connections
TFT Pin | ESP32 Pin | Description |
---|---|---|
VCC | 3.3V | Power supply (3.3V) |
GND | GND | Ground |
CS | GPIO5 | Chip Select (SPI) |
RST | GPIO22 | Reset (optional if tied high) |
DC (RS) | GPIO21 | Data/Command Control |
SDA (MOSI) | GPIO23 | SPI Data line (VSPI MOSI) |
SCK | GPIO18 | SPI Clock |
LED (BL) | 3.3V | Backlight |
4. Connect the Push Button
- Button one leg → ESP32 Pin 32
- Button other leg → ESP32 GND
Install the Libraries:
- Add ESP32 board support to Arduino IDE. If you have not already setup
- Before uploading the code, install the following libraries :
Upload the Code
- Copy the entire project code from the code below and open the code in Arduino IDE.
Modify the WiFi settings with your network credentials.
1 2 |
const char* ssid = "YourWiFiName"; const char* password = "YourWiFiPassword"; |
Source code – IoT ESP32 Prepaid Energy Meter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 |
#include <PZEM004Tv30.h> #include <SPI.h> #include <Adafruit_GFX.h> #include <Adafruit_ST7735.h> #include <WiFi.h> #include <WebServer.h> #include <ArduinoJson.h> #include <EEPROM.h> #include <ESP.h> // Required for ESP.restart() // Define pins for PZEM #define PZEM_RX_PIN 16 #define PZEM_TX_PIN 17 // TFT display pins #define TFT_CS 5 #define TFT_DC 21 #define TFT_RST 22 #define TFT_MOSI 23 #define TFT_SCLK 18 // Relay pin for power control #define RELAY_PIN 4 // Push button pin for page change and manual relay override #define BUTTON_PIN 32 // Example pin, choose an available pin // EEPROM addresses #define EEPROM_SIZE 128 // Increased EEPROM size to accommodate more settings #define BALANCE_ADDR 0 #define ENERGY_ADDR 4 #define OVER_VOLTAGE_THRESHOLD_ADDR 8 #define OVER_CURRENT_THRESHOLD_ADDR 12 #define THEFT_CURRENT_THRESHOLD_ADDR 16 #define MINIMUM_BALANCE_ADDR 20 #define COST_PER_KWH_ADDR 24 // Corrected definition - retyped this line // Default values for thresholds (used if EEPROM is empty or corrupted) #define DEFAULT_OVER_VOLTAGE_THRESHOLD 260.0 #define DEFAULT_OVER_CURRENT_THRESHOLD 10.0 #define DEFAULT_THEFT_CURRENT_THRESHOLD 0.02 #define DEFAULT_MINIMUM_BALANCE 0.0 #define DEFAULT_COST_PER_KWH 0.2 #define DEFAULT_BALANCE 0.0 #define DEFAULT_ENERGY 0.0 // Authentication credentials for web interface const char* admin_username = "admin"; const char* admin_password = "admin123"; // Server port WebServer server(80); // Colors (Added missing colors if needed) #define BLACK 0x0000 #define BLUE 0x001F #define RED 0xF800 #define GREEN 0x07E0 #define YELLOW 0xFFE0 #define WHITE 0xFFFF #define DARKGRAY 0x4208 #define CYAN 0x07FF #define MAGENTA 0xF81F #define ORANGE 0xFD20 // Example Orange // Text buffers to track changes and avoid unnecessary redraws char lastVoltageText[20] = ""; // Increased buffer size to 20 char lastCurrentText[20] = ""; char lastPowerText[20] = ""; char lastEnergyText[20] = ""; char lastBalanceText[20] = ""; char lastStatusText[20] = ""; char lastFaultText[20] = ""; char lastTheftText[20] = ""; // Common text settings - Function to reduce repetition void setTextProperties(Adafruit_ST7735 &display, uint16_t color, uint8_t size = 1) { // Modified to accept tft display object display.setTextColor(color); display.setTextSize(size); } // Global variables unsigned long measurementInterval = 2000; // Interval between PZEM readings (milliseconds) unsigned long lastReadTime = 0; unsigned long screenRefreshTime = 0; unsigned long balanceUpdateTime = 0; unsigned long lastEnergyValue = 0; const unsigned long SCREEN_REFRESH_INTERVAL = 1000; // Interval for screen refresh (milliseconds) const unsigned long BALANCE_UPDATE_INTERVAL = 60000; // Update balance every minute bool layoutDrawn = false; unsigned long notificationEndTime = 0; //used for determine the end of the notification String currentNotification = ""; // string to current displaying notification uint16_t notificationColor = WHITE; bool pzemConnected = true; // Flag to track PZEM connection status bool relayState = true; // Flag to track relay state float balance = 0.0; // Current balance in rupees float lastEnergy = 0.0; // Last energy reading bool faultDetected = false; // Flag for fault detection bool theftDetected = false; // Flag for theft detection int displayPage = 0; // Current display page (0=main, 1=alerts) unsigned long pageChangeTime = 0; // Time to switch between pages const unsigned long PAGE_INTERVAL = 5000; // Switch pages every 5 seconds bool manualOverride = false; // Flag for manual relay control override // Thresholds - now variables, loaded from EEPROM and configurable float overVoltageThreshold = DEFAULT_OVER_VOLTAGE_THRESHOLD; float overCurrentThreshold = DEFAULT_OVER_CURRENT_THRESHOLD; float theftCurrentThreshold = DEFAULT_THEFT_CURRENT_THRESHOLD; float minimumBalance = DEFAULT_MINIMUM_BALANCE; float costPerKWh = DEFAULT_COST_PER_KWH; // Initialize TFT and PZEM Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST); PZEM004Tv30 pzem(Serial2, PZEM_RX_PIN, PZEM_TX_PIN); // Structure for PZEM readings struct PzemData { float voltage; float current; float power; float energy; bool isValid; }; // Store readings PzemData readings = {0, 0, 0, 0, false}; // Store IP address as a string to avoid repeated calls String ipAddressStr = ""; // WiFi credentials - Replace with your actual credentials const char* ssid = "ESP32"; const char* password = "aaaaaaaa"; // Button debounce variables const unsigned long debounceDelay = 50; // Debounce delay in milliseconds unsigned long lastButtonPress = 0; bool buttonState = false; // Current button state bool lastButtonState = false; // Previous button state // Notification timer interval (5 minutes) const unsigned long NOTIFICATION_INTERVAL = 300000; // 5 minutes in milliseconds unsigned long lastNotificationTime = 0; // WiFi reconnection variables unsigned long lastWifiCheckTime = 0; const unsigned long WIFI_RECHECK_INTERVAL = 30000; // Check WiFi every 30 seconds // Manual relay override button variables unsigned long buttonPressStartTime = 0; bool manualRelayControlActive = false; // Function prototypes void handleRoot(); void handleLogin(); void handleRecharge(); void handleData(); void handleReset(); void handleSetRelay(); void handleNotFound(); void readPzemData(); void drawLayout(); void drawLiveDataPage(); void drawAlertsPage(); void updateDisplayValues(); void showNotification(const String& message, uint16_t color, unsigned long duration = 5000); //default 5 sec notification void clearNotification(); void displayStartupSequence(); void configureWiFi(); // Modified to remove WiFiManager void saveBalance(); void loadBalance(); void updateBalance(); void checkFaultsAndTheft(); void controlRelay(); void printDebugInfo(const char* action, bool success); void handleSetThresholds(); // New handler for setting thresholds void saveThresholds(); // Function to save thresholds to EEPROM void loadThresholds(); // Function to load thresholds from EEPROM String generateThresholdsHTML(); // Function to generate HTML for threshold settings section void handleFactoryReset(); // New handler for factory reset void resetEEPROMToDefaults(); // Function to reset EEPROM to defaults void checkWiFiConnection(); // Function to check and reconnect WiFi // Helper function to clear a text area on TFT void clearTextArea(int x, int y, int w, int h) { tft.fillRect(x, y, w, h, BLACK); } // HTML page stored in PROGMEM (Improved Web Dashboard UI/UX) - Modified to include Factory Reset Button const char index_html[] PROGMEM = R"rawliteral( ` <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Prepaid Energy Monitor Pro</title> <style> /* Modern Professional Color Palette */ :root { --primary: #0a2463; --secondary: #3e92cc; --accent: #2dc653; --warning: #ffb400; --danger: #d64045; --dark: #1e1f26; --light: #f8f9fa; --gray-100: #f0f2f5; --gray-200: #e9ecef; --gray-300: #dee2e6; --gray-400: #ced4da; --gray-500: #adb5bd; --gray-600: #6c757d; --gray-700: #495057; --gray-800: #343a40; --text: #212529; --box-shadow: 0 2px 8px rgba(0, 0, 0, 0.12); --card-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); --transition: all 0.2s ease; } * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: 'Roboto', 'Segoe UI', sans-serif; background-color: var(--gray-100); color: var(--text); line-height: 1.6; } .dashboard-container { display: grid; grid-template-columns: 240px 1fr; min-height: 100vh; } /* Sidebar */ .sidebar { background-color: var(--primary); color: white; padding-top: 20px; position: fixed; height: 100vh; width: 240px; z-index: 100; box-shadow: var(--box-shadow); } .brand { display: flex; align-items: center; padding: 0 20px 20px; border-bottom: 1px solid rgba(255, 255, 255, 0.1); } .brand-icon { font-size: 24px; margin-right: 10px; } .brand-name { font-size: 18px; font-weight: 600; } .nav-menu { list-style: none; padding: 20px 0; } .nav-item { padding: 0 15px; margin-bottom: 5px; } .nav-link { display: flex; align-items: center; padding: 12px 15px; color: rgba(255, 255, 255, 0.8); text-decoration: none; border-radius: 5px; transition: var(--transition); } .nav-link:hover { background-color: rgba(255, 255, 255, 0.1); color: white; } .nav-link.active { background-color: var(--secondary); color: white; } .nav-icon { margin-right: 10px; width: 20px; text-align: center; } /* Main Content */ .main-content { grid-column: 2; padding: 20px; } .page-header { padding: 15px 0; margin-bottom: 20px; display: flex; justify-content: space-between; align-items: center; } .page-title { font-size: 24px; font-weight: 500; color: var(--dark); } .page-subtitle { font-size: 14px; color: var(--gray-600); margin-top: 5px; } /* Cards & Grids */ .card-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 20px; margin-bottom: 25px; } .card { background-color: white; border-radius: 10px; box-shadow: var(--card-shadow); padding: 20px; transition: var(--transition); } .card:hover { box-shadow: 0 10px 15px rgba(0, 0, 0, 0.07); transform: translateY(-2px); } .card-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 15px; } .card-title { font-size: 16px; font-weight: 500; color: var(--gray-700); } .card-icon { font-size: 18px; width: 36px; height: 36px; display: flex; align-items: center; justify-content: center; border-radius: 50%; background-color: var(--secondary); color: white; } .dashboard-value { font-size: 32px; font-weight: 600; color: var(--primary); margin: 10px 0; } .unit { font-size: 16px; font-weight: normal; color: var(--gray-600); } .trend { display: flex; align-items: center; font-size: 14px; color: var(--gray-600); } .trend-up { color: var(--accent); } .trend-down { color: var(--danger); } /* Status Indicators */ .status-card { display: flex; flex-direction: column; height: 100%; } .status-indicators { flex-grow: 1; } .status-item { display: flex; align-items: center; padding: 12px 0; border-bottom: 1px solid var(--gray-200); } .status-item:last-child { border-bottom: none; } .status-indicator { width: 12px; height: 12px; border-radius: 50%; margin-right: 12px; } .status-active { background-color: var(--accent); } .status-inactive { background-color: var(--gray-500); } .status-warning { background-color: var(--warning); } .status-danger { background-color: var(--danger); } .status-label { font-weight: 500; flex-grow: 1; } .status-value { font-weight: 600; } /* Action Areas */ .action-card { height: 100%; } .action-content { padding-top: 10px; } .input-group { display: flex; margin-bottom: 15px; } .input-group .form-input { flex-grow: 1; border-top-right-radius: 0; border-bottom-right-radius: 0; } .input-group .input-append { display: flex; align-items: center; padding: 0 12px; background-color: var(--gray-200); border: 1px solid var(--gray-300); border-left: none; border-top-right-radius: 5px; border-bottom-right-radius: 5px; color: var(--gray-700); } /* Forms */ .form-group { margin-bottom: 20px; } .form-label { display: block; margin-bottom: 8px; font-weight: 500; color: var(--gray-700); } .form-input { width: 100%; padding: 10px 12px; border: 1px solid var(--gray-300); border-radius: 5px; font-size: 14px; transition: var(--transition); } .form-input:focus { outline: none; border-color: var(--secondary); box-shadow: 0 0 0 3px rgba(62, 146, 204, 0.1); } /* Buttons */ .btn { display: inline-block; font-weight: 500; text-align: center; vertical-align: middle; cursor: pointer; border: 1px solid transparent; padding: 10px 16px; font-size: 14px; line-height: 1.5; border-radius: 5px; transition: var(--transition); } .btn-primary { color: white; background-color: var(--secondary); border-color: var(--secondary); } .btn-primary:hover { background-color: #3182b8; border-color: #3182b8; } .btn-success { color: white; background-color: var(--accent); border-color: var(--accent); } .btn-success:hover { background-color: #25a745; border-color: #25a745; } .btn-danger { color: white; background-color: var(--danger); border-color: var(--danger); } .btn-danger:hover { background-color: #c13c40; border-color: #c13c40; } .btn-block { display: block; width: 100%; } .btn:disabled { opacity: 0.65; pointer-events: none; } /* Alerts */ .alert { padding: 12px 15px; margin-bottom: 15px; border-radius: 5px; border: 1px solid transparent; font-size: 14px; } .alert-success { color: #155724; background-color: #d4edda; border-color: #c3e6cb; } .alert-danger { color: #721c24; background-color: #f8d7da; border-color: #f5c6cb; } .alert-warning { color: #856404; background-color: #fff3cd; border-color: #ffeeba; } /* Settings */ .settings-section { margin-bottom: 25px; } .settings-header { display: flex; align-items: center; margin-bottom: 15px; padding-bottom: 10px; border-bottom: 1px solid var(--gray-300); } .settings-icon { margin-right: 10px; color: var(--secondary); } .settings-title { font-size: 18px; font-weight: 500; } .settings-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 20px; } /* Hide sections */ .page-section { display: none; } .page-section.active { display: block; } /* Footer */ .dashboard-footer { margin-top: 30px; padding-top: 15px; border-top: 1px solid var(--gray-300); color: var(--gray-600); font-size: 14px; text-align: center; } /* Responsive */ @media (max-width: 992px) { .dashboard-container { grid-template-columns: 1fr; } .sidebar { display: none; } .main-content { grid-column: 1; } } </style> </head> <body> <div class="dashboard-container"> <!-- Sidebar Navigation --> <div class="sidebar"> <div class="brand"> <div class="brand-icon">⚡</div> <div class="brand-name">EnergyPro</div> </div> <ul class="nav-menu"> <li class="nav-item"> <a href="#dashboard" class="nav-link active" onclick="showPage('dashboard')"> <span class="nav-icon">📊</span> <span>Dashboard</span> </a> </li> <li class="nav-item"> <a href="#account" class="nav-link" onclick="showPage('account')"> <span class="nav-icon">💰</span> <span>Account</span> </a> </li> <li class="nav-item"> <a href="#control" class="nav-link" onclick="showPage('control')"> <span class="nav-icon">🎮</span> <span>Control</span> </a> </li> <li class="nav-item"> <a href="#settings" class="nav-link" onclick="showPage('settings')"> <span class="nav-icon">⚙️</span> <span>Settings</span> </a> </li> <li class="nav-item"> <a href="#reports" class="nav-link" onclick="showPage('reports')"> <span class="nav-icon">📈</span> <span>Reports</span> </a> </li> <li class="nav-item"> <a href="#help" class="nav-link" onclick="showPage('help')"> <span class="nav-icon">❓</span> <span>Help</span> </a> </li> </ul> </div> <!-- Main Content Area --> <div class="main-content"> <!-- Dashboard Section --> <div id="dashboard-section" class="page-section active"> <div class="page-header"> <div> <h1 class="page-title">Energy Dashboard</h1> <p class="page-subtitle">Real-time monitoring of your prepaid energy system</p> </div> <div id="update-timestamp">Last updated: --:--:--</div> </div> <!-- Metric Cards --> <div class="card-grid"> <div class="card"> <div class="card-header"> <h3 class="card-title">BALANCE</h3> <div class="card-icon">₹</div> </div> <div class="dashboard-value" id="balance">--<span class="unit">₹</span></div> <div class="trend">Available credit</div> </div> <div class="card"> <div class="card-header"> <h3 class="card-title">VOLTAGE</h3> <div class="card-icon">V</div> </div> <div class="dashboard-value" id="voltage">--<span class="unit">V</span></div> <div class="trend">Line voltage</div> </div> <div class="card"> <div class="card-header"> <h3 class="card-title">CURRENT</h3> <div class="card-icon">A</div> </div> <div class="dashboard-value" id="current">--<span class="unit">A</span></div> <div class="trend">Line current</div> </div> <div class="card"> <div class="card-header"> <h3 class="card-title">POWER</h3> <div class="card-icon">W</div> </div> <div class="dashboard-value" id="power">--<span class="unit">W</span></div> <div class="trend">Active power</div> </div> </div> <!-- Status and Control Cards --> <div class="card-grid"> <div class="card status-card"> <div class="card-header"> <h3 class="card-title">SYSTEM STATUS</h3> </div> <div class="status-indicators"> <div class="status-item"> <div class="status-indicator" id="power-indicator"></div> <div class="status-label">Power</div> <div class="status-value" id="power-status">--</div> </div> <div class="status-item"> <div class="status-indicator" id="fault-indicator"></div> <div class="status-label">Fault Status</div> <div class="status-value" id="fault-status">--</div> </div> <div class="status-item"> <div class="status-indicator" id="theft-indicator"></div> <div class="status-label">Theft Detection</div> <div class="status-value" id="theft-status">--</div> </div> <div class="status-item"> <div class="status-indicator status-active"></div> <div class="status-label">Total Energy</div> <div class="status-value" id="energy-value">-- kWh</div> </div> </div> </div> <div class="card action-card"> <div class="card-header"> <h3 class="card-title">QUICK ACTIONS</h3> </div> <div class="action-content"> <div id="power-control-alert" style="display:none;" class="alert"></div> <p style="margin-bottom:15px;">Control power supply to your premises</p> <div style="display:flex;gap:10px;margin-bottom:20px;"> <button id="power-on-btn" class="btn btn-success" onclick="setRelay(1)" style="flex:1;" disabled>Power ON</button> <button id="power-off-btn" class="btn btn-danger" onclick="setRelay(0)" style="flex:1;" disabled>Power OFF</button> </div> <p style="margin-bottom:15px;">Quick recharge your account</p> <div class="input-group"> <input type="number" id="quick-recharge-amount" class="form-input" value="100" min="10" step="10"> <div class="input-append">₹</div> </div> <button class="btn btn-primary btn-block" onclick="quickRecharge()">Recharge Now</button> </div> </div> </div> </div> <!-- Account Section --> <div id="account-section" class="page-section"> <div class="page-header"> <div> <h1 class="page-title">Account Management</h1> <p class="page-subtitle">Manage your prepaid account balance and transactions</p> </div> </div> <div class="card-grid"> <div class="card"> <div class="card-header"> <h3 class="card-title">ACCOUNT BALANCE</h3> </div> <div class="dashboard-value" id="account-balance">--<span class="unit">₹</span></div> <div class="trend">Available credit</div> </div> <div class="card"> <div class="card-header"> <h3 class="card-title">CONSUMPTION RATE</h3> </div> <div class="dashboard-value" id="account-balance">--<span class="unit">₹/day</span></div> <div class="trend">Average daily consumption</div> </div> </div> <div class="card"> <div class="card-header"> <h3 class="card-title">RECHARGE ACCOUNT</h3> </div> <div id="recharge-alert" style="display:none;" class="alert"></div> <div class="form-group"> <label for="recharge-amount" class="form-label">Recharge Amount</label> <div class="input-group"> <input type="number" id="recharge-amount" class="form-input" value="100" min="10" step="10"> <div class="input-append">₹</div> </div> </div> <button class="btn btn-primary" onclick="rechargeAccount()">Process Recharge</button> </div> </div> <!-- Control Section --> <div id="control-section" class="page-section"> <div class="page-header"> <div> <h1 class="page-title">System Control</h1> <p class="page-subtitle">Monitor and control your energy system</p> </div> </div> <div class="card-grid"> <div class="card"> <div class="card-header"> <h3 class="card-title">POWER CONTROL</h3> </div> <div id="relay-control-alert" style="display:none;" class="alert"></div> <p style="margin:15px 0;">Current power status: <span id="relay-control-status">Loading...</span></p> <div style="display:flex;gap:10px;"> <button id="control-power-on-btn" class="btn btn-success" onclick="setRelay(1)" style="flex:1;" disabled>Turn Power ON</button> <button id="control-power-off-btn" class="btn btn-danger" onclick="setRelay(0)" style="flex:1;" disabled>Turn Power OFF</button> </div> </div> <div class="card"> <div class="card-header"> <h3 class="card-title">SYSTEM STATUS</h3> </div> <div class="status-indicators"> <div class="status-item"> <div class="status-indicator" id="control-voltage-indicator"></div> <div class="status-label">Voltage Status</div> <div class="status-value" id="control-voltage-status">--</div> </div> <div class="status-item"> <div class="status-indicator" id="control-current-indicator"></div> <div class="status-label">Current Status</div> <div class="status-value" id="control-current-status">--</div> </div> <div class="status-item"> <div class="status-indicator" id="control-fault-indicator"></div> <div class="status-label">Fault Status</div> <div class="status-value" id="control-fault-status">--</div> </div> <div class="status-item"> <div class="status-indicator" id="control-theft-indicator"></div> <div class="status-label">Theft Detection</div> <div class="status-value" id="control-theft-status">--</div> </div> </div> </div> </div> </div> <!-- Settings Section --> <div id="settings-section" class="page-section"> <div class="page-header"> <div> <h1 class="page-title">System Settings</h1> <p class="page-subtitle">Configure thresholds and system parameters</p> </div> </div> <div id="threshold-alert" style="display:none;" class="alert"></div> <form id="threshold-form"> <div class="card settings-section"> <div class="settings-header"> <span class="settings-icon">⚠️</span> <h3 class="settings-title">Safety Thresholds</h3> </div> <div class="settings-grid"> <div class="form-group"> <label for="overVoltage" class="form-label">Over Voltage Threshold</label> <div class="input-group"> <input type="number" id="overVoltage" name="overVoltage" class="form-input" value="260" step="1"> <div class="input-append">V</div> </div> </div> <div class="form-group"> <label for="overCurrent" class="form-label">Over Current Threshold</label> <div class="input-group"> <input type="number" id="overCurrent" name="overCurrent" class="form-input" value="10.00" step="0.1"> <div class="input-append">A</div> </div> </div> </div> </div> <div class="card settings-section"> <div class="settings-header"> <span class="settings-icon">🔒</span> <h3 class="settings-title">Security Settings</h3> </div> <div class="settings-grid"> <div class="form-group"> <label for="theftCurrent" class="form-label">Theft Detection Threshold</label> <div class="input-group"> <input type="number" id="theftCurrent" name="theftCurrent" class="form-input" value="0.020" step="0.001"> <div class="input-append">A</div> </div> </div> </div> </div> <div class="card settings-section"> <div class="settings-header"> <span class="settings-icon">💰</span> <h3 class="settings-title">Billing Settings</h3> </div> <div class="settings-grid"> <div class="form-group"> <label for="minBalance" class="form-label">Minimum Balance Threshold</label> <div class="input-group"> <input type="number" id="minBalance" name="minBalance" class="form-input" value="0.00" step="1"> <div class="input-append">₹</div> </div> </div> <div class="form-group"> <label for="costPerKWh" class="form-label">Cost per kWh</label> <div class="input-group"> <input type="number" id="costPerKWh" name="costPerKWh" class="form-input" value="0.20" step="0.01"> <div class="input-append">₹</div> </div> </div> </div> </div> <button type="button" class="btn btn-primary" onclick="setThresholds()">Save All Settings</button> </form> <!-- Factory Reset Button --> <div class="card settings-section"> <div class="settings-header"> <span class="settings-icon" style="color: var(--danger);">⚠️</span> <h3 class="settings-title" style="color: var(--danger);">Factory Reset</h3> </div> <p style="margin-top: 10px; margin-bottom: 20px;">Reset all settings to default and clear energy data. This action is irreversible.</p> <button type="button" class="btn btn-danger" onclick="factoryReset()">Factory Reset</button> <div id="factory-reset-alert" style="display:none;" class="alert"></div> </div> </div> <!-- Reports Section (placeholder) --> <div id="reports-section" class="page-section"> <div class="page-header"> <div> <h1 class="page-title">Reports & Analytics</h1> <p class="page-subtitle">View energy consumption reports and analytics</p> </div> </div> <div class="card"> <div class="card-header"> <h3 class="card-title">ENERGY CONSUMPTION</h3> </div> <p style="text-align:center;padding:40px 0;">Consumption reports and charts will be displayed here</p> </div> </div> <!-- Help Section (placeholder) --> <div id="help-section" class="page-section"> <div class="page-header"> <div> <h1 class="page-title">Help & Support</h1> <p class="page-subtitle">Get help with using your prepaid energy monitor</p> </div> </div> <div class="card"> <div class="card-header"> <h3 class="card-title">SUPPORT INFORMATION</h3> </div> <p style="text-align:center;padding:40px 0;">Help and support information will be displayed here</p> </div> </div> <div class="dashboard-footer"> <p id="update-status">Last Update: --</p> <p>Prepaid Energy Monitor Pro © 2025</p> </div> </div> </div> <script> let relayState = false; // Track relay state on client-side // Show selected page section function showPage(page) { // Hide all page sections const pageSections = document.querySelectorAll('.page-section'); pageSections.forEach(section => { section.classList.remove('active'); }); // Show selected page section document.getElementById(page + '-section').classList.add('active'); // Update active nav link const navLinks = document.querySelectorAll('.nav-link'); navLinks.forEach(link => { link.classList.remove('active'); }); document.querySelector(`.nav-link[href="#${page}"]`).classList.add('active'); // Update content for specific pages if (page === 'account') { const dashboardBalance = document.getElementById('balance').textContent; document.getElementById('account-balance').textContent = dashboardBalance; } } function updateValues() { fetch('/data') .then(response => response.json()) .then(data => { // Dashboard Section Updates updateDashboardValues(data); updateSystemStatus(data); updatePowerControlUI(data.relayState); // Account Section Updates updateAccountSection(data); // Control Section Updates updateControlSection(data); // Update timestamp const now = new Date(); document.getElementById('update-timestamp').textContent = `Last updated: ${now.toLocaleTimeString()}`; document.getElementById('update-status').textContent = `Last Update: ${now.toLocaleTimeString()}`; }) .catch(error => { console.error('Error fetching data:', error); showAlert('Connection error. Retrying...', 'warning', 'main-alert'); document.getElementById('update-status').textContent = 'Update Failed'; }); } function updateDashboardValues(data) { updateElement('balance', data.balance.toFixed(2) + '<span class="unit">₹</span>'); updateElement('voltage', data.voltage.toFixed(1) + '<span class="unit">V</span>'); updateElement('current', data.current.toFixed(3) + '<span class="unit">A</span>'); updateElement('power', data.power.toFixed(1) + '<span class="unit">W</span>'); updateElement('energy-value', data.energy.toFixed(3) + ' kWh'); } function updateSystemStatus(data) { updateStatusIndicator('power-indicator', data.relayState ? 'status-active' : 'status-inactive'); updateElement('power-status', data.relayState ? 'ON' : 'OFF'); updateStatusIndicator('fault-indicator', data.faultDetected ? 'status-danger' : 'status-active'); updateElement('fault-status', data.faultDetected ? 'FAULT' : 'OK'); updateStatusIndicator('theft-indicator', data.theftDetected ? 'status-warning' : 'status-active'); updateElement('theft-status', data.theftDetected ? 'DETECTED' : 'OK'); } function updatePowerControlUI(serverRelayState) { relayState = serverRelayState; const onButtonDashboard = document.getElementById('power-on-btn'); const offButtonDashboard = document.getElementById('power-off-btn'); const onButtonControl = document.getElementById('control-power-on-btn'); const offButtonControl = document.getElementById('control-power-off-btn'); if (relayState) { onButtonDashboard.disabled = true; offButtonDashboard.disabled = false; onButtonControl.disabled = true; offButtonControl.disabled = false; } else { onButtonDashboard.disabled = false; offButtonDashboard.disabled = true; onButtonControl.disabled = false; offButtonControl.disabled = true; } const relayStatusText = relayState ? 'ON' : 'OFF'; updateElement('relay-control-status', relayStatusText); } function updateAccountSection(data) { updateElement('account-balance', data.balance.toFixed(2) + '<span class="unit">₹</span>'); // Example consumption rate calculation (needs actual logic) const consumptionRate = (data.energy * data.costPerKWh).toFixed(2); // Example updateElement('consumption-rate', consumptionRate + '<span class="unit">₹/day</span>'); // Needs proper daily calculation } function updateControlSection(data) { updateElement('relay-control-status', data.relayState ? 'ON' : 'OFF'); updateStatusIndicator('control-voltage-indicator', (data.voltage < 180 || data.voltage > 250) ? 'status-warning' : 'status-active'); updateElement('control-voltage-status', (data.voltage < 180) ? 'LOW' : (data.voltage > 250 ? 'HIGH' : 'OK')); updateStatusIndicator('control-current-indicator', (data.current > data.overCurrent) ? 'status-warning' : 'status-active'); updateElement('control-current-status', (data.current > data.overCurrent) ? 'OVER' : 'OK'); updateStatusIndicator('control-fault-indicator', data.faultDetected ? 'status-danger' : 'status-active'); updateElement('control-fault-status', data.faultDetected ? 'FAULT' : 'OK'); updateStatusIndicator('control-theft-indicator', data.theftDetected ? 'status-warning' : 'status-active'); updateElement('control-theft-status', data.theftDetected ? 'DETECTED' : 'OK'); updatePowerControlUI(data.relayState); // Keep control section buttons in sync } function updateElement(id, html) { const element = document.getElementById(id); if (element) { element.innerHTML = html; } } function updateStatusIndicator(id, className) { const indicator = document.getElementById(id); if (indicator) { indicator.className = 'status-indicator ' + className; } } function setRelay(state) { const onButtonDashboard = document.getElementById('power-on-btn'); const offButtonDashboard = document.getElementById('power-off-btn'); const onButtonControl = document.getElementById('control-power-on-btn'); const offButtonControl = document.getElementById('control-power-off-btn'); const alertElementDashboard = document.getElementById('power-control-alert'); const alertElementControl = document.getElementById('relay-control-alert'); // Disable buttons to prevent multiple clicks onButtonDashboard.disabled = true; offButtonDashboard.disabled = true; onButtonControl.disabled = true; offButtonControl.disabled = true; const alertElement = document.querySelector('.page-section.active').id === 'dashboard-section' ? alertElementDashboard : alertElementControl; showAlert('Processing power control...', 'warning', alertElement.id); let overrideState = 0; // Assume no override if (state === 0) { overrideState = 1; // Set override if turning off } else { overrideState = 0; // Clear override if turning on } fetch(`/setRelay?state=${state}&override=${overrideState}`, { method: 'POST' }) .then(response => response.json()) .then(data => { if (data.success) { updatePowerControlUI(state === 1); // Update UI based on requested state showAlert(`Power turned ${state === 1 ? 'ON' : 'OFF'} successfully.`, 'success', alertElement.id); } else { showAlert('Power control failed: ' + (data.message || 'Unknown error'), 'danger', alertElement.id); updatePowerControlUI(relayState); // Revert UI to previous state if failed } // Re-enable buttons after request completes (or fails) onButtonDashboard.disabled = false; offButtonDashboard.disabled = false; onButtonControl.disabled = false; offButtonControl.disabled = false; }) .catch(error => { console.error('Error controlling relay:', error); showAlert('Relay control error', 'danger', alertElement.id); updatePowerControlUI(relayState); // Revert UI on error // Re-enable buttons even on error onButtonDashboard.disabled = false; offButtonDashboard.disabled = false; onButtonControl.disabled = false; offButtonControl.disabled = false; }); } function rechargeAccount() { const amount = document.getElementById('recharge-amount').value; const rechargeAlert = document.getElementById('recharge-alert'); const rechargeButton = document.querySelector('#recharge-alert + button.btn-primary, #account-section button.btn-primary'); rechargeButton.disabled = true; // Disable recharge button during request showAlert('Processing recharge...', 'warning', 'recharge-alert'); fetch(`/recharge?amount=${amount}`, { method: 'POST' }) .then(response => response.json()) .then(data => { if (data.success) { showAlert(`Recharge successful. New balance: ₹${data.balance.toFixed(2)}`, 'success', 'recharge-alert'); updateElement('balance', data.balance.toFixed(2) + '<span class="unit">₹</span>'); updateElement('account-balance', data.balance.toFixed(2) + '<span class="unit">₹</span>'); } else { showAlert('Recharge failed. ' + (data.message || 'Please try again.'), 'danger', 'recharge-alert'); } rechargeButton.disabled = false; // Re-enable button after request completes }) .catch(error => { console.error('Recharge error:', error); showAlert('Recharge error. Please try again.', 'danger', 'recharge-alert'); rechargeButton.disabled = false; // Re-enable button even on error }); } function quickRecharge() { const amount = document.getElementById('quick-recharge-amount').value; const rechargeAlert = document.getElementById('recharge-alert'); const rechargeButton = document.querySelector('#dashboard-section .action-card button.btn-primary'); rechargeButton.disabled = true; showAlert('Processing recharge...', 'warning', 'power-control-alert'); fetch(`/recharge?amount=${amount}`, { method: 'POST' }) .then(response => response.json()) .then(data => { if (data.success) { showAlert(`Recharge successful. New balance: ₹${data.balance.toFixed(2)}`, 'success', 'power-control-alert'); updateElement('balance', data.balance.toFixed(2) + '<span class="unit">₹</span>'); updateElement('account-balance', data.balance.toFixed(2) + '<span class="unit">₹</span>'); } else { showAlert('Recharge failed. ' + (data.message || 'Please try again.'), 'danger', 'power-control-alert'); } rechargeButton.disabled = false; }) .catch(error => { console.error('Recharge error:', error); showAlert('Recharge error. Please try again.', 'danger', 'power-control-alert'); rechargeButton.disabled = false; }); } function showAlert(message, type, alertElementId) { const alertDiv = document.getElementById(alertElementId); if(alertDiv) { alertDiv.textContent = message; alertDiv.className = `alert alert-${type}`; alertDiv.style.display = 'block'; setTimeout(() => { alertDiv.style.display = 'none'; }, 5000); } } function setThresholds() { const form = document.getElementById('threshold-form'); const formData = new FormData(form); // Use FormData to easily collect form data const thresholdAlert = document.getElementById('threshold-alert'); const saveButton = document.querySelector('#threshold-form button.btn-primary'); // Select save button const data = {}; formData.forEach((value, key) => { data[key] = parseFloat(value); // Convert string values from form to floats }); saveButton.disabled = true; // Disable save button during request showAlert('Saving settings...', 'warning', 'threshold-alert'); fetch('/setThresholds', { method: 'POST', headers: { 'Content-Type': 'application/json' // Set content type to JSON }, body: JSON.stringify(data) // Send form data as JSON }) .then(response => response.json()) .then(responseData => { if (responseData.success) { showAlert('Settings updated successfully.', 'success', 'threshold-alert'); // Optionally update displayed threshold values on the page if you wish } else { showAlert('Failed to update settings: ' + (responseData.message || 'Unknown error'), 'danger', 'threshold-alert'); } saveButton.disabled = false; // Re-enable button after request completes }) .catch(error => { console.error('Error setting thresholds:', error); showAlert('Error updating settings. Please try again.', 'danger', 'threshold-alert'); saveButton.disabled = false; // Re-enable button even on error }); } function factoryReset() { const resetAlert = document.getElementById('factory-reset-alert'); const resetButton = document.querySelector('#settings-section button.btn-danger'); resetButton.disabled = true; showAlert('Performing factory reset...', 'warning', 'factory-reset-alert'); fetch('/factoryReset', { method: 'POST' }) .then(response => response.json()) .then(data => { if (data.success) { showAlert('Factory reset successful. System restarting...', 'success', 'factory-reset-alert'); setTimeout(() => { window.location.href = '/login'; // Redirect to login page after reset }, 3000); // Wait 3 seconds before redirecting and restarting ESP } else { showAlert('Factory reset failed: ' + (data.message || 'Unknown error'), 'danger', 'factory-reset-alert'); } resetButton.disabled = false; }) .catch(error => { console.error('Factory reset error:', error); showAlert('Factory reset error. Please try again.', 'danger', 'factory-reset-alert'); resetButton.disabled = false; }); } // Initial update and set interval updateValues(); setInterval(updateValues, 3000); // Update every 3 seconds // Set initial page to dashboard showPage('dashboard'); </script> </body> </html> )rawliteral"; // Login page HTML (No changes needed unless further UI/UX improvements are desired) const char login_html[] PROGMEM = R"rawliteral( <!DOCTYPE HTML> <html lang="en"> <head> <title>Login - ESP32 Prepaid Energy Monitor</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"> <style> :root { --primary-color: #3498db; --secondary-color: #2c3e50; --background-color: #f5f7fa; --card-background: #ffffff; --text-color: #333333; --text-light: #7f8c8d; --shadow: 0 4px 6px rgba(0, 0, 0, 0.1); --transition: all 0.3s ease; } body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; background-color: var(--background-color); margin: 0; padding: 0; color: var(--text-color); line-height: 1.6; } .login-container { max-width: 400px; margin: 100px auto; background: var(--card-background); padding: 30px; border-radius: 8px; box-shadow: var(--shadow); } .login-container h2 { text-align: center; color: var(--secondary-color); margin-bottom: 30px; display: flex; align-items: center; justify-content: center; } .login-container h2 i { margin-right: 10px; color: var(--primary-color); } .form-group { margin-bottom: 20px; } .form-group label { display: block; margin-bottom: 5px; font-weight: bold; color: var(--secondary-color); } .form-group input { width: 100%; padding: 10px; border: 1px solid #ddd; border-radius: 4px; font-size: 16px; } .button { background: linear-gradient(to right, var(--primary-color), var(--secondary-color)); color: white; border: none; padding: 12px 24px; border-radius: 4px; cursor: pointer; font-size: 16px; font-weight: bold; transition: var(--transition); display: inline-flex; align-items: center; width: 100%; justify-content: center; } .button i { margin-right: 8px; } .button:hover { transform: translateY(-2px); box-shadow: 0 6px 12px rgba(0, 0, 0, 0.15); } .alert { padding: 10px 15px; border-radius: 4px; margin-bottom: 15px; font-size: 14px; } .alert-danger { background-color: #f8d7da; color: #721c24; border: 1px solid #f5c6cb; } </style> </head> <body> <div class="login-container"> <h2><i class="fas fa-lock"></i> Login</h2> <div id="error-message" class="alert alert-danger" style="display: none;"> Invalid username or password </div> <form action="/login" method="post"> <div class="form-group"> <label for="username">Username</label> <input type="text" id="username" name="username" required> </div> <div class="form-group"> <label for="password">Password</label> <input type="password" id="password" name="password" required> </div> <button type="submit" class="button"> <i class="fas fa-sign-in-alt"></i> Login </button> </form> </div> <script>// Check for error parameter in URL const urlParams = new URLSearchParams(window.location.search); if (urlParams.has('error')) { document.getElementById('error-message').style.display = 'block'; } </script> </body> </html> )rawliteral"; void setup() { // Initialize serial communication Serial.begin(115200); delay(500); // Initialize EEPROM EEPROM.begin(EEPROM_SIZE); // Initialize relay pin pinMode(RELAY_PIN, OUTPUT); digitalWrite(RELAY_PIN, relayState ? HIGH : LOW); // Initialize Button Pin pinMode(BUTTON_PIN, INPUT_PULLUP); // Initialize TFT display tft.initR(INITR_BLACKTAB); tft.setRotation(1); // Adjust rotation if needed for your display tft.fillScreen(BLACK); tft.setTextWrap(false); // Display startup sequence displayStartupSequence(); // Load saved balance and thresholds from EEPROM loadBalance(); loadThresholds(); // Configure WiFi (Simplified) configureWiFi(); // Store IP address as string if (WiFi.status() == WL_CONNECTED) { ipAddressStr = WiFi.localIP().toString(); showNotification("IP: " + ipAddressStr, GREEN, 5000); } else { showNotification("WiFi Failed to connect", RED, 5000); } // Set up web server routes server.on("/", HTTP_GET, handleRoot); server.on("/login", HTTP_GET, [](){ server.send(200, "text/html", login_html); }); server.on("/login", HTTP_POST, handleLogin); server.on("/data", HTTP_GET, handleData); server.on("/recharge", HTTP_POST, handleRecharge); server.on("/reset", HTTP_POST, handleReset); server.on("/setRelay", HTTP_POST, handleSetRelay); server.on("/setThresholds", HTTP_POST, handleSetThresholds); // New handler for thresholds server.on("/factoryReset", HTTP_POST, handleFactoryReset); // New handler for factory reset server.onNotFound(handleNotFound); // Start web server server.begin(); // Show notification showNotification("System Ready", GREEN, 3000); } void loop() { // Handle web server client requests server.handleClient(); // Read PZEM data at specified intervals if (millis() - lastReadTime >= measurementInterval) { readPzemData(); lastReadTime = millis(); // Check for faults and theft checkFaultsAndTheft(); // Control relay controlRelay(); } // Update balance based on energy usage if (millis() - balanceUpdateTime >= BALANCE_UPDATE_INTERVAL) { updateBalance(); balanceUpdateTime = millis(); saveBalance(); } // **Button Handling for Page Change and Manual Relay Override** int reading = digitalRead(BUTTON_PIN); bool currentButtonState = (reading == LOW); // Button is active low if (currentButtonState && !lastButtonState) { // Button pressed if (millis() - lastButtonPress > debounceDelay) { lastButtonPress = millis(); buttonPressStartTime = millis(); manualRelayControlActive = true; // Start checking for long press } } else if (!currentButtonState && lastButtonState) { // Button released if (manualRelayControlActive) { if (millis() - buttonPressStartTime >= 1000) { // Long press detected (3 seconds) manualOverride = !manualOverride; // Toggle manual override state relayState = !relayState; // Toggle relay state digitalWrite(RELAY_PIN, relayState ? LOW : HIGH); showNotification(relayState ? "Manual Relay ON" : "Manual Relay OFF", YELLOW); // Web server will reflect state on next /data request } else { displayPage = (displayPage + 1) % 2; // Toggle page on short press pageChangeTime = millis(); layoutDrawn = false; // Redraw layout for new page if needed } manualRelayControlActive = false; } } lastButtonState = currentButtonState; // Update display with page switching if (millis() - screenRefreshTime >= SCREEN_REFRESH_INTERVAL) { screenRefreshTime = millis(); if (!layoutDrawn) { drawLayout(); // Draw common layout elements (title, separators) - Draw only once per page change layoutDrawn = true; } if (millis() - pageChangeTime >= PAGE_INTERVAL) { // Automatic page switch removed due to button implementation, keep it on current page until button press // displayPage = (displayPage + 1) % 2; // Switch between 2 pages (0 and 1) - Removed Automatic switching pageChangeTime = millis(); //reset page change timer, even if not auto-switching now } updateDisplayValues(); // Update dynamic values on the current page - Update only values, not full page redraw } // Clear notification if display time has elapsed if (notificationEndTime > 0 && millis() >= notificationEndTime) { clearNotification(); } // Periodic notifications every 5 minutes if (millis() - lastNotificationTime >= NOTIFICATION_INTERVAL) { if (faultDetected) { showNotification("Fault Detected! Check System.", RED); } else if (theftDetected) { showNotification("Theft Detected! Investigate.", RED); } else if (balance <= minimumBalance) { // Use variable threshold showNotification("Low Balance! Recharge Soon.", YELLOW); } else if (!pzemConnected) { showNotification("PZEM Disconnected!", RED); } else if (WiFi.status() != WL_CONNECTED) { showNotification("WiFi Disconnected!", RED); } // Add other conditions for different types of notifications as needed. lastNotificationTime = millis(); } // Check WiFi connection periodically and attempt to reconnect checkWiFiConnection(); } // Function to check WiFi connection and reconnect if necessary void checkWiFiConnection() { if (millis() - lastWifiCheckTime >= WIFI_RECHECK_INTERVAL) { lastWifiCheckTime = millis(); if (WiFi.status() != WL_CONNECTED) { Serial.println("WiFi Disconnected. Attempting reconnection..."); WiFi.reconnect(); showNotification("WiFi Disconnected! Reconnecting...", YELLOW); // Optionally add a timeout for reconnection attempts to avoid indefinite blocking } else if (WiFi.status() == WL_CONNECTED && currentNotification.startsWith("WiFi Disconnected!")) { showNotification("WiFi Reconnected!", GREEN); } } } // Read data from PZEM sensor void readPzemData() { readings.voltage = pzem.voltage(); readings.current = pzem.current(); readings.power = pzem.power(); readings.energy = pzem.energy(); // Determine if reading is valid (voltage is used as indicator) readings.isValid = !(isnan(readings.voltage) || readings.voltage < 1.0); // Update connection status bool newConnectionStatus = readings.isValid; if (pzemConnected != newConnectionStatus) { pzemConnected = newConnectionStatus; if (pzemConnected) { showNotification("PZEM Connected", GREEN); } else { showNotification("PZEM Disconnected", RED); } } // Debug output printDebugInfo("PZEM Reading", readings.isValid); } // Draw common layout elements (Title, separators, static labels) - Reduced redraw to title and separator only void drawLayout() { tft.fillScreen(BLACK); // Clear the whole screen only on page change for clean transition tft.setTextSize(1); // Title Bar - Compact for 1.8 inch display setTextProperties(tft, YELLOW); tft.setCursor(2, 2); tft.print("ENERGY METER"); tft.drawFastHLine(0, 14, tft.width(), BLUE); // Header bar background // Page Content Area (below title bar) if (displayPage == 0) { drawLiveDataPage(); // Draw Live Data Page elements } else if (displayPage == 1) { drawAlertsPage(); // Draw Alerts Page elements } layoutDrawn = true; // Ensure layout is marked as drawn after drawing page specific elements } // Constants for layout spacing and text positions - Optimized for 1.8 inch display const int HEADER_HEIGHT = 14; const int BOX_HEIGHT = 30; const int BOX_ROUND_RADIUS = 3; const int BOX_VERTICAL_START = HEADER_HEIGHT + 3; const int ROW_SPACING = BOX_HEIGHT + 2; const int LABEL_TEXT_Y_OFFSET = 3; const int VALUE_TEXT_Y_OFFSET = 18; const int FOOTER_AREA_HEIGHT = 20; const int FOOTER_TEXT_Y_OFFSET = 6; // Draw Live Data Page - Improved Layout with constants void drawLiveDataPage() { // Draw header bar tft.fillRect(0, 0, tft.width(), HEADER_HEIGHT, BLUE); tft.setTextSize(1); setTextProperties(tft, WHITE); tft.setCursor(5, 4); tft.print("ESP32 PREPAID METER"); // Row 1: Voltage & Current tft.fillRoundRect(2, BOX_VERTICAL_START + (ROW_SPACING * 0), tft.width() - 4, BOX_HEIGHT, BOX_ROUND_RADIUS, DARKGRAY); tft.drawRoundRect(2, BOX_VERTICAL_START + (ROW_SPACING * 0), tft.width() - 4, BOX_HEIGHT, BOX_ROUND_RADIUS, WHITE); // Row 2: Power & Energy tft.fillRoundRect(2, BOX_VERTICAL_START + (ROW_SPACING * 1), tft.width() - 4, BOX_HEIGHT, BOX_ROUND_RADIUS, DARKGRAY); tft.drawRoundRect(2, BOX_VERTICAL_START + (ROW_SPACING * 1), tft.width() - 4, BOX_HEIGHT, BOX_ROUND_RADIUS, WHITE); // Row 3: Balance & Status (Replaced Frequency & PF) tft.fillRoundRect(2, BOX_VERTICAL_START + (ROW_SPACING * 2), tft.width() - 4, BOX_HEIGHT, BOX_ROUND_RADIUS, DARKGRAY); tft.drawRoundRect(2, BOX_VERTICAL_START + (ROW_SPACING * 2), tft.width() - 4, BOX_HEIGHT, BOX_ROUND_RADIUS, WHITE); // Draw labels for each row tft.setTextSize(1); setTextProperties(tft, CYAN); // Row 1 labels tft.setCursor(5, BOX_VERTICAL_START + (ROW_SPACING * 0) + LABEL_TEXT_Y_OFFSET); tft.print("VOLTAGE"); tft.setCursor(tft.width() / 2 + 5, BOX_VERTICAL_START + (ROW_SPACING * 0) + LABEL_TEXT_Y_OFFSET); tft.print("CURRENT"); // Row 2 labels tft.setCursor(5, BOX_VERTICAL_START + (ROW_SPACING * 1) + LABEL_TEXT_Y_OFFSET); tft.print("POWER"); tft.setCursor(tft.width() / 2 + 5, BOX_VERTICAL_START + (ROW_SPACING * 1) + LABEL_TEXT_Y_OFFSET); tft.print("ENERGY"); // Row 3 labels (Balance & Status) tft.setCursor(5, BOX_VERTICAL_START + (ROW_SPACING * 2) + LABEL_TEXT_Y_OFFSET); tft.print("BALANCE"); tft.setCursor(tft.width() / 2 + 5, BOX_VERTICAL_START + (ROW_SPACING * 2) + LABEL_TEXT_Y_OFFSET); tft.print("STATUS"); // Draw notification area and IP address at bottom tft.drawRect(2, 113, tft.width() - 4, FOOTER_AREA_HEIGHT, WHITE); // Display IP address in footer tft.setTextSize(1); setTextProperties(tft, GREEN); tft.setCursor(5, 113 + FOOTER_TEXT_Y_OFFSET); tft.print("IP: "); // Use stored IP address string if (WiFi.status() == WL_CONNECTED) { tft.print(ipAddressStr); } else { setTextProperties(tft, RED); tft.print("Not Connected"); setTextProperties(tft, GREEN); // Reset color for IP: prefix for next redraw } // Reset all text buffers when drawing a new page memset(lastVoltageText, 0, sizeof(lastVoltageText)); memset(lastCurrentText, 0, sizeof(lastCurrentText)); memset(lastPowerText, 0, sizeof(lastPowerText)); memset(lastEnergyText, 0, sizeof(lastEnergyText)); memset(lastBalanceText, 0, sizeof(lastBalanceText)); memset(lastStatusText, 0, sizeof(lastStatusText)); } void drawAlertsPage() { tft.fillScreen(BLACK); tft.setTextSize(2); setTextProperties(tft, YELLOW); tft.setCursor(5, 10); tft.print("ALERTS"); tft.drawFastHLine(0, 30, tft.width(), DARKGRAY); setTextProperties(tft, CYAN); int y_offset = 40; tft.setTextSize(1); setTextProperties(tft, CYAN); // Set color for labels on Alerts page tft.setCursor(5, 40); tft.print("Over Voltage Limit:"); tft.setCursor(5, 55); tft.print("Over Current Limit:"); tft.setCursor(5, 70); tft.print("Theft Current:"); tft.setCursor(5, 85); tft.print("Min Balance:"); // Footer with smaller text tft.setTextSize(1); setTextProperties(tft, DARKGRAY); tft.setCursor(5, tft.height() - 10); tft.print("2/2"); // Reset alert text buffers memset(lastFaultText, 0, sizeof(lastFaultText)); memset(lastTheftText, 0, sizeof(lastTheftText)); } void updateDisplayValues() { if (displayPage == 0) { // Live Data Page int x_value_start_left = 5; int x_value_start_right = tft.width() / 2 + 5; char newText[20]; // Increased buffer size for values with units tft.setTextSize(1); setTextProperties(tft, WHITE); // Row 1: Voltage tft.fillRect(x_value_start_left, BOX_VERTICAL_START + (ROW_SPACING * 0) + VALUE_TEXT_Y_OFFSET, tft.width()/2 - 10, BOX_HEIGHT - VALUE_TEXT_Y_OFFSET - 2, BLACK); tft.setCursor(x_value_start_left, BOX_VERTICAL_START + (ROW_SPACING * 0) + VALUE_TEXT_Y_OFFSET); if (readings.isValid) { sprintf(newText, "%.1fV", readings.voltage); tft.print(newText); strcpy(lastVoltageText, newText); } else { tft.print("0.0V"); // Display 0.0V when no reading strcpy(lastVoltageText, "0.0V"); } // Row 1: Current tft.fillRect(x_value_start_right, BOX_VERTICAL_START + (ROW_SPACING * 0) + VALUE_TEXT_Y_OFFSET, tft.width()/2 - 10, BOX_HEIGHT - VALUE_TEXT_Y_OFFSET - 2, BLACK); tft.setCursor(x_value_start_right, BOX_VERTICAL_START + (ROW_SPACING * 0) + VALUE_TEXT_Y_OFFSET); if (readings.isValid) { sprintf(newText, "%.3fA", readings.current); tft.print(newText); strcpy(lastCurrentText, newText); } else { tft.print("0.000A"); // Display 0.000A when no reading strcpy(lastCurrentText, "0.000A"); } // Row 2: Power tft.fillRect(x_value_start_left, BOX_VERTICAL_START + (ROW_SPACING * 1) + VALUE_TEXT_Y_OFFSET, tft.width()/2 - 10, BOX_HEIGHT - VALUE_TEXT_Y_OFFSET - 2, BLACK); tft.setCursor(x_value_start_left, BOX_VERTICAL_START + (ROW_SPACING * 1) + VALUE_TEXT_Y_OFFSET); if (readings.isValid) { sprintf(newText, "%.1fW", readings.power); tft.print(newText); strcpy(lastPowerText, newText); } else { tft.print("0.0W"); // Display 0.0W when no reading strcpy(lastPowerText, "0.0W"); } // Row 2: Energy tft.fillRect(x_value_start_right, BOX_VERTICAL_START + (ROW_SPACING * 1) + VALUE_TEXT_Y_OFFSET, tft.width()/2 - 10, BOX_HEIGHT - VALUE_TEXT_Y_OFFSET - 2, BLACK); tft.setCursor(x_value_start_right, BOX_VERTICAL_START + (ROW_SPACING * 1) + VALUE_TEXT_Y_OFFSET); if (readings.isValid) { sprintf(newText, "%.2fkWh", readings.energy); tft.print(newText); strcpy(lastEnergyText, newText); } else { tft.print("0.00kWh"); // Display 0.00kWh when no reading strcpy(lastEnergyText, "0.00kWh"); } // Row 3: Balance tft.fillRect(x_value_start_left, BOX_VERTICAL_START + (ROW_SPACING * 2) + VALUE_TEXT_Y_OFFSET, tft.width()/2 - 10, BOX_HEIGHT - VALUE_TEXT_Y_OFFSET - 2, BLACK); tft.setCursor(x_value_start_left, BOX_VERTICAL_START + (ROW_SPACING * 2) + VALUE_TEXT_Y_OFFSET); sprintf(newText, "%.2f Rs", balance); tft.print(newText); strcpy(lastBalanceText, newText); // Row 3: Status tft.fillRect(x_value_start_right, BOX_VERTICAL_START + (ROW_SPACING * 2) + VALUE_TEXT_Y_OFFSET, tft.width()/2 - 10, BOX_HEIGHT - VALUE_TEXT_Y_OFFSET - 2, BLACK); tft.setCursor(x_value_start_right, BOX_VERTICAL_START + (ROW_SPACING * 2) + VALUE_TEXT_Y_OFFSET); if (balance <= minimumBalance) { setTextProperties(tft, RED); strcpy(newText, "LOW BAL"); } else if (faultDetected) { setTextProperties(tft, RED); strcpy(newText, "FAULT"); } else if (theftDetected) { setTextProperties(tft, ORANGE); // Use Orange for theft strcpy(newText, "THEFT"); } else if (!pzemConnected) { setTextProperties(tft, RED); strcpy(newText, "PZEM OFF"); } else if (relayState) { setTextProperties(tft, GREEN); strcpy(newText, "ACTIVE"); } else { setTextProperties(tft, DARKGRAY); // Use dark gray for OFF status strcpy(newText, "OFF"); } tft.print(newText); strcpy(lastStatusText, newText); setTextProperties(tft, WHITE); // Reset text color to white for other values } else if (displayPage == 1) { // Alerts Page tft.setTextSize(1); setTextProperties(tft, WHITE); int x_value_start = 130; // Adjusted x position for values on alerts page char newText[20]; setTextProperties(tft, WHITE); // Set color for threshold values on Alerts page tft.fillRect(x_value_start, 40, tft.width() - x_value_start - 5, 8, BLACK); tft.setCursor(x_value_start, 40); sprintf(newText, "%.1fV", overVoltageThreshold); tft.print(newText); tft.fillRect(x_value_start, 55, tft.width() - x_value_start - 5, 8, BLACK); tft.setCursor(x_value_start, 55); sprintf(newText, "%.2fA", overCurrentThreshold); tft.print(newText); tft.fillRect(x_value_start, 70, tft.width() - x_value_start - 5, 8, BLACK); tft.setCursor(x_value_start, 70); sprintf(newText, "%.3fA", theftCurrentThreshold); tft.print(newText); tft.fillRect(x_value_start, 85, tft.width() - x_value_start - 5, 8, BLACK); tft.setCursor(x_value_start, 85); sprintf(newText, "%.2f Rs", minimumBalance); tft.print(newText); } // Notification area - Moved higher for better visibility static unsigned long lastNotificationUpdate = 0; static char lastNotificationText[30] = ""; int notificationY = 113 + FOOTER_TEXT_Y_OFFSET; // Adjusted notification Y if (notificationEndTime > 0 && millis() < notificationEndTime) { if (strcmp(currentNotification.c_str(), lastNotificationText) != 0 || millis() - lastNotificationUpdate > 500) { tft.fillRect(2, 113, tft.width() - 4, FOOTER_AREA_HEIGHT, BLACK); // Clear footer area tft.setTextSize(1); setTextProperties(tft, notificationColor); tft.setCursor(2, notificationY); tft.print(currentNotification); strcpy(lastNotificationText, currentNotification.c_str()); lastNotificationUpdate = millis(); } } else if (notificationEndTime > 0 && millis() >= notificationEndTime) { // Clear notification when expired tft.fillRect(2, 113, tft.width() - 4, FOOTER_AREA_HEIGHT, BLACK); // Clear footer area notificationEndTime = 0; lastNotificationText[0] = '\0'; // Redraw IP address after clearing notification setTextProperties(tft, GREEN); tft.setCursor(5, 113 + FOOTER_TEXT_Y_OFFSET); tft.print("IP: "); if (WiFi.status() == WL_CONNECTED) { tft.print(ipAddressStr); } else { setTextProperties(tft, RED); tft.print("Not Connected"); setTextProperties(tft, GREEN); // Reset color for IP: prefix for next redraw } } } void showNotification(const String& message, uint16_t color, unsigned long duration) { currentNotification = message; notificationColor = color; notificationEndTime = millis() + duration; // Notification area positioned higher int notificationY = 113 + FOOTER_TEXT_Y_OFFSET; tft.fillRect(2, 113, tft.width() - 4, FOOTER_AREA_HEIGHT, BLACK); // Clear footer area tft.setTextSize(1); setTextProperties(tft, color); tft.setCursor(2, notificationY); tft.print(message); } void clearNotification() { // Clear the notification area at its new position tft.fillRect(2, 113, tft.width() - 4, FOOTER_AREA_HEIGHT, BLACK); notificationEndTime = 0; // Redraw IP address after clearing notification setTextProperties(tft, GREEN); tft.setCursor(5, 113 + FOOTER_TEXT_Y_OFFSET); tft.print("IP: "); if (WiFi.status() == WL_CONNECTED) { tft.print(ipAddressStr); } else { setTextProperties(tft, RED); tft.print("Not Connected"); setTextProperties(tft, GREEN); // Reset color for IP: prefix for next redraw } } void displayStartupSequence() { tft.fillScreen(BLACK); tft.setTextSize(2); // Larger text for startup setTextProperties(tft, GREEN); // Center the title text int titleX = (tft.width() - 12*11)/2; // Approximate centering for "ENERGY METER" tft.setCursor(titleX > 0 ? titleX : 0, 10); tft.print("ENERGY METER"); tft.setTextSize(1); // Smaller text for status messages setTextProperties(tft, WHITE); // Set color for status text to white tft.setCursor(5, 35); tft.print("Booting..."); // Progress bar sized appropriately for display int barWidth = tft.width() - 10; tft.drawRect(5, 45, barWidth, 8, WHITE); for (int i = 0; i < barWidth - 4; i += 2) { tft.fillRect(7, 47, i, 4, BLUE); delay(25); // Faster animation } // Status messages positioned better clearTextArea(5, 60, tft.width() - 10, 8); setTextProperties(tft, WHITE); // Ensure color is set before each text print tft.setCursor(5, 60); tft.print("Init sensors..."); delay(300); clearTextArea(5, 60, tft.width() - 10, 8); setTextProperties(tft, WHITE); tft.setCursor(5, 60); tft.print("Web server..."); delay(300); clearTextArea(5, 60, tft.width() - 10, 8); setTextProperties(tft, WHITE); tft.setCursor(5, 60); tft.print("System ready!"); delay(500); } void configureWiFi() { Serial.println("Connecting to WiFi..."); WiFi.begin(ssid, password); tft.fillScreen(BLACK); tft.setTextSize(1); // Smaller text to fit more info setTextProperties(tft, WHITE); tft.setCursor(5, 10); tft.print("Connecting WiFi..."); // Add a small animation during connection int dotCount = 0; int timeout = 0; while (WiFi.status() != WL_CONNECTED && timeout < 30) { delay(1000); Serial.print("."); // Clear animation line and redraw tft.fillRect(5, 25, tft.width() - 10, 8, BLACK); setTextProperties(tft, WHITE); // Set color before printing dots tft.setCursor(5, 25); for(int i = 0; i < dotCount; i++) { tft.print("."); } dotCount = (dotCount + 1) % 4; // Cycle between 0-3 dots timeout++; } if (WiFi.status() == WL_CONNECTED) { tft.fillRect(5, 25, tft.width() - 10, 16, BLACK); setTextProperties(tft, WHITE); tft.setCursor(5, 25); tft.print("Connected!"); tft.setCursor(5, 40); tft.print(WiFi.localIP().toString()); Serial.println("\nWiFi Connected!"); Serial.print("IP address: "); Serial.println(WiFi.localIP()); } else { tft.fillRect(5, 25, tft.width() - 10, 8, BLACK); setTextProperties(tft, RED); tft.setCursor(5, 25); tft.print("Connection Failed!"); Serial.println("\nWiFi Connection Failed!"); } delay(1500); // Show the result briefly } // Save balance to EEPROM - No changes needed, looks good void saveBalance() { EEPROM.writeFloat(BALANCE_ADDR, balance); EEPROM.writeFloat(ENERGY_ADDR, lastEnergy); EEPROM.commit(); printDebugInfo("Save Balance", true); } // Load balance from EEPROM - No changes needed, looks good void loadBalance() { balance = EEPROM.readFloat(BALANCE_ADDR); lastEnergy = EEPROM.readFloat(ENERGY_ADDR); // Check if values are valid (not NaN or negative) if (isnan(balance) || balance < 0) { balance = 0.0; } if (isnan(lastEnergy) || lastEnergy < 0) { lastEnergy = 0.0; } printDebugInfo("Load Balance", true); } // Save thresholds to EEPROM - No changes needed, looks good void saveThresholds() { EEPROM.writeFloat(OVER_VOLTAGE_THRESHOLD_ADDR, overVoltageThreshold); EEPROM.writeFloat(OVER_CURRENT_THRESHOLD_ADDR, overCurrentThreshold); EEPROM.writeFloat(THEFT_CURRENT_THRESHOLD_ADDR, theftCurrentThreshold); EEPROM.writeFloat(MINIMUM_BALANCE_ADDR, minimumBalance); EEPROM.writeFloat(COST_PER_KWH_ADDR, costPerKWh); EEPROM.commit(); printDebugInfo("Save Thresholds", true); } // Load thresholds from EEPROM - No changes needed, looks good void loadThresholds() { overVoltageThreshold = EEPROM.readFloat(OVER_VOLTAGE_THRESHOLD_ADDR); overCurrentThreshold = EEPROM.readFloat(OVER_CURRENT_THRESHOLD_ADDR); theftCurrentThreshold = EEPROM.readFloat(THEFT_CURRENT_THRESHOLD_ADDR); minimumBalance = EEPROM.readFloat(MINIMUM_BALANCE_ADDR); costPerKWh = EEPROM.readFloat(COST_PER_KWH_ADDR); // Corrected usage - making sure macro is spelled correctly // Check if values are valid (not NaN or negative/zero where appropriate) if (isnan(overVoltageThreshold) || overVoltageThreshold <= 0) { overVoltageThreshold = DEFAULT_OVER_VOLTAGE_THRESHOLD; } if (isnan(overCurrentThreshold) || overCurrentThreshold <= 0) { overCurrentThreshold = DEFAULT_OVER_CURRENT_THRESHOLD; } if (isnan(theftCurrentThreshold) || theftCurrentThreshold < 0) { theftCurrentThreshold = DEFAULT_THEFT_CURRENT_THRESHOLD; } if (isnan(minimumBalance) ) { // Minimum balance can be negative if you want to allow credit minimumBalance = DEFAULT_MINIMUM_BALANCE; } if (isnan(costPerKWh) || costPerKWh <= 0) { costPerKWh = DEFAULT_COST_PER_KWH; } printDebugInfo("Load Thresholds", true); } // Update balance based on energy consumption - No changes needed, looks good void updateBalance() { if (!readings.isValid) return; float energyUsed = readings.energy - lastEnergy; // If energy counter was reset or first reading if (energyUsed < 0) { energyUsed = readings.energy; } // Calculate cost float cost = energyUsed * costPerKWh; // Deduct from balance if relay is on and not manually overridden off if (relayState && !manualOverride && cost > 0) { balance -= cost; // Ensure balance doesn't go negative if (balance < 0) { balance = 0; } printDebugInfo("Balance Updated", true); } // Update last energy reading lastEnergy = readings.energy; } // Check for electrical faults and theft - No changes needed, looks good void checkFaultsAndTheft() { if (!readings.isValid) return; // Check for faults (over voltage or over current) bool newFaultState = (readings.voltage > overVoltageThreshold || readings.current > overCurrentThreshold); // Only show notification if fault state changes if (newFaultState != faultDetected) { faultDetected = newFaultState; if (faultDetected) { showNotification("Fault Detected!", RED); } else { showNotification("Fault Cleared", GREEN); } } // Check for theft (current flowing when relay is off) bool newTheftState = (!relayState && readings.current > theftCurrentThreshold); // Only show notification if theft state changes if (newTheftState != theftDetected) { theftDetected = newTheftState; if (theftDetected) { showNotification("Theft Detected!", RED); } else { showNotification("Theft Alert Cleared", GREEN); } } } // Control relay based on balance and faults - Modified to respect manual override void controlRelay() { if (manualOverride) return; // Do not auto-control relay if manually overridden bool shouldBeOn = (balance > minimumBalance && !faultDetected); // Only change relay state if needed if (relayState != shouldBeOn) { relayState = shouldBeOn; digitalWrite(RELAY_PIN, relayState ? LOW : HIGH); if (relayState) { showNotification("Power Enabled", GREEN); } else { showNotification("Power Disabled", RED); } } } // Print debug information to serial - No changes needed, looks good void printDebugInfo(const char* action, bool success) { Serial.print("["); Serial.print(action); Serial.print("] "); if (success) { Serial.println("Success"); } else { Serial.println("Failed"); } } // Web server handlers - No changes needed, looks good void handleRoot() { if (!server.authenticate(admin_username, admin_password)) { return server.requestAuthentication(); } server.send(200, "text/html", index_html); } void handleLogin() { if (server.method() != HTTP_POST) { server.send(405, "text/plain", "Method Not Allowed"); return; } if (server.arg("username") == admin_username && server.arg("password") == admin_password) { server.sendHeader("Location", "/"); server.send(303); } else { server.sendHeader("Location", "/login?error=1"); server.send(303); } } void handleRecharge() { if (!server.authenticate(admin_username, admin_password)) { server.send(401, "text/plain", "Unauthorized"); return; } if (server.method() != HTTP_POST) { server.send(405, "text/plain", "Method Not Allowed"); return; } if (server.hasArg("amount")) { float amount = server.arg("amount").toFloat(); if (amount > 0) { balance += amount; saveBalance(); // Create JSON response String response = "{\"success\":true,\"balance\":" + String(balance, 2) + "}"; server.send(200, "application/json", response); showNotification("Recharged: Rs" + String(amount, 2), GREEN); return; } } server.send(400, "application/json", "{\"success\":false,\"message\":\"Invalid amount\"}"); } void handleData() { if (!server.authenticate(admin_username, admin_password)) { server.send(401, "text/plain", "Unauthorized"); return; } // Create JSON document DynamicJsonDocument doc(1024); // Add readings doc["isValid"] = readings.isValid; doc["voltage"] = readings.voltage; doc["current"] = readings.current; doc["power"] = readings.power; doc["energy"] = readings.energy; // If reading is invalid, send 0 values for web dashboard to clear values if (!readings.isValid) { doc["voltage"] = 0.0; doc["current"] = 0.0; doc["power"] = 0.0; doc["energy"] = 0.0; } // Add status information doc["balance"] = balance; doc["relayState"] = relayState; doc["faultDetected"] = faultDetected; doc["theftDetected"] = theftDetected; doc["overVoltageThreshold"] = overVoltageThreshold; doc["overCurrentThreshold"] = overCurrentThreshold; doc["theftCurrentThreshold"] = theftCurrentThreshold; doc["minimumBalance"] = minimumBalance; doc["costPerKWh"] = costPerKWh; doc["manualOverride"] = manualOverride; // Send manual override status // Serialize JSON to string String response; serializeJson(doc, response); server.send(200, "application/json", response); } void handleReset() { if (!server.authenticate(admin_username, admin_password)) { server.send(401, "text/plain", "Unauthorized"); return; } if (server.method() != HTTP_POST) { server.send(405, "text/plain", "Method Not Allowed"); return; } // Reset energy counter on PZEM bool success = pzem.resetEnergy(); // Reset stored energy value lastEnergy = 0.0; saveBalance(); String response = "{\"success\":" + String(success ? "true" : "false") + "}"; server.send(200, "application/json", response); if (success) { showNotification("Energy Counter Reset", YELLOW); } else { showNotification("Reset Failed", RED); } } void handleSetRelay() { if (!server.authenticate(admin_username, admin_password)) { server.send(401, "text/plain", "Unauthorized"); return; } if (server.method() != HTTP_POST) { server.send(405, "text/plain", "Method Not Allowed"); return; } if (server.hasArg("state") && server.hasArg("override")) { int state = server.arg("state").toInt(); int override_manual = server.arg("override").toInt(); bool success = false; if (state == 0) { relayState = false; digitalWrite(RELAY_PIN, HIGH); success = true; showNotification("Power Off", RED); manualOverride = (override_manual == 1); // Set manual override based on parameter } else if (state == 1) { if (balance > minimumBalance && !faultDetected) { relayState = true; digitalWrite(RELAY_PIN, LOW); success = true; showNotification("Power On", GREEN); manualOverride = false; // Clear manual override when turned on manually or automatically } } String response = "{\"success\":" + String(success ? "true" : "false") + "}"; server.send(200, "application/json", response); return; } server.send(400, "application/json", "{\"success\":false,\"message\":\"Missing state parameter\"}"); } void handleSetThresholds() { if (!server.authenticate(admin_username, admin_password)) { server.send(401, "text/plain", "Unauthorized"); return; } if (server.method() != HTTP_POST) { server.send(405, "text/plain", "Method Not Allowed"); return; } DynamicJsonDocument doc(512); // Adjust size as needed DeserializationError error = deserializeJson(doc, server.arg("plain")); if (error) { Serial.print(F("deserializeJson() failed: ")); Serial.println(error.f_str()); server.send(400, "application/json", "{\"success\":false,\"message\":\"Invalid JSON\"}"); return; } if (doc.containsKey("overVoltage") && doc.containsKey("overCurrent") && doc.containsKey("theftCurrent") && doc.containsKey("minBalance") && doc.containsKey("costPerKWh")) { overVoltageThreshold = doc["overVoltage"].as<float>(); overCurrentThreshold = doc["overCurrent"].as<float>(); theftCurrentThreshold = doc["theftCurrent"].as<float>(); minimumBalance = doc["minBalance"].as<float>(); costPerKWh = doc["costPerKWh"].as<float>(); saveThresholds(); // Save new thresholds to EEPROM String response = "{\"success\":true}"; server.send(200, "application/json", response); showNotification("Thresholds Updated", GREEN); return; } else { server.send(400, "application/json", "{\"success\":false,\"message\":\"Missing parameters\"}"); return; } } void handleFactoryReset() { if (!server.authenticate(admin_username, admin_password)) { server.send(401, "text/plain", "Unauthorized"); return; } if (server.method() != HTTP_POST) { server.send(405, "text/plain", "Method Not Allowed"); return; } resetEEPROMToDefaults(); // Reset EEPROM values to defaults showNotification("Factory Resetting...", YELLOW, 2000); // Notify user on TFT delay(2000); // Short delay to show notification server.send(200, "application/json", "{\"success\":true}"); ESP.restart(); // Restart ESP32 to apply defaults immediately } void resetEEPROMToDefaults() { balance = DEFAULT_BALANCE; lastEnergy = DEFAULT_ENERGY; overVoltageThreshold = DEFAULT_OVER_VOLTAGE_THRESHOLD; overCurrentThreshold = DEFAULT_OVER_CURRENT_THRESHOLD; theftCurrentThreshold = DEFAULT_THEFT_CURRENT_THRESHOLD; minimumBalance = DEFAULT_MINIMUM_BALANCE; costPerKWh = DEFAULT_COST_PER_KWH; saveBalance(); saveThresholds(); // Optionally clear the entire EEPROM (be cautious, this is usually not needed and can reduce EEPROM life if done excessively) // for (int i = 0; i < EEPROM_SIZE; i++) { // EEPROM.write(i, 0); // } // EEPROM.commit(); printDebugInfo("EEPROM Reset to Defaults", true); } void handleNotFound() { server.send(404, "text/plain", "404: Not found"); } |
Verify and upload the code to your ESP32
Power Up and Test
After safely completing all wiring, power up the ESP32; the TFT screen should light up and show the startup sequence
After a successful connection, the system displays its IP address on the TFT screen and connects to that IP address from a browser connected to your WiFi.
Log in using the default credentials:
- Username: admin
- Password: admin123
Configure Your System
Through the web dashboard, you can:
- Add credit to your prepaid balance
- Set the cost per kWh to match your local electricity rates
- Configure safety thresholds:
-
- Over-voltage protection
- Over-current protection
- Theft detection sensitivity
- Minimum balance before cutoff
- Control the power relay manually when needed
How to Use Your Prepaid Energy Monitor
Local Display (TFT Screen)
- The TFT shows real-time voltage, current, power, energy, and balance
- Press the button to cycle through different information screens
- Long-press the button to toggle power ON/OFF manually
Web Dashboard
Access the web dashboard from any device on your network by entering the IP address shown on the TFT screen:
- Main Dashboard:
-
- View all electrical parameters in real time.
- See your remaining balance.
- Check system status indicators.
- Control Panel:
-
- Turn power ON/OFF
- Add credit to your balance
- View alert indicators
- Settings:
-
- Adjust all threshold values.
- Set electricity cost per kWh
- Perform factory reset if needed
Testing with Different Loads:
- No Load Testing: Connect the system without any appliance to verify baseline readings; the display shows near-zero current and power readings when no load is connected
- Small Load (2W Smart Bulb):
The display shows a small current draw and accurate power measurement with minimal Load.
Web dashboard
Medium Load (100W Bulb):
The display clearly shows increased current and power consumption with the 100W Load.
Long-press the button to toggle power ON/OFF manually.
Advanced Features
Fault Protection
The system automatically detects and protects against:
- Over-voltage conditions (configurable threshold)
- Over-current conditions (configurable threshold)
- Energy theft (detects current when the relay is OFF)
Stable Memory
Even after power outages, the system remembers:
- Your prepaid balance
- Accumulated energy usage
- All custom settings
Troubleshooting Tips
- No readings from PZEM: Check serial connections between ESP32 and PZEM
- TFT not displaying: Verify TFT connections and power
- Can’t connect to web dashboard: Check WiFi settings and connection
- Relay not switching: Verify relay connections and test relay functionality
- Balance decreasing too quickly: Verify your cost per kWh setting
1 Comment
I LOVE THE WAY YOU MAKE THE PROJECT ACCEPTABLE TO EVERYONE