39 if (i2c_dev_probe(&dev, I2C_DEV_WRITE) == 0) { |
55 if (i2c_dev_probe(&dev, I2C_DEV_WRITE) == 0) { |
40 ESP_LOGI(TAG, "Found ADPS-9930"); |
56 ESP_LOGI(TAG, "Found ADPS-9930"); |
41 } |
57 } |
42 dev.addr = 0x40; |
58 dev.addr = 0x40; |
43 if (i2c_dev_probe(&dev, I2C_DEV_WRITE) == 0) { |
59 if (i2c_dev_probe(&dev, I2C_DEV_WRITE) == 0) { |
|
60 ESP_ERROR_CHECK(ina219_init_desc(&ina219_b_dev, 0x40, 0, CONFIG_I2C_MASTER_SDA, CONFIG_I2C_MASTER_SCL)); |
|
61 ESP_ERROR_CHECK(ina219_init(&ina219_b_dev)); |
44 ESP_LOGI(TAG, "Found INA219 Battery"); |
62 ESP_LOGI(TAG, "Found INA219 Battery"); |
45 } |
63 } |
46 dev.addr = 0x41; |
64 dev.addr = 0x41; |
47 if (i2c_dev_probe(&dev, I2C_DEV_WRITE) == 0) { |
65 if (i2c_dev_probe(&dev, I2C_DEV_WRITE) == 0) { |
|
66 ESP_ERROR_CHECK(ina219_init_desc(&ina219_s_dev, 0x41, 0, CONFIG_I2C_MASTER_SDA, CONFIG_I2C_MASTER_SCL)); |
|
67 ESP_ERROR_CHECK(ina219_init(&ina219_s_dev)); |
48 ESP_LOGI(TAG, "Found INA219 Solar"); |
68 ESP_LOGI(TAG, "Found INA219 Solar"); |
49 } |
69 } |
50 dev.addr = 0x76; |
70 dev.addr = 0x76; |
51 if (i2c_dev_probe(&dev, I2C_DEV_WRITE) == 0) { |
71 if (i2c_dev_probe(&dev, I2C_DEV_WRITE) == 0) { |
52 ESP_ERROR_CHECK(bmp280_init_desc(&bmp280_dev, BMP280_I2C_ADDRESS_0, 0, CONFIG_I2C_MASTER_SDA, CONFIG_I2C_MASTER_SCL)); |
72 ESP_ERROR_CHECK(bmp280_init_desc(&bmp280_dev, BMP280_I2C_ADDRESS_0, 0, CONFIG_I2C_MASTER_SDA, CONFIG_I2C_MASTER_SCL)); |
53 ESP_ERROR_CHECK(bmp280_init(&bmp280_dev, &bmp280_params)); |
73 ESP_ERROR_CHECK(bmp280_init(&bmp280_dev, &bmp280_params)); |
54 ESP_LOGI(TAG, "Found BMP280 @ 0x76 id: 0x%02x", bmp280_dev.id); |
74 ESP_LOGI(TAG, "Found BMP280 @ 0x76 id: 0x%02x", bmp280_dev.id); |
55 } |
75 } else { |
56 dev.addr = 0x77; |
76 dev.addr = 0x77; |
57 if (i2c_dev_probe(&dev, I2C_DEV_WRITE) == 0) { |
77 if (i2c_dev_probe(&dev, I2C_DEV_WRITE) == 0) { |
58 ESP_LOGI(TAG, "Found BMP280 @ 0x77"); |
78 ESP_ERROR_CHECK(bmp280_init_desc(&bmp280_dev, BMP280_I2C_ADDRESS_1, 0, CONFIG_I2C_MASTER_SDA, CONFIG_I2C_MASTER_SCL)); |
|
79 ESP_ERROR_CHECK(bmp280_init(&bmp280_dev, &bmp280_params)); |
|
80 ESP_LOGI(TAG, "Found BMP280 @ 0x77 id: 0x%02x", bmp280_dev.id); |
|
81 } |
59 } |
82 } |
60 |
83 |
61 /* |
84 /* |
62 * Create FreeRTOS tasks |
85 * Create FreeRTOS tasks |
63 */ |
86 */ |
64 xSemaphoreBMP280 = xSemaphoreCreateMutex(); |
87 xSemaphoreBMP280 = xSemaphoreCreateMutex(); |
65 |
88 |
66 xTaskCreate(&task_bmp280, "task_bmp280", 2560, NULL, 8, &xTaskBMP280); |
89 xTaskCreate(&task_bmp280, "task_bmp280", 2560, NULL, 8, &xTaskBMP280); |
|
90 xTaskCreate(&task_ina219, "task_ina219", 2560, NULL, 8, &xTaskINA219); |
67 |
91 |
68 /* |
92 /* |
69 * Main application loop. |
93 * Main application loop. |
70 */ |
94 */ |
|
95 int State = State_Init; |
|
96 int OldState = State_Init + 1; |
|
97 |
71 while (1) { |
98 while (1) { |
72 request_bmp280(); |
99 if (OldState != State) { |
73 vTaskDelay(2000 / portTICK_PERIOD_MS); |
100 ESP_LOGI(TAG, "Switch to state %d", State); |
|
101 OldState = State; |
|
102 } |
|
103 switch (State) { |
|
104 case State_Init: request_bmp280(); |
|
105 request_ina219(); |
|
106 // getTempBaro(); |
|
107 // getLightValues(); |
|
108 // getVoltsCurrent(); |
|
109 State = State_Connect; |
|
110 break; |
|
111 |
|
112 case State_Connect: // Wake WiFi ?? |
|
113 // if (NetworkConnect()) { |
|
114 // State = State_Working; |
|
115 // Alarm &= ~AL_NOWIFI; |
|
116 // DisCounter = 0; |
|
117 // } else { |
|
118 // DisCounter++; |
|
119 // if (DisCounter > 30) { |
|
120 // Alarm |= AL_NOWIFI; |
|
121 // } |
|
122 // delay(2000); |
|
123 // } |
|
124 break; |
|
125 |
|
126 case State_Working: // WorkAgain = false; |
|
127 // client.loop(); |
|
128 |
|
129 // // Measure |
|
130 // getVoltsCurrent(); |
|
131 // solarVolts = solarCurrent = batteryVolts = batteryCurrent = 0; |
|
132 // loops = 0; |
|
133 // totalTime = 0; |
|
134 // for (int i = 0; i < loopno; i++) { |
|
135 /* |
|
136 * If there are only 2 loops, and the flag that we came from deep-sleep is set |
|
137 * then assume the following: |
|
138 * 1. No current (or very low for the dc converter) during DS_TIME seconds. |
|
139 * 2. Low power during 0.5 second (no WiFi yet). |
|
140 * 3. 5 seconds power usage for the last measurement. |
|
141 * Calculate the average battery current from this. |
|
142 * |
|
143 * If there are more loops and we came from continues running do the following: |
|
144 * 1. Take the current use from all exept the last one, weight loops * 10 seconds. |
|
145 * 2. Take the last one, and weight for 5 seconds. |
|
146 * Calculate the average battery current from this. |
|
147 */ |
|
148 // if (m_Valid[i]) { |
|
149 // solarVolts += s_Volts[i]; |
|
150 // solarCurrent += s_Current[i]; |
|
151 // batteryVolts += b_Volts[i]; |
|
152 // if (i == (loopno - 1)) { |
|
153 // // Add the extra time |
|
154 // m_Time[i] += SUB_TIME; |
|
155 // } |
|
156 // batteryCurrent += b_Current[i] * m_Time[i]; |
|
157 // totalTime += m_Time[i]; |
|
158 // loops++; |
|
159 // } |
|
160 // } |
|
161 |
|
162 // if (EEPROM.read(EM_DS_Active)) { |
|
163 // totalTime += EEPROM.read(EM_DS_Time) * 1000; |
|
164 // batteryCurrent += DS_CURRENT * EEPROM.read(EM_DS_Time) * 1000; |
|
165 // // Serial.printf("Added %d totalTime %d\n", EEPROM.read(EM_DS_Time) * 1000, totalTime); |
|
166 // } |
|
167 |
|
168 // If valid measurements |
|
169 // if (loops) { |
|
170 // solarVolts = solarVolts / loops; |
|
171 // solarCurrent = solarCurrent / loops; |
|
172 // solarPower = solarVolts * solarCurrent; |
|
173 // batteryVolts = batteryVolts / loops; |
|
174 // batteryCurrent = batteryCurrent / totalTime; |
|
175 // batteryPower = batteryVolts * batteryCurrent; |
|
176 // BatteryState(batteryVolts, (0 - batteryCurrent) + solarCurrent); |
|
177 |
|
178 // //#if Debug == true |
|
179 // Serial.print(F(" Solar Volts: ")); |
|
180 // Serial.print(solarVolts, 4); |
|
181 // Serial.print(F("V Current: ")); |
|
182 // Serial.print(solarCurrent, 4); |
|
183 // Serial.print(F("mA Power: ")); |
|
184 // Serial.print(solarPower, 4); |
|
185 // Serial.println(F("mW")); |
|
186 |
|
187 // Serial.print(F("Battery Volts: ")); |
|
188 // Serial.print(batteryVolts, 4); |
|
189 // Serial.print(F("V Current: ")); |
|
190 // Serial.print(batteryCurrent, 4); |
|
191 // Serial.print(F("mA Power: ")); |
|
192 // Serial.print(batteryPower, 4); |
|
193 // Serial.print(F("mW Capacity ")); |
|
194 // Serial.print(batteryState); |
|
195 // Serial.println(F("%")); |
|
196 // //#endif |
|
197 |
|
198 /* Check alarm conditions */ |
|
199 // if (batteryState <= 10) { |
|
200 // Alarm |= AL_ACCULOW; |
|
201 // } else { |
|
202 // Alarm &= ~AL_ACCULOW; |
|
203 // } |
|
204 // } |
|
205 // getTempHumi(); |
|
206 // Publish(); |
|
207 // Subscribe |
|
208 // #if Production == true |
|
209 // client.subscribe("balkon/output/set/#"); |
|
210 // #else |
|
211 // client.subscribe("wemos/output/set/#"); |
|
212 // #endif |
|
213 // client.loop(); |
|
214 // State = State_WorkDone; |
|
215 // gTimeNext = millis() + SUB_TIME; |
|
216 break; |
|
217 |
|
218 case State_WorkDone: // Hang around for a while to process the subscriptions. |
|
219 // client.loop(); |
|
220 // delay(1); |
|
221 // if (WorkAgain) { |
|
222 // // Some command was executed. |
|
223 // State = State_Working; |
|
224 // } |
|
225 // if (gTimeInMillis > gTimeNext) { |
|
226 // State = State_Stop; |
|
227 // } |
|
228 break; |
|
229 |
|
230 case State_Stop: // #if Production == true |
|
231 // client.unsubscribe("balkon/output/set/#"); |
|
232 // #else |
|
233 // client.unsubscribe("wemos/output/set/#"); |
|
234 // #endif |
|
235 // delay(1); |
|
236 // client.loop(); |
|
237 // client.disconnect(); |
|
238 // delay(1); |
|
239 // WiFi.mode( WIFI_OFF ); |
|
240 // // Reset values for average current measurement. |
|
241 // HaveIP = false; |
|
242 // loopno = 0; |
|
243 // gLastTime = millis(); |
|
244 // delay(10); |
|
245 // #if defined(ARDUINO_ESP8266_WEMOS_D1MINI) |
|
246 // WiFi.forceSleepBegin(0); // 0 == forever |
|
247 // #endif |
|
248 |
|
249 /* |
|
250 * If any output is on, use 6 10 seconds loops. |
|
251 * If nothing on, do a deep sleep. |
|
252 */ |
|
253 // if (EEPROM.read(EM_Relay1) || EEPROM.read(EM_Relay2) || EEPROM.read(EM_Dimmer3) || EEPROM.read(EM_Dimmer4)) { |
|
254 // if (EEPROM.read(EM_DS_Active)) { |
|
255 // EEPROM.write(EM_DS_Active, 0); |
|
256 // EEPROM.commit(); |
|
257 // } |
|
258 |
|
259 // // Active mode, 60 seconds loop |
|
260 // ST_LOOPS = 6; |
|
261 // gTimeNext = millis() + ST_INTERVAL; |
|
262 // #if Debug == true |
|
263 // Serial.println(F("Start sleeploops")); |
|
264 // #endif |
|
265 // State = State_Wait; |
|
266 // } else { |
|
267 // ds_time = DS_TIME; |
|
268 // if (solarVolts < 6) { |
|
269 // // At night, increase the deep-sleep time. |
|
270 // ds_time *= 4; |
|
271 // } |
|
272 // if ((! EEPROM.read(EM_DS_Active)) || (EEPROM.read(EM_DS_Time) != ds_time)) { |
|
273 // EEPROM.write(EM_DS_Active, 1); |
|
274 // EEPROM.write(EM_DS_Time, ds_time); |
|
275 // EEPROM.commit(); |
|
276 // Serial.println("wrote new deep-sleep settings"); |
|
277 // } |
|
278 // State = State_GoSleep; |
|
279 // } |
|
280 /* |
|
281 * Update CRC and write rtcData. |
|
282 */ |
|
283 // rtcData.crc32 = calculateCRC32((uint8_t*) &rtcData.data, sizeof(rtcData)-4); |
|
284 // #if defined(ARDUINO_ESP8266_WEMOS_D1MINI) |
|
285 // if (ESP.rtcUserMemoryWrite(0, (uint32_t*) &rtcData, sizeof(rtcData))) { |
|
286 // #if Debug == true |
|
287 // Serial.print("Write: "); |
|
288 // printMemory(); |
|
289 // #endif |
|
290 // } else { |
|
291 // Serial.println("Write error rtcData"); |
|
292 // } |
|
293 // #endif |
|
294 break; |
|
295 |
|
296 case State_Wait: // if (gTimeInMillis > gTimeNext) { |
|
297 // State = State_Measure; |
|
298 // } |
|
299 break; |
|
300 |
|
301 case State_Measure: // getVoltsCurrent(); |
|
302 // if (isnan(Temperature)) { |
|
303 // getTempHumi(); |
|
304 // } |
|
305 // gTimeNext = millis() + ST_INTERVAL; |
|
306 // if (loopno >= ST_LOOPS) { |
|
307 // getLightValues(); |
|
308 // State = State_Connect; |
|
309 // } else { |
|
310 // State = State_Wait; |
|
311 // } |
|
312 break; |
|
313 |
|
314 case State_GoSleep: // ds_time = EEPROM.read(EM_DS_Time); |
|
315 // #if Debug == true |
|
316 // Serial.printf("Going to deep-sleep for %d seconds\n", ds_time); |
|
317 // #endif |
|
318 // ESP.deepSleep(ds_time * 1e6); |
|
319 break; |
|
320 } |
|
321 vTaskDelay(20 / portTICK_PERIOD_MS); |
74 } |
322 } |
75 // Not reached. |
323 // Not reached. |
76 } |
324 } |