-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneopixel-solar.py
1498 lines (1196 loc) · 53.7 KB
/
neopixel-solar.py
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
#
#Thanks so much to Uwe Moslehner in the Enphase Users + Owners FB group for the example of Token Auth working on D7 firmware.
# Hot water element size in watts + 300 to avoid on/off cycling.
run_power = 2000 # size of the hot water element + 200 W
grid_draw = 0 # this is the baseline added to solar production to offset a series of poor solar production days
start_hour = 10 # hours post midnight before switching starts, to avoid telegraphing at sun comes up
heating_done = 0 # minutes of relay on with power < element (surrogate marked for themostat off and heating done)
voltage = 240 # set here to solve json float problems
import os
import sys #allows use of sys.exit()
import urequests
import network
from umqtt.simple import MQTTClient
from utime import sleep
from machine import Pin, I2C, RTC
from neopixel import NeoPixel
import json
import random #for the random start delay
import ntptime
import gc
import secrets
#Make these global to keep pixel status updates easy
solar_production = 0
power_consumption = 0
evse_consumption = 0
shelly_power = 0
#max_freq sets the maximum frequency in Hz and accepts the values 20000000, 40000000, 80000000, 160000000, and 240000000
#machine.freq(160000000)
NUMBER_PIXELS = 25
LED_PIN = 8
strip = NeoPixel(Pin(LED_PIN), NUMBER_PIXELS)
pin_led = Pin(10, mode=Pin.OUT)
pin_led.off()
network.country("AU")
sta_if = network.WLAN(network.STA_IF)
sta_if.active(False) # TOGGLING THIS PREVENTS "OSError: Wifi Internal Error"
sta_if.active(True)
#sta_if.config(pm=sta_if.PM_NONE) # disable power management, seems to need active(TRUE) first
#sta_if.config(pm=sta_if.PM_PERFORMANCE) # disable power management, seems to need active(TRUE) first
sta_if.config(pm=sta_if.PM_POWERSAVE) # disable power management, seems to need active(TRUE) first
loopnum = 0
errcount = 0
################################
def npixel(i,red,green,blue):
# i = i -6
if i > 24: i = 24
strip[i] = (red,green,blue) # red=255, green and blue are 0
strip.write() # send the data from RAM down the wire
return
#################################
def do_connect():
loopnum = 0
while not sta_if.isconnected():
loopnum = 0
try:
sta_if.active(False)
sta_if.active(True)
sta_if.config(txpower=10)
sta_if.connect(secrets.SSID, secrets.wifi_password)
print('Maybe connected now: {}...'.format(sta_if.status()))
sleep(1)
except:
print("well that attempt failed")
sta_if.disconnect()
sta_if.active(False)
while loopnum < 25:
print('Got status {}...'.format(sta_if.status()))
if sta_if.status() == 2: #Magenta
npixel(loopnum,1,0,1)
elif sta_if.status() == 1001: #Yellow, Connecting
npixel(loopnum,1,1,0)
elif sta_if.status() == 202: #Cyan, Password error
npixel(loopnum,0,1,1)
elif sta_if.status() == 1000: #Green, Idle/waiting
npixel(loopnum,0,1,0)
elif sta_if.status() == 203: #Blue, Association fail
npixel(loopnum,0,0,1)
elif sta_if.status() == 1010: #CONNECTED
npixel(loopnum,1,1,1)
sleep(1)
break
else:
npixel(loopnum,1,0,0) #Red, Some other error
loopnum = loopnum + 1
sleep(1)
# Never got error 201 No AP, 200 Timeout, 204 Handshake timeout
if loopnum >= 25:
loopnum = 0
strip.fill((0,0,0)) #Clears the neopixels
strip.write()
else:
if sta_if.status() == 1010: #Connected
if loopnum > 1: npixel(loopnum,1,1,1)
print("Wifi is connected")
sleep(1)
else:
print('Wifi connection failed: {}...'.format(sta_if.status()))
npixel(loopnum,1,0,0)
return
#########################
def syncnettime():
# https://docs.micropython.org/en/latest/library/machine.RTC.html
gc.collect()
for loopnum in range(0, 3):
try:
npixel(loopnum,0,0,1)
print("Getting current time from net")
response = urequests.get("http://worldtimeapi.org/api/timezone/Australia/Brisbane")
json_data = json.loads(response.text)
response.close()
print(json_data)
current_time = json_data["datetime"]
the_date, the_time = current_time.split("T")
year, month, mday = [int(x) for x in the_date.split("-")]
the_time = the_time.split(".")[0]
hours, minutes, seconds = [int(x) for x in the_time.split(":")]
print(year, month, mday, 0, hours, minutes, seconds, 0)
#sets the date and time from the extracted JSON data
rtc.datetime((year, month, mday, 0, hours, minutes, seconds, 0))
t = rtc.datetime()
timestamp = '{:04d}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}'.format(t[0], t[1], t[2], t[4], t[5], t[6])
print(timestamp)
except OSError as err:
# npixel(loopnum,1,0,0)
print(err)
print("OSError:", gc.mem_free())
if err == "-202": do_connect()
sleep(loopnum * 2)
except Exception as err:
print(err)
sleep(loopnum * 2)
except:
print("Something else went wrong, time syncing failed")
sleep(loopnum * 2)
else:
npixel(loopnum,1,1,1)
print("Time sync complete")
sleep(1)
return()
npixel(loopnum,1,0,0)
sleep(3)
import machine
machine.reset()
return()
##############################################################################
#def read_production_data(solar_production, power_consumption, shelly_power, shelly_temp, solar_today, consumption_today, voltage):
def read_production_data():
rtc = RTC()
t = rtc.datetime()
global solar_production, power_consumption, shelly_power, shelly_temp, solar_today, consumption_today, voltage
if solar_production > 0: drawpixels('cyan')
#url = 'http://envoy.local/production.json'
url = 'https://192.168.1.101/production.json'
# NOTE: I was getting connect refused when hitting envoy.local, probably due to some router DNS problems, so I set a static IP:
# Error Connecting: HTTPConnectionPool(host='envoy.local', port=80): Max retries exceeded with url: /production.json (Caused by NewConnectionError('<urequests.packages.urllib3.connection.HTTPConnection object at 0x75cf3ad0>: Failed to establish a new connection: [Errno -2] Name or service not known',))
solar_today = 0
# while power_consumption == 0:
headers = {
"Accept": "application/json",
"Authorization": secrets.authtoken,
}
try:
response = urequests.get(url, headers=headers) # self-signed certificate
json_data = json.loads(response.text)
response.close()
solar_production = json_data['production'][1]['wNow']
voltage = json_data['production'][1]['rmsVoltage']
solar_today = json_data['production'][1]['whToday']
power_consumption = json_data['consumption'][0]['wNow']
consumption_today = json_data['consumption'][0]['whToday']
solar_today = solar_today / 1000
consumption_today = consumption_today / 1000
except:
t = rtc.datetime()
timestamp = '{:02d}:{:02d}:{:02d}'.format(t[4], t[5], t[6])
print(timestamp + "Error getting solar reading, now returning")
global errcount
errcount = errcount + 1
#pin_led.on() #use on board LED as error indicator
# timestamp = '{:02d}:{:02d}:{:02d}'.format(t[4], t[5], t[6])
# ev_kw = evse_consumption / 1000
# ev_kwh = evse_energy / 1000
# hw_kw = shelly_power / 1000
# hw_kwh = shelly_temp /1000000
# display.fill(0)
# display.text("S:" + "{:.0f}".format(solar_production) + "W P:" + "{:.0f}".format(power_consumption) + "W", 0, 0, 1)
# display.text("E:" + "{:.1f}".format(ev_kw) + "kW " + "{:.1f}".format(ev_kwh) + "kWh", 0, 8, 1)
# display.text("H:" + "{:.1f}".format(hw_kw) + "kW " + "{:.1f}".format(hw_kwh) + "kWh", 0, 16, 1)
# display.text(timestamp + " " + "{:.1f}".format(voltage) + "V", 0, 24, 1)
# display.show()
if solar_production > 0: drawpixels('red')
sleep(1)
# t = rtc.datetime()
# timestamp = '{:02d}:{:02d}:{:02d}'.format(t[4], t[5], t[6])
do_connect() #check wifi connected
# return(solar_production, power_consumption, shelly_power, shelly_temp, solar_today, consumption_today, voltage)
return(solar_production, power_consumption, shelly_power, shelly_temp, solar_today, consumption_today, voltage)
# do_connect() #check wifi connected
# return solar_production, power_consumption, shelly_power, shelly_temp, solar_today, consumption_today, voltage
return(solar_production, power_consumption, shelly_power, shelly_temp, solar_today, consumption_today, voltage)
##############################################
def publish_mqtt(target):
# strip[0] = (0,1,1) # red=255, green and blue are 0
# strip.write() # send the data from RAM down the wire
extra_power = solar_production - (power_consumption - evse_consumption)
# this sorts out weird spikes in MQTT available power, due to EVSE reporting incorrectly high as it fires up
if evse_consumption > power_consumption: extra_power = solar_production
t = rtc.datetime()
# if evse if off, solar +ve and > evse_start_solar
if extra_power < evse_min_power and solar_production > evse_start_solar and evse_consumption < 1000 and solar_production > power_consumption-shelly_power and t[4] < 16:
extra_power = evse_min_power
# if evse is on, using majority of power and solar > evse_min_solar
elif extra_power < evse_min_power and evse_consumption > 1000 and evse_consumption/power_consumption > 0.46 and solar_production > evse_min_solar and t[4] < 16:
extra_power = evse_min_power
# if extra_power < evse_min_power and evse_consumption > 2200: evse_min_solar = 50
# print("Setting EVSE to", extra_power, "while available solar is >", evse_min_solar, "and spare solar is not available")
if target == "csvlog":
if solar_production > 0: drawpixels('blue')
t = rtc.datetime()
timestamp = '{:04d}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}'.format(t[0], t[1], t[2], t[4], t[5], t[6])
mqttstring = timestamp+", "\
+str("{:.0f}".format(solar_production))+", "\
+str("{:.0f}".format(power_consumption))+", "\
+str("{:.0f}".format(shelly_power))+", "\
+str("{:.1f}".format(shelly_temp))+", "\
+str("{:.3f}".format(solar_today))+", "\
+str("{:.3f}".format(consumption_today))+", "\
+str("{:.1f}".format(voltage))+", "\
+str("{:.1f}".format(evse_temp))+", "\
+str("{:.0f}".format(evse_consumption))+", "\
+str("{:.0f}".format(extra_power))
mqttdata = mqttstring.encode() #convert to bytearray to send to MQTT
else:
mqttdata = "{:.1f}".format(extra_power)
try:
client_name = 'ESP32'
broker_addr = '192.168.1.17'
mqttc = MQTTClient(client_name, broker_addr, keepalive=60)
mqttc.connect()
mqttc.publish(target, mqttdata)
mqttc.publish("solar/power_consumption","{:.0f}".format(power_consumption))
mqttc.publish("solar/solar_production","{:.0f}".format(solar_production))
evse = evse_consumption + shelly_power
mqttc.publish("solar/evse","{:.0f}".format(evse))
mqttc.publish("solar/voltage","{:.1f}".format(voltage))
mqttc.disconnect()
except:
if solar_production > 0 and target == "csvlog": drawpixels('red')
t = rtc.datetime()
timestamp = '{:02d}:{:02d}:{:02d}'.format(t[4], t[5], t[6])
print("MQTT publishing failed", timestamp)
if target == "csvlog": do_connect() #check wifi connected
return()
#################### Gets the OpenEVSE status
def read_evse_data(evse_consumption):
# strip[0] = (0,0,1) # red=255, green and blue are 0
# strip.write() # send the data from RAM down the wire
old_evse_consumption = evse_consumption
if solar_production > 0: drawpixels('magenta')
try:
response = urequests.get('http://192.168.1.102/status', timeout=3)
json_data = json.loads(response.text)
response.close()
evse_energy = json_data['session_energy']
amp = json_data['amp'] / 1000
evse_consumption = amp * voltage
evse_temp = json_data['temp'] / 10
except:
evse_energy = -1
evse_temp = -1
evse_consumption = old_evse_consumption
rtc = RTC()
t = rtc.datetime()
timestamp = '{:02d}:{:02d}:{:02d}'.format(t[4], t[5], t[6])
print(timestamp + " Error contacting OpenEVSE")
if solar_production > 0: drawpixels('red')
sleep(1)
#
# return evse_consumption, 0, 0
return evse_consumption, evse_energy, evse_temp
######################
def read_shelly_data():
# strip[0] = (1,0,0) # red=255, green and blue are 0
# strip.write() # send the data from RAM down the wire
try:
response = urequests.get('http://192.168.1.33/status')
json_data = json.loads(response.text)
response.close()
shelly_power = json_data['meters'][0]['power']
shelly_temp = json_data['tmp']['tC']
# print (datetime.now().strftime("%H:%M:%S:%f"), "SHELLY:", "{:.1f}".format(shelly_power), "W consumption", "Temperature", "{:.1f}".format(shelly_temp), "Celcius,")
except:
shelly_power = -1
shelly_temp = -1
return shelly_power, shelly_temp
#################### Switch the Shelly relay and double check it responded
def switch_relay(shelly_state):
if solar_production > 0: drawpixels('yellow')
shelly_reply = "pending"
loopnum = 0
while shelly_state != shelly_reply:
# Ask shelly if it is on? ison is either 1 or 0.
try:
response = urequests.get('http://192.168.1.33/relay/0')
json_data = json.loads(response.text)
response.close()
ison = json_data['ison']
except:
print("AND TIMED OUT CHECKING SHELLY STATE ")
ison = -1
if ison == 1 and shelly_state == "off":
print("Shelly is on, turning off.")
send_switch(shelly_state)
elif ison == 0 and shelly_state == "on":
print("Shelly is off, turning on.")
send_switch(shelly_state)
elif ison == 1:
shelly_reply = "on"
print("Shelly is on")
elif ison == 0:
shelly_reply = "off"
print("Shelly is off")
else:
print("FAILED TO GET CURRENT SHELLY STATE ")
if solar_production > 0: drawpixels('red')
# counts the number of loops
loopnum +=1
if loopnum >= 10:
# Give up and make the reply = state to exit the loop
shelly_reply = shelly_state
sleep(0.5)
#print("Time, Solar, Power, Shelly, EVSE, evse_min_solar, grid_draw, heating_done, Boot time, Errcount")
# Now it loops to see if the reply matches the expected state
drawpixels('')
return
###################################################################
def send_switch(shelly_state):
url = "http://192.168.1.33/relay/0?turn=" + shelly_state
try:
print("Sending the switching request:", shelly_state)
response = urequests.get(url)
response.close()
except:
print("REQUEST TO SHELLY TIMED OUT ")
return
############################################################
#def drawpixels(solar, power, evse, brightness):
def drawpixels(status):
brightness = 10
NUMBER_PIXELS = 25 #25 -1 less, since numbering starts at 0
LED_PIN = 8
evse = evse_consumption + shelly_power
strip = NeoPixel(Pin(LED_PIN), NUMBER_PIXELS)
ledpower = (power_consumption/6000) * NUMBER_PIXELS
ledsolar = (solar_production/6000) * NUMBER_PIXELS
ledevse = (evse/6000) * NUMBER_PIXELS
# first_pixel = 1 # set to 0 if no status update
# else:
# first_pixel = 0 # set to 0 if no status update
for i in range(0, NUMBER_PIXELS):
if i < int(ledpower):
R = brightness # red=255, green and blue are 0
elif i == int(ledpower):
R = (ledpower - int(ledpower)) * brightness
elif i > int(ledpower):
R = 0
if i < int(ledsolar):
G = brightness # red=255, green and blue are 0
elif i == int(ledsolar):
G = (ledsolar - int(ledsolar)) * brightness
elif i > int(ledsolar):
G = 0
if i < int(ledevse):
B = brightness # red=255, green and blue are 0
G = 0
elif i == int(ledevse):
B = (ledevse - int(ledevse)) * brightness
elif i > int(ledevse):
B = 0
if (R == 0 and G == 0 and B == 0) or i == NUMBER_PIXELS-1:
if status == "magenta":
strip[i] = (1,0,1)
strip.write()
return
elif status == "cyan":
strip[i] = (0,1,1)
strip.write()
return
elif status == "yellow":
strip[i] = (1,1,0)
strip.write()
return
elif status == "blue":
strip[i] = (0,0,1)
strip.write()
return
elif status == "red":
strip[i] = (1,0,0)
strip.write()
return
elif R == 0 and G == 0 and B == 0:
return
strip[i] = (int(R),int(G),int(B)) # red=255, green and blue are 0
strip.write() # send the data from RAM down the wire
return()
###############################################################################
###############################################################################
#from machine import WDT
#wdt = WDT(timeout=180000) # enable watchdog timer with a timeout of 180s
#wdt.feed()
rtc = RTC()
t = rtc.datetime()
do_connect() # to wifi
syncnettime()
consumption_today = 0
#evse_consumption = 0
evse_consumption = -1
evse_energy = -1
log_minute = t[5] # need t = rtc.datetime() just above
night_reset = 0
power_consumption = 0
shelly_temp = 0
shelly_power = 0
solar_production = 0
solar_today = 0
voltage = 240
evse_start_solar = 1000
evse_min_solar = 400
evse_min_power = 1600
t = rtc.datetime()
print("Starting the main loop")
boottime = '{:04d}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}'.format(t[0], t[1], t[2], t[4], t[5], t[6])
print("Time: " + boottime)
strip.fill((0,0,0)) #Clears the neopixels to stop initial wifi logging showing.
strip.write()
# this is a delay to leave relay off for a bit after switching off, avoids flicking on then off when EVSE takes all the power at turn on.
# it also avoid large grid draw exactly on the hour during the first turn on.
#loops_till_start = random.randint(1, 100)
loops_till_start = 0
last_consumption = power_consumption
last_solar = solar_production
last_evse = evse_consumption
try:
# Allows an exception to catch errors and continue running.
while 1: # Run forever
#get the current solar production and consumption data
shelly_power, shelly_temp = read_shelly_data()
evse_consumption, evse_energy, evse_temp = read_evse_data(evse_consumption)
solar_production, power_consumption, shelly_power, shelly_temp, solar_today, consumption_today, voltage = read_production_data()
publish_mqtt("solar/export") # send data for EVSE and general solar/consumption
drawpixels('')
gc.collect()
t = rtc.datetime()
timestamp = '{:02d}:{:02d}:{:02d}'.format(t[4], t[5], t[6])
print(timestamp, "Solar", solar_production, "Power", power_consumption, "Shelly", shelly_power, "EVSE", evse_consumption, "EVSE min solar", evse_min_solar, "Grid draw", grid_draw, "heating_done", heating_done, "Boot", boottime, "Err", errcount)
if t[5] != log_minute:
# this checks if Shelly has been turned on when it should be off, and add 600W grid_draw
shelly_power, shelly_temp = read_shelly_data()
if shelly_power > 100:
grid_draw = grid_draw + 700
start_hour = 6
switch_relay("off") # checks the shelly relay really is off, and turns it off
if power_consumption > last_consumption + 100 or \
power_consumption < last_consumption - 100 or \
solar_production > last_solar + 150 or \
solar_production < last_solar - 150 or \
evse_consumption > last_evse + 150 or \
evse_consumption < last_evse - 150 or \
t[5] != log_minute:
publish_mqtt("csvlog")
t = rtc.datetime()
log_minute = t[5]
last_consumption = power_consumption
last_solar = solar_production
last_evse = evse_consumption
# this calculates if there's enough spare solar power to run
extra_power = solar_production+grid_draw-power_consumption
###################################
# this starts the heater if there's >'runpower' W excess power, and it's >= start_hour. The start_hour clause tried to optimise when solar should be available.
t = rtc.datetime()
if (extra_power > run_power and t[4] >= start_hour and loops_till_start < 1)\
or (grid_draw > 1 and solar_production > evse_consumption and evse_consumption > run_power and t[4] >= start_hour and loops_till_start < 1):
publish_mqtt("csvlog")
switch_relay("on")
night_reset = 1
# writes to the logfile before and after turning off the switch
shelly_power, shelly_temp = read_shelly_data()
evse_consumption, evse_energy, evse_temp = read_evse_data(evse_consumption)
solar_production, power_consumption, shelly_power, shelly_temp, solar_today, consumption_today, voltage = read_production_data()
publish_mqtt("csvlog")
t = rtc.datetime()
log_minute = t[5]
##################################
# and continues running while there's enough solar.
while solar_production + grid_draw > power_consumption\
or (grid_draw > 1 and solar_production + grid_draw > evse_consumption and (solar_production + grid_draw) > (power_consumption - evse_consumption)):
#wdt.feed()
sleep(2) #2 seconds
# AND now get fresh data from the solar system
shelly_power, shelly_temp = read_shelly_data()
evse_consumption, evse_energy, evse_temp = read_evse_data(evse_consumption)
solar_production, power_consumption, shelly_power, shelly_temp, solar_today, consumption_today, voltage = read_production_data()
publish_mqtt("solar/export")
drawpixels('')
gc.collect()
t = rtc.datetime()
timestamp = '{:02d}:{:02d}:{:02d}'.format(t[4], t[5], t[6])
#print(timestamp, solar_production, power_consumption, shelly_power, evse_consumption, grid_draw, heating_done, boottime, errcount)
# print(timestamp, solar_production, power_consumption, shelly_power, evse_consumption, evse_min_solar, grid_draw, heating_done, boottime, errcount)
print(timestamp, "Solar", solar_production, "Power", power_consumption, "Shelly", shelly_power, "EVSE", evse_consumption, "EVSE min solar", evse_min_solar, "Grid draw", grid_draw, "heating_done", heating_done, "Boot", boottime, "Err", errcount)
# This part checks if the relay is on once a minute (while it should be on), and if it is off, turns it on.
if t[5] != log_minute:
# this checks if Shelly has been turned off when it should be on, and resets grid_draw to 0.
shelly_power, shelly_temp = read_shelly_data()
if shelly_power < 100: grid_draw = 0
# checks the shelly relay really is on, and turns it on
switch_relay("on")
# check to see if power consumption is less than element size, and count minutes, to check heating completed
if shelly_power < 100 and solar_production + grid_draw > run_power:
heating_done = heating_done+1
print("Hot water finished (minutes):", heating_done)
if heating_done > 5: grid_draw = 0
if power_consumption > last_consumption + 100 or \
power_consumption < last_consumption - 100 or \
solar_production > last_solar + 150 or \
solar_production < last_solar - 150 or \
evse_consumption > last_evse + 150 or \
evse_consumption < last_evse - 150 or \
t[5] != log_minute:
publish_mqtt("csvlog")
t = rtc.datetime()
log_minute = t[5]
last_consumption = power_consumption
last_solar = solar_production
last_evse = evse_consumption
# And exits the loop turning off the shelly
else:
#"WHOOPS TOO MUCH CONSUMPTION")
publish_mqtt("csvlog")
switch_relay("off")
#wdt.feed()
sleep(2)
# writes to the logfile before and after turning off the switch
shelly_power, shelly_temp = read_shelly_data()
evse_consumption, evse_energy, evse_temp = read_evse_data(evse_consumption)
solar_production, power_consumption, shelly_power, shelly_temp, solar_today, consumption_today, voltage = read_production_data()
publish_mqtt("csvlog")
t = rtc.datetime()
log_minute = t[5]
loops_till_start = 2 # this does X loops as a delay before it turns on again, to prevent switch telegraphing
#wdt.feed()
sleep(3)
elif solar_production <= 0:
# IT'S NIGHT TIME NOW.
#wdt.feed()
sleep(5)
# just before midnight reset max_power, and set the start time for tomorrow based on whether power consumption < 1800w while relay on (presumably heating completed for > 5 minutes)
t = rtc.datetime()
if t[4] == 0 and t[5] >= 0 and night_reset == 1:
loops_till_start = random.randint(10, 100) # delay 1st start by RANDOM number of loops
pin_led.off() #reset on board LED error indicator
if heating_done > 90:
start_hour = 10
grid_draw = 0
elif heating_done > 5:
start_hour = 9
grid_draw = 0
else:
start_hour = 8
grid_draw = grid_draw + 525
heating_done = 0
night_reset = 0
evse_min_solar = 800
syncnettime()
else:
# this runs if there is solar, but we're waiting to start the hot water heating
night_reset = 1
t = rtc.datetime()
if (extra_power > run_power and t[4] >= start_hour and loops_till_start > 0) \
or (grid_draw > 1 and solar_production > evse_consumption and evse_consumption > run_power and t[4] >= start_hour and loops_till_start > 0):
loops_till_start = loops_till_start - 1
print(loops_till_start)
#wdt.feed()
sleep(3)
#except ValueError:
# print("Whoops, maybe a devision by zero. Sleep 5")
# sleep(5)
except KeyboardInterrupt:
print("Keyboardinterrupt caught")
# event.set() # tells the RGB thread to exit
# print("Turn off the Shelly hot water switch...")
# url = "http://192.168.1.33/relay/0?turn=off"
# response = urequests.get(url) # turns off the shelly switch
# print("Clean up the GPIOs...")
## GPIO.cleanup()
print("Exit")
#wdt.deinit()
# sys.exit()
#
#Thanks so much to Uwe Moslehner in the Enphase Users + Owners FB group for the example of Token Auth working on D7 firmware.
# Hot water element size in watts + 300 to avoid on/off cycling.
run_power = 2000 # size of the hot water element + 200 W
grid_draw = 0 # this is the baseline added to solar production to offset a series of poor solar production days
start_hour = 10 # hours post midnight before switching starts, to avoid telegraphing at sun comes up
heating_done = 0 # minutes of relay on with power < element (surrogate marked for themostat off and heating done)
voltage = 240 # set here to solve json float problems
import os
import sys #allows use of sys.exit()
import urequests
import network
from umqtt.simple import MQTTClient
from utime import sleep
from machine import Pin, I2C, RTC
from neopixel import NeoPixel
import json
import random #for the random start delay
import ntptime
import gc
import secrets
#Make these global to keep pixel status updates easy
solar_production = 0
power_consumption = 0
evse_consumption = 0
shelly_power = 0
#max_freq sets the maximum frequency in Hz and accepts the values 20000000, 40000000, 80000000, 160000000, and 240000000
#machine.freq(160000000)
NUMBER_PIXELS = 25
LED_PIN = 8
strip = NeoPixel(Pin(LED_PIN), NUMBER_PIXELS)
pin_led = Pin(10, mode=Pin.OUT)
pin_led.off()
network.country("AU")
sta_if = network.WLAN(network.STA_IF)
sta_if.active(False) # TOGGLING THIS PREVENTS "OSError: Wifi Internal Error"
sta_if.active(True)
#sta_if.config(pm=sta_if.PM_NONE) # disable power management, seems to need active(TRUE) first
#sta_if.config(pm=sta_if.PM_PERFORMANCE) # disable power management, seems to need active(TRUE) first
sta_if.config(pm=sta_if.PM_POWERSAVE) # disable power management, seems to need active(TRUE) first
loopnum = 0
errcount = 0
################################
def npixel(i,red,green,blue):
# i = i -6
if i > 24: i = 24
strip[i] = (red,green,blue) # red=255, green and blue are 0
strip.write() # send the data from RAM down the wire
return
#################################
def do_connect():
loopnum = 0
while not sta_if.isconnected():
loopnum = 0
try:
sta_if.active(False)
sta_if.active(True)
sta_if.config(txpower=10)
sta_if.connect(secrets.SSID, secrets.wifi_password)
print('Maybe connected now: {}...'.format(sta_if.status()))
sleep(1)
except:
print("well that attempt failed")
sta_if.disconnect()
sta_if.active(False)
while loopnum < 25:
print('Got status {}...'.format(sta_if.status()))
if sta_if.status() == 2: #Magenta
npixel(loopnum,1,0,1)
elif sta_if.status() == 1001: #Yellow, Connecting
npixel(loopnum,1,1,0)
elif sta_if.status() == 202: #Cyan, Password error
npixel(loopnum,0,1,1)
elif sta_if.status() == 1000: #Green, Idle/waiting
npixel(loopnum,0,1,0)
elif sta_if.status() == 203: #Blue, Association fail
npixel(loopnum,0,0,1)
elif sta_if.status() == 1010: #CONNECTED
npixel(loopnum,1,1,1)
sleep(1)
break
else:
npixel(loopnum,1,0,0) #Red, Some other error
loopnum = loopnum + 1
sleep(1)
# Never got error 201 No AP, 200 Timeout, 204 Handshake timeout
if loopnum >= 25:
loopnum = 0
strip.fill((0,0,0)) #Clears the neopixels
strip.write()
else:
if sta_if.status() == 1010: #Connected
if loopnum > 1: npixel(loopnum,1,1,1)
print("Wifi is connected")
sleep(1)
else:
print('Wifi connection failed: {}...'.format(sta_if.status()))
npixel(loopnum,1,0,0)
return
#########################
def syncnettime():
# https://docs.micropython.org/en/latest/library/machine.RTC.html
gc.collect()
for loopnum in range(0, 3):
try:
npixel(loopnum,0,0,1)
print("Getting current time from net")
response = urequests.get("http://worldtimeapi.org/api/timezone/Australia/Brisbane")
json_data = json.loads(response.text)
response.close()
print(json_data)
current_time = json_data["datetime"]
the_date, the_time = current_time.split("T")
year, month, mday = [int(x) for x in the_date.split("-")]
the_time = the_time.split(".")[0]
hours, minutes, seconds = [int(x) for x in the_time.split(":")]
print(year, month, mday, 0, hours, minutes, seconds, 0)
#sets the date and time from the extracted JSON data
rtc.datetime((year, month, mday, 0, hours, minutes, seconds, 0))
t = rtc.datetime()
timestamp = '{:04d}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}'.format(t[0], t[1], t[2], t[4], t[5], t[6])
print(timestamp)
except OSError as err:
# npixel(loopnum,1,0,0)
print(err)
print("OSError:", gc.mem_free())
if err == "-202": do_connect()
sleep(loopnum * 2)
except Exception as err:
print(err)
sleep(loopnum * 2)
except:
print("Something else went wrong, time syncing failed")
sleep(loopnum * 2)
else:
npixel(loopnum,1,1,1)
print("Time sync complete")
sleep(1)
return()
npixel(loopnum,1,0,0)
sleep(3)
import machine
machine.reset()
return()
##############################################################################
#def read_production_data(solar_production, power_consumption, shelly_power, shelly_temp, solar_today, consumption_today, voltage):
def read_production_data():
rtc = RTC()
t = rtc.datetime()
global solar_production, power_consumption, shelly_power, shelly_temp, solar_today, consumption_today, voltage
if solar_production > 0: drawpixels('cyan')
#url = 'http://envoy.local/production.json'
url = 'https://192.168.1.101/production.json'
# NOTE: I was getting connect refused when hitting envoy.local, probably due to some router DNS problems, so I set a static IP:
# Error Connecting: HTTPConnectionPool(host='envoy.local', port=80): Max retries exceeded with url: /production.json (Caused by NewConnectionError('<urequests.packages.urllib3.connection.HTTPConnection object at 0x75cf3ad0>: Failed to establish a new connection: [Errno -2] Name or service not known',))
solar_today = 0
# while power_consumption == 0:
headers = {
"Accept": "application/json",
"Authorization": secrets.authtoken,
}
try:
response = urequests.get(url, headers=headers) # self-signed certificate
json_data = json.loads(response.text)
response.close()
solar_production = json_data['production'][1]['wNow']
voltage = json_data['production'][1]['rmsVoltage']
solar_today = json_data['production'][1]['whToday']
power_consumption = json_data['consumption'][0]['wNow']
consumption_today = json_data['consumption'][0]['whToday']
solar_today = solar_today / 1000
consumption_today = consumption_today / 1000
except:
t = rtc.datetime()
timestamp = '{:02d}:{:02d}:{:02d}'.format(t[4], t[5], t[6])
print(timestamp + "Error getting solar reading, now returning")
global errcount
errcount = errcount + 1
#pin_led.on() #use on board LED as error indicator
# timestamp = '{:02d}:{:02d}:{:02d}'.format(t[4], t[5], t[6])
# ev_kw = evse_consumption / 1000
# ev_kwh = evse_energy / 1000
# hw_kw = shelly_power / 1000
# hw_kwh = shelly_temp /1000000
# display.fill(0)
# display.text("S:" + "{:.0f}".format(solar_production) + "W P:" + "{:.0f}".format(power_consumption) + "W", 0, 0, 1)
# display.text("E:" + "{:.1f}".format(ev_kw) + "kW " + "{:.1f}".format(ev_kwh) + "kWh", 0, 8, 1)
# display.text("H:" + "{:.1f}".format(hw_kw) + "kW " + "{:.1f}".format(hw_kwh) + "kWh", 0, 16, 1)
# display.text(timestamp + " " + "{:.1f}".format(voltage) + "V", 0, 24, 1)
# display.show()
if solar_production > 0: drawpixels('red')
sleep(1)
# t = rtc.datetime()
# timestamp = '{:02d}:{:02d}:{:02d}'.format(t[4], t[5], t[6])
do_connect() #check wifi connected
# return(solar_production, power_consumption, shelly_power, shelly_temp, solar_today, consumption_today, voltage)
return(solar_production, power_consumption, shelly_power, shelly_temp, solar_today, consumption_today, voltage)
# do_connect() #check wifi connected
# return solar_production, power_consumption, shelly_power, shelly_temp, solar_today, consumption_today, voltage
return(solar_production, power_consumption, shelly_power, shelly_temp, solar_today, consumption_today, voltage)
##############################################
def publish_mqtt(target):
# strip[0] = (0,1,1) # red=255, green and blue are 0
# strip.write() # send the data from RAM down the wire
extra_power = solar_production - (power_consumption - evse_consumption)
# this sorts out weird spikes in MQTT available power, due to EVSE reporting incorrectly high as it fires up
if evse_consumption > power_consumption: extra_power = solar_production