This program pulls the greenhouse data from ThingSpeak every minute and refreshes the screen if the data has been updated by the greenhouse sender, which is every 10 minutes. The screen auto-dims after 5 minutes and is restored by touching it. The program pulls data from the public version of this data so no API key is required.
The display will highlight in red if the greenhouse sender battery drops below 3.5V and I've just realised, something I intended to add is a check to flag if the sender has gone off-line. (Never happened yet though
Code: [Local Link Removed for Guests]
wlog time$
bluetooth.delete
wlog can.stop
TP = 12 ' Time Period - Number of hours for max / min
TP = TP * 6 ' New reading every 10 minutes so 6 per hour. This is number of array entries.
OldID$ = ""
dim IntTemp(TP) : dim ExtTemp(TP) : dim IntHum(TP)
dim ExtHum(TP) : dim DifTemp(TP) : dim AtmPres(TP)
dim IntHum(TP) : dim ExtHum(TP)
TFT.INIT 1
TFT.BRIGHTNESS 255
OnTouch TouchDet
timer0 60000, RequestData
gosub RequestData
pause 1000
wait
RequestData:
BrtCnt = BrtCnt + 1
if BrtCnt > 5 then
tft.brightness 50
end if
wlog "Data request"
NewData$ = wget$("54.210.227.170/channels/1283507/feeds.json?results=" + str$(TP),443)
if instr(NewData$,"No Data") = 1 then wlog "No response from server."
ID$ = json$(NewData$,"channel.last_entry_id")
if ID$ <> OldID$ then
wlog newdata$
IntTempMax = -100
IntTempMin = 100
ExtTempMax = -100
ExtTempMin = 100
Updated$ = mid$(json$(NewData$,"channel.updated_at"),12,5)
for lcnt = 1 to TP
AtmPres(lcnt) = val(json$(NewData$,"feeds.field3[" + str$(lcnt) + "]" ))
IntTempX = val(json$(NewData$,"feeds.field4[" + str$(lcnt) + "]" ))
if IntTempX > IntTempMax then IntTempMax = IntTempX
if IntTempX < IntTempMin then IntTempMin = IntTempX
IntTemp(lcnt) = IntTempX
ExtTempX = val(json$(NewData$,"feeds.field6[" + str$(lcnt) + "]" ))
if ExtTempX > ExtTempMax then ExtTempMax = ExtTempX
if ExtTempX < ExtTempMin then ExtTempMin = ExtTempX
ExtTemp(lcnt)= ExtTempX
IntHum(lcnt) = val(json$(NewData$,"feeds.field5[" + str$(lcnt) + "]" ))
ExtHum(lcnt) = val(json$(NewData$,"feeds.field7[" + str$(lcnt) + "]" ))
DifTemp(lcnt) = IntTempX - ExtTempX
next lcnt
Batt$ = json$(NewData$,"feeds.field1[" + str$(TP) + "]" )
Signal$ = json$(NewData$,"feeds.field2[" + str$(TP) + "]" )
gosub Screen0
OldID$ = ID$
endif
return
TouchDet:
tft.brightness 255
BrtCnt = 0
touch.read
XPos = touch.x
YPos = touch.y
gosub Screen0
return
Screen0:
TFT.FILL green
tft.text.font 4
TFT.TEXT.SIZE 1
tft.rect 0,0,319,239,yellow
tft.rect 1,1,317,237,yellow
tft.rect 2,2,315,235,yellow
tft.line 40,32,274,32, black
tft.text.col black
PrintAtBld 40,10,"Greenhouse Monitor"
PrintAt 20,42,"At " + left$(time$,5)
PrintAt 164,42,"Int."
PrintAt 246,42,"Ext."
PrintAt 20,72,"Cur temp"
PrintAt 20,102,"Min temp"
PrintAt 20,132,"Max temp"
PrintAt 20,162,"Humidity"
PrintAt 164,72,str$(IntTemp(TP))
PrintAt 246,72,str$(ExtTemp(TP))
PrintAt 164,102,str$(IntTempMin)
PrintAt 246,102,str$(ExtTempMin)
PrintAt 164,132,str$(IntTempMax)
PrintAt 246,132,str$(ExtTempMax)
PrintAt 164,162,str$(IntHum(TP))
PrintAt 246,162,str$(ExtHum(TP))
PrintAt 192,200,"Signal " + Signal$
if val(Batt$) > 3.4 then
PrintAt 20,200,"Battery " + Batt$ + "v"
else
' Greenhouse sender battery 3.4V or less - Needs recharging.
tft.text.col black,red
PrintAtBld 20,200,"Battery " + Batt$ + "v"
tft.text.col black, green
end if
return
sub PrintAt(x,y,t$)
tft.text.pos x,y
tft.print t$
end sub
sub PrintAtBld(x,y,t$)
tft.text.pos x,y
tft.print t$
tft.text.pos x+1,y
tft.print t$
end sub