|
| 1 | +#include <PxMatrix.h> |
| 2 | +#include <ESP8266WiFi.h> |
| 3 | +#include <PubSubClient.h> |
| 4 | +#include <Ticker.h> |
| 5 | +#include "arduino_secrets.h" |
| 6 | + |
| 7 | +// === CONFIGURATION ==== |
| 8 | +const char* ssid = SECRET_WIFI_SSID; |
| 9 | +const char* password = SECRET_WIFI_PASSWORD; |
| 10 | +const char* mqtt_server = SECRET_MQTT_SERVER; |
| 11 | +const char* mqtt_username = SECRET_MQTT_USER; |
| 12 | +const char* mqtt_password = SECRET_MQTT_PASSWORD; |
| 13 | +const char* mqtt_client_id = "Energy-Montor"; |
| 14 | +const uint16_t mqtt_port = 1883; |
| 15 | + |
| 16 | +const char* pv_power_topic = "homeassistant/pv-power"; |
| 17 | +const char* grid_power_topic = "homeassistant/grid-power"; |
| 18 | + |
| 19 | +// ======================= |
| 20 | + |
| 21 | +WiFiClient espClient; |
| 22 | +PubSubClient mqttClient(espClient); |
| 23 | +Ticker displayTicker; |
| 24 | + |
| 25 | +// This defines the 'on' time of the display is us. The larger this number, |
| 26 | +// the brighter the display. If too large the ESP will crash |
| 27 | +uint8_t display_draw_time = 60; //30-70 is usually fine |
| 28 | + |
| 29 | +#define MATRIX_WIDTH 64 |
| 30 | +#define MATRIX_HEIGHT 32 |
| 31 | + |
| 32 | +PxMATRIX display(MATRIX_WIDTH, MATRIX_HEIGHT, 16, 2, 5, 4, 15, 12); |
| 33 | + |
| 34 | +// colors |
| 35 | +const uint16_t white = display.color565(255, 255, 255); |
| 36 | +const uint16_t red = display.color565(255, 77, 77); |
| 37 | +const uint16_t green = display.color565(159, 255, 128); |
| 38 | +const uint16_t blue = display.color565(0, 0, 255); |
| 39 | +const uint16_t black = display.color565(0, 0, 0); |
| 40 | + |
| 41 | +// Converted using the following site: http://www.rinkydinkelectronics.com/t_imageconverter565.php |
| 42 | +uint16_t static sun[] = { |
| 43 | + 0xDE03, 0x0000, 0x0000, 0x0000, 0xE623, 0x0000, 0x0000, 0x0000, 0xCD63, 0x0000, 0xDE03, 0x0000, 0x0000, 0xEE63, 0x0000, 0x0000, // 0x0010 (16) pixels |
| 44 | + 0xDE03, 0x0000, 0x0000, 0x0000, 0x8BC1, 0xDE03, 0xFEE4, 0xDE03, 0x8BC1, 0x0000, 0x0000, 0x0000, 0x0000, 0xDE03, 0xFEE4, 0xFEE4, // 0x0020 (32) pixels |
| 45 | + 0xFEE4, 0xDE03, 0x0000, 0x0000, 0xE623, 0xF6A3, 0xFEE4, 0xFEE4, 0xFEE4, 0xFEE4, 0xFEE4, 0xF683, 0xD5E3, 0x0000, 0x0000, 0xDE03, // 0x0030 (48) pixels |
| 46 | + 0xFEE4, 0xFEE4, 0xFEE4, 0xDE03, 0x0000, 0x0000, 0x0000, 0x0000, 0x8BC1, 0xDE03, 0xFEE4, 0xDE03, 0x8BC1, 0x0000, 0x0000, 0x0000, // 0x0040 (64) pixels |
| 47 | + 0xDE03, 0x0000, 0x0000, 0xDE03, 0x0000, 0x0000, 0xF6A3, 0x0000, 0xDE03, 0x0000, 0x0000, 0x0000, 0xDE03, 0x0000, 0x0000, 0x0000, // 0x0050 (80) pixels |
| 48 | + 0xDE03 |
| 49 | +}; |
| 50 | + |
| 51 | +uint16_t static house[] = { |
| 52 | + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xE924, 0x0000, 0x0000, // 0x0010 (16) pixels |
| 53 | + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xE924, 0xFFFF, 0xE924, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xE924, 0xD0E3, 0xE924, // 0x0020 (32) pixels |
| 54 | + 0xA8A2, 0xE924, 0x0000, 0x0000, 0xE924, 0xE924, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xD0E3, 0xD0E3, 0x0000, 0xE924, 0xFFFF, // 0x0030 (48) pixels |
| 55 | + 0xD3A0, 0xFFFF, 0x32D6, 0xEF7E, 0xE924, 0x0000, 0x0000, 0xE924, 0xFFFF, 0xD3A0, 0xFFFF, 0xC65C, 0xFFFF, 0xE924, 0x0000, 0x0000, // 0x0040 (64) pixels |
| 56 | + 0xE924, 0xFFFF, 0xD3A0, 0xFFFF, 0xFFFF, 0xFFFF, 0xE924, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // 0x0050 (80) pixels |
| 57 | + 0x0000 |
| 58 | +}; |
| 59 | + |
| 60 | +uint16_t static pole[] = { |
| 61 | + 0x0000, 0x0000, 0x096A, 0x1B14, 0x1B56, 0x1B14, 0x096A, 0x0000, 0x0000, 0x0000, 0x096A, 0x1B14, 0x0000, 0x1B56, 0x0000, 0x1B14, // 0x0010 (16) pixels |
| 62 | + 0x096A, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x1B56, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x096A, 0x1B14, 0x1B56, // 0x0020 (32) pixels |
| 63 | + 0x1B14, 0x09ED, 0x0043, 0x0000, 0x0000, 0x096A, 0x1B14, 0x0000, 0x1B56, 0x0000, 0x12B2, 0x096A, 0x0000, 0x0000, 0x0000, 0x0000, // 0x0030 (48) pixels |
| 64 | + 0x0000, 0x1B56, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x1B56, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // 0x0040 (64) pixels |
| 65 | + 0x0000, 0x0000, 0x0000, 0x1B56, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x8B24, 0x8B24, 0x8B24, 0x8B24, 0x8B24, 0x8B24, 0x8B24, // 0x0050 (80) pixels |
| 66 | + 0x0000 |
| 67 | +}; |
| 68 | + |
| 69 | +struct State { |
| 70 | + float pvPower; |
| 71 | + float gridPower; |
| 72 | + float powerConsumption; |
| 73 | +}; |
| 74 | + |
| 75 | +State state = { 0.0, 0.0, 0.0 }; |
| 76 | + |
| 77 | +void setup() { |
| 78 | + Serial.begin(9600); |
| 79 | + |
| 80 | + // 1/16 scan display |
| 81 | + display.begin(16); |
| 82 | + |
| 83 | + display.clearDisplay(); |
| 84 | + displayTicker.attach(0.004, displayUpdater); |
| 85 | + |
| 86 | + setupWifi(); |
| 87 | + |
| 88 | + display.clearDisplay(); |
| 89 | + drawStaticImages(); |
| 90 | + |
| 91 | + mqttClient.setServer(mqtt_server, mqtt_port); |
| 92 | + mqttClient.setCallback(mqttCallback); |
| 93 | +} |
| 94 | + |
| 95 | +// ISR (interrupt service routine) for display refresh |
| 96 | +void displayUpdater() { |
| 97 | + display.display(display_draw_time); |
| 98 | +} |
| 99 | + |
| 100 | +void setupWifi() { |
| 101 | + delay(10); |
| 102 | + |
| 103 | + display.setCursor(0, 0); |
| 104 | + display.setTextColor(blue); |
| 105 | + display.printf("Connecting to %s", ssid); |
| 106 | + |
| 107 | + WiFi.mode(WIFI_STA); |
| 108 | + WiFi.begin(ssid, password); |
| 109 | + |
| 110 | + while (WiFi.status() != WL_CONNECTED) { |
| 111 | + delay(3000); |
| 112 | + display.print("."); |
| 113 | + } |
| 114 | + |
| 115 | + display.clearDisplay(); |
| 116 | + display.setCursor(0, 0); |
| 117 | + display.println("connected"); |
| 118 | + display.println(WiFi.localIP()); |
| 119 | + |
| 120 | + delay(2000); |
| 121 | +} |
| 122 | + |
| 123 | +void loop() { |
| 124 | + if (!mqttClient.connected()) { |
| 125 | + connectToMqttServer(); |
| 126 | + } |
| 127 | + mqttClient.loop(); |
| 128 | +} |
| 129 | + |
| 130 | +void connectToMqttServer() { |
| 131 | + while (!mqttClient.connected()) { |
| 132 | + Serial.print("Attempting MQTT connection..."); |
| 133 | + |
| 134 | + mqttClient.setServer(mqtt_server, 1883); |
| 135 | + mqttClient.setCallback(mqttCallback); |
| 136 | + |
| 137 | + // Attempt to connect |
| 138 | + if (mqttClient.connect(mqtt_client_id, mqtt_username, mqtt_password)) { |
| 139 | + Serial.println("connected"); |
| 140 | + |
| 141 | + mqttClient.subscribe(pv_power_topic); |
| 142 | + mqttClient.subscribe(grid_power_topic); |
| 143 | + } else { |
| 144 | + Serial.print("failed, rc="); |
| 145 | + Serial.print(mqttClient.state()); |
| 146 | + Serial.println(" trying again in 5 seconds"); |
| 147 | + delay(5000); |
| 148 | + } |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +void mqttCallback(char* topic, byte* payload, unsigned int length) { |
| 153 | + payload[length] = '\0'; |
| 154 | + String payloadStr = String((char*)payload); |
| 155 | + |
| 156 | + Serial.printf("Message arrived [%s]: %s\n", topic, payloadStr); |
| 157 | + |
| 158 | + float floatPayload = payloadStr.toFloat(); |
| 159 | + |
| 160 | + if (strcmp(topic, pv_power_topic) == 0) { |
| 161 | + state.pvPower = floatPayload; |
| 162 | + } else if (strcmp(topic, grid_power_topic) == 0) { |
| 163 | + state.gridPower = floatPayload; |
| 164 | + } |
| 165 | + |
| 166 | + state.powerConsumption = state.gridPower + state.pvPower; |
| 167 | + |
| 168 | + updateDisplay(); |
| 169 | +} |
| 170 | + |
| 171 | +void drawStaticImages() { |
| 172 | + drawIcon(sun, 1, 1); |
| 173 | + drawIcon(house, 1, 11); |
| 174 | + drawIcon(pole, 1, 21); |
| 175 | +} |
| 176 | + |
| 177 | +void drawIcon(uint16_t* icon, int x, int y) { |
| 178 | + int iconHeight = 9; |
| 179 | + int iconWidth = 9; |
| 180 | + int counter = 0; |
| 181 | + for (int yy = 0; yy < iconHeight; yy++) { |
| 182 | + for (int xx = 0; xx < iconWidth; xx++) { |
| 183 | + display.drawPixel(x + xx, y + yy, icon[counter]); |
| 184 | + counter++; |
| 185 | + } |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +void updateDisplay() { |
| 190 | + display.fillRect(10, 2, MATRIX_WIDTH - 10, MATRIX_HEIGHT - 5, black); |
| 191 | + |
| 192 | + display.setCursor(9, 2); |
| 193 | + display.setTextColor(white); |
| 194 | + display.printf("%7.1f W", state.pvPower); |
| 195 | + |
| 196 | + display.setCursor(9, 12); |
| 197 | + display.setTextColor(white); |
| 198 | + display.printf("%7.1f W", state.powerConsumption); |
| 199 | + |
| 200 | + display.setCursor(9, 22); |
| 201 | + |
| 202 | + if (state.gridPower > 0) { |
| 203 | + display.setTextColor(red); |
| 204 | + } else if (state.gridPower < 0) { |
| 205 | + display.setTextColor(green); |
| 206 | + } else { |
| 207 | + display.setTextColor(white); |
| 208 | + } |
| 209 | + display.printf("%7.1f W", state.gridPower); |
| 210 | +} |
0 commit comments