Demarcation of each script's section

Michael Ingraham 2019-07-28 18:09:09 -04:00
parent 8e6b85ba10
commit 18af97aac0
1 changed files with 186 additions and 188 deletions

@ -15,7 +15,7 @@
#### Scripting Language Example
Actually this code is too large. This is meant to show some of the possibilities
**>D**
>**>D**
; define all vars here
p:mintmp=10 (p:means permanent)
p:maxtmp=30
@ -32,14 +32,14 @@ dimmer=0
sw=0
rssi=0
param=0
col=""
>
>col=""
ocol=""
chan1=0
chan2=0
chan3=0
ahum=0
>
>ahum=0
atemp=0
tcnt=0
hour=0
@ -49,109 +49,109 @@ M:movav=0
; define array with 10 entries
m:array=0 10
**>B**
>**>B**
string=hello+"how are you?"
=\>print BOOT executed
=\>print %hello%
=\>mp3track 1
; list gpio pin definitions
>
>; list gpio pin definitions
for cnt 0 16 1
tmp=pd[cnt]
=>print %cnt% = %tmp%
next
; get gpio pin for relais 1
>
>; get gpio pin for relais 1
tmp=pn[21]
=>print relais 1 is on pin %tmp%
; pulse relais over raw gpio
>
>; pulse relais over raw gpio
spin(tmp 1)
delay(100)
spin(tmp 0)
; raw pin level
>
>; raw pin level
=>print level of gpio1 %pin[1]%
; pulse over tasmota cmd
>
>; pulse over tasmota cmd
=>power 1
delay(100)
=>power 0
**>T**
>**>T**
hum=BME280#Humidity
temp=BME280#Temperature
rssi=Wifi#RSSI
string=SleepMode
; add to median filter
>
>; add to median filter
median=temp
; add to moving average filter
movav=hum
; show filtered results
>
>; show filtered results
=>print %median% %movav%
if chg[rssi]>0
>
>if chg[rssi]>0
then =>print rssi changed to %rssi%
endif
if temp\>30
>
>if temp\>30
and hum\>70
then =\>print damn hot!
endif
**>S**
>**>S**
; every second but not completely reliable time here
; use upsecs and uptime or best t: for reliable timers
; arrays
>
>; arrays
array[1]=4
array[2]=5
tmp=array[1]+array[2]
; call subrountines with parameters
>
>; call subrountines with parameters
=#sub1("hallo")
=#sub2(999)
; stop timer after expired
>
>; stop timer after expired
if timer1==0
then timer1=-1
=>print timer1 expired
endif
; auto counter with restart
>
>; auto counter with restart
if count>=10
then =>print 10 seconds over
count=0
endif
if upsecs%5==0
>
>if upsecs%5==0
then =\>print %upsecs% (every 5 seconds)
endif
; not recommended for reliable timers
>
>; not recommended for reliable timers
timer+=1
if timer\>=5
then =\>print 5 seconds over (may be)
timer=0
endif
dimmer+=1
>
>dimmer+=1
if dimmer\>100
then dimmer=0
endif
=\>dimmer %dimmer%
>
>=\>dimmer %dimmer%
=\>WebSend %url% dimmer %dimmer%
; show on display
>
>; show on display
dp0
=\>displaytext [c1l1f1s2p20] dimmer=%dimmer%
=\>print %upsecs% %uptime% %time% %sunrise% %sunset% %tstamp%
if time\>sunset
>
>=\>print %upsecs% %uptime% %time% %sunrise% %sunset% %tstamp%
>
>if time\>sunset
and time< sunrise
then
; night time
@ -164,13 +164,13 @@ if pwr[1]\>0
then =\>power1 0
endif
endif
; clr display on boot
>
>; clr display on boot
if boot\>0
then =\>displaytext [z]
endif
; frost warning
>
>; frost warning
if temp<0
and mt<=0
then =#sendmail("frost alert")
@ -178,13 +178,13 @@ then =#sendmail("frost alert")
mt=300
=>mp3track 2
endif
; var has been updated
>
>; var has been updated
if upd[hello]>0
then =>print %hello%
endif
; send to Thingspeak every 60 seconds
>
>; send to Thingspeak every 60 seconds
; average data in between
if upsecs%60==0
then
@ -199,22 +199,21 @@ ahum+=hum
atemp+=temp
tcnt+=1
endif
hour=int(time/60)
>
>hour=int(time/60)
if chg[hour]>0
then
; exactly every hour
=>print full hour reached
endif
if time>5 {
>
>if time>5 {
=>print more then 5 minutes after midnight
} else {
=>print less then 5 minutes after midnight
}
; publish abs hum every teleperiod time
>
>; publish abs hum every teleperiod time
if mqtts>0
and upsecs%tper==0
then
@ -224,8 +223,8 @@ tmp=(6.112\*tmp\*hum\*18.01534)/((273.15+temp)\*8.31447215)
; publish median filtered value
=>Publish tele/%topic%/SENSOR {"Script":{"abshum":%med(0 tmp)%}}
endif
;switch case state machine
>
>;switch case state machine
switch state
case 1
=>print state=%state% , start
@ -237,55 +236,54 @@ case 3
=>print state=%state% , reset
state=1
ends
; subroutines
>
>; subroutines
\#sub1(string)
=>print sub1: %string%
\#sub2(param)
=>print sub2: %param%
\#sendmail(string)
>
>\#sendmail(string)
=>sendmail [smtp.gmail.com:465:user:passwd:<sender@gmail.de>:<rec@gmail.de>:alarm] %string%
**>E**
>**>E**
=\>print event executed!
; get HSBColor 1. component
>
>; get HSBColor 1. component
tmp=st(HSBColor , 1)
; check if switch changed state
>
>; check if switch changed state
sw=sw[1]
if chg[sw]>0
then =\>power1 %sw%
endif
hello=&quot;event occured&quot;
; check for Color change (Color is a string)
>
>hello=&quot;event occured&quot;
>
>; check for Color change (Color is a string)
col=Color
; color change needs 2 string vars
if col!=ocol
then ocol=col
=>print color changed %col%
endif
; or check change of color channels
>
>; or check change of color channels
chan1=Channel[1]
chan2=Channel[2]
chan3=Channel[3]
if chg[chan1]>0
>
>if chg[chan1]>0
or chg[chan2]>0
or chg[chan3]>0
then => color has changed
endif
; compose color string for red
>
>; compose color string for red
col=hn(255)+hn(0)+hn(0)
=>color %col%
**>R**
>**>R**
=\>print restarting now
[Back To Top](#top)
@ -293,8 +291,8 @@ col=hn(255)+hn(0)+hn(0)
------------------------------------------------------------------------------
#### Log Sensor
; define all vars here
; reserve large strings
>; define all vars here
; reserve large strings
**>D 48**
hum=0
temp=0
@ -305,14 +303,13 @@ M:mhum=0 60
M:mtemp=0 60
str=""
**>B**
>**>B**
; set sensor file download link
fl1("slog.txt")
; delete file in case we want to start fresh
;fd("slog.txt")
; list all files in root directory
>
>; list all files in root directory
fr=fo("/" 0)
for cnt 1 20 1
res=fr(str fr)
@ -325,18 +322,17 @@ endif
next
fc(fr)
**>T**
>**>T**
; get sensor values
temp=BME280#Temperature
hum=BME280#Humidity
**>S**
>**>S**
; average sensor values every second
mhum=hum
mtemp=temp
; write average to sensor log every minute
>
>; write average to sensor log every minute
if upsecs%60==0
then
; open file for write
@ -349,7 +345,8 @@ res=fw(str fr)
fc(fr)
endif
**>R**
>**>R**
[Back To Top](#top)
------------------------------------------------------------------------------
@ -358,7 +355,7 @@ endif
Some vars are set from iobroker
DisplayText substituted to save script space
**>D**
>**>D**
hum=0
temp=0
press=0
@ -377,13 +374,13 @@ DT="DisplayText"
punit="hPa"
tunit="C"
**>B**
>**>B**
;reset auto draw
=>%DT% [zD0]
;clr display and draw a frame
=>%DT% [x0y20h296x0y40h296]
**>T**
>**>T**
; get tele vars
temp=BME280#Temperature
hum=BME280#Humidity
@ -394,7 +391,7 @@ ahum=SGP30#aHumidity
tunit=TempUnit
punit=PressureUnit
**>S**
>**>S**
// update display every teleperiod time
if upsecs%tper==0
then
@ -405,8 +402,8 @@ dp2
=>%DT% [p10x30y25]TVOC: %tvoc% ppb
=>%DT% [p10x160y25]eCO2: %eco2% ppm
=>%DT% [p10c26l5]ahum: %ahum% g^m3
dp0
>
>dp0
=>%DT% [p25c1l5]WR 1 (Dach) : %wr1% W
=>%DT% [p25c1l6]WR 2 (Garage): %-wr3% W
=>%DT% [p25c1l7]WR 3 (Garten): %-wr2% W
@ -416,8 +413,10 @@ dp0
=>%DT% [d]
endif
**>E**
**>R**
>**>E**
>**>R**
[Back To Top](#top)
------------------------------------------------------------------------------
@ -428,7 +427,7 @@ display switches on and off with proximity sensor
some vars are set from iobroker
**>D**
>**>D**
temp=0
press=0
zwz=0
@ -444,10 +443,10 @@ punit="hPa"
tunit="C"
hour=0
**>B**
>**>B**
=>%DT% [z]
// define 2 graphs, 2. has 3 tracks
>
>// define 2 graphs, 2. has 3 tracks
=>%DT% [zCi1G2656:5:20:400:80:1440:-5000:5000:3Ci7f3x410y20]+5000 W[x410y95]-5000 W [Ci7f1x70y3] Zweirichtungsz~80hler - 24 Stunden
=>%DT% [Ci1G2657:5:120:400:80:1440:0:5000:3Ci7f3x410y120]+5000 W[x410y195]0 W [Ci7f1x70y103] Wechselrichter 1-3 - 24 Stunden
=>%DT% [Ci1G2658:5:120:400:80:1440:0:5000:16][Ci1G2659:5:120:400:80:1440:0:5000:5]
@ -455,15 +454,15 @@ hour=0
=>mp3volume 100
=>mp3track 4
**>T**
>**>T**
; get some tele vars
temp=BMP280#Temperature
press=BMP280#Pressure
tunit=TempUnit
punit=PressureUnit
dist=VL53L0X#Distance
; check proximity sensor to switch display on/off
>
>; check proximity sensor to switch display on/off
; to prevent burn in
if dist>300
then
@ -478,8 +477,7 @@ then
endif
endif
**>S**
>**>S**
; update graph every teleperiod
if upsecs%tper==0
then
@ -502,15 +500,17 @@ endif
=>%DT% [p-8x410y155Ci16Bi0]%-wr2% W
=>%DT% [p-8x410y170Ci5Bi0]%-wr3% W
endif
; chime every full hour
>
>; chime every full hour
hour=int(time/60)
if chg[hour]>0
then =>mp3track 4
endif
**>E**
**>R**
>**>E**
>**>R**
[Back To Top](#top)
------------------------------------------------------------------------------
@ -518,7 +518,7 @@ endif
#### LED Bar Display with WS2812 LED Chain
Used as display for solar house power input/output (+-5000 Watts)
**>D**
>**>D**
m:array=0 60 ;defines array for 60 led pixels
cnt=0
val=0
@ -538,23 +538,23 @@ max=5000
min=-5000
pos=0
**>B**
>**>B**
div=pixels/steps
=#prep
ws2812(array)
>
>; ledbar is set from broker
; ledbar is set from broker
**>S**
>**>S**
if ledbar<min
then ledbar=min
endif
if ledbar>max
>
>if ledbar>max
then ledbar=max
endif
pos=(ledbar/max)*(pixels/2)
>
>pos=(ledbar/max)*(pixels/2)
if ledbar>0
then
pos+=(pixels/2)
@ -567,30 +567,30 @@ if pos>pixels-1
then pos=1
endif
endif
if pos<1
>
>if pos<1
or pos>pixels
then pos=1
endif
=#prep
if ledbar==0
>
>=#prep
>
>if ledbar==0
then
array[pos]=blue
array[pos-1]=blue
else
array[pos]=blue
endif
; only used if power is off
>
>; only used if power is off
; so lets may be used normally if on
if pwr[1]==0
then
ws2812(array)
endif
; subroutine for grid
>
>; subroutine for grid
#prep
for cnt 1 pixels 1
ind+=1
@ -598,8 +598,8 @@ if ind>div
then ind=1
tog^=1
endif
if cnt<=pixels/2
>
>if cnt<=pixels/2
then
if tog>0
then val=colr1
@ -614,7 +614,8 @@ endif
array[cnt]=val
next
**>R**
>**>R**
[Back To Top](#top)
------------------------------------------------------------------------------
@ -623,83 +624,81 @@ next
Since I own a magic home with IR receiver I made a script to show how it works
I have 2 magic home devices that should be synchronized so I send the commands also to a second magic home via websend
; define (expand string section to 25 chars)
>; define (expand string section to 25 chars)
**>D 25**
istr=""
ws="websend [192.168.178.84]"
; event section
>; event section
**>E**
; get ir data
istr=IrReceived#Data
; on
>
>; on
if istr=="0x00F7C03F"
then
=>wakeup
=>%ws% wakeup
endif
; off
>
>; off
if istr=="0x00F740BF"
then
=>power1 0
=>%ws% power1 0
endif
;white
>
>;white
if istr=="0x00F7E01F"
then
=>color 000000ff
=>%ws% color 000000ff
endif
;red
>
>;red
if istr=="0x00F720DF"
then
=>color ff000000
=>%ws% color ff000000
endif
;green
>
>;green
if istr=="0x00F7A05F"
then
=>color 00ff0000
=>%ws% color 00ff0000
endif
;blue
>
>;blue
if istr=="0x00F7609F"
then
=>color 0000ff00
=>%ws% color 0000ff00
endif
; dimmer up
>
>; dimmer up
if istr=="0x00F700FF"
then
=>dimmer +
=>%ws% dimmer +
endif
;dimmer down
>
>;dimmer down
if istr=="0x00F7807F"
then
=>dimmer -
=>%ws% dimmer -
endif
istr=""
>
>istr=""
[Back To Top](#top)
------------------------------------------------------------------------------
#### Fast Polling
; expand strings to hold websend
>; expand strings to hold websend
**>D 25**
sw=0
ws="websend [192.168.178.86]"
@ -707,18 +706,17 @@ timer=0
hold=0
toggle=0
**>B**
>**>B**
; gpio 5 button input
spinm(5,0)
; fast section 100ms
>; fast section 100ms
**>F**
sw=pin[5]
; 100 ms timer
timer+=1
; 3 seconds long press
>
>; 3 seconds long press
; below 0,5 short press
if sw==0
and timer>5
@ -729,8 +727,8 @@ then
toggle^=1
=>%ws% power1 %toggle%
endif
if sw>0
>
>if sw>0
then
;pressed
if timer>30
@ -757,7 +755,7 @@ endif
#### Switching By Recognizing Mains Power Frequency
[#6085 (comment)](../issues/6085#issuecomment-512353010)
**>D**
>**>D**
sw1=0
sw2=0
cnt1=0
@ -767,15 +765,15 @@ timer2=0
toggle1=0
toggle2=0
**>B**
>**>B**
=>print "WiFi 2-Gang Switch Script"
**>F**
>**>F**
; Counter1/2 and Relay1/2 configured in template
cnt1=pc[1]
cnt2=pc[2]
if chg[cnt1]>0
>
>if chg[cnt1]>0
then
; counter1 has changed, switch is on
sw1=1
@ -783,8 +781,8 @@ else
; no change switch is off
sw1=0
endif
if chg[cnt2]>0
>
>if chg[cnt2]>0
then
; counter2 has changed, switch is on
sw2=1
@ -792,12 +790,12 @@ else
; no change switch is off
sw2=0
endif
; 100 ms timer
>
>; 100 ms timer
timer1+=1
timer2+=1
if sw1==0
>
>if sw1==0
and timer1>2
and timer1<30
then
@ -805,13 +803,13 @@ then
toggle1^=1
=>Power1 %toggle1%
endif
if sw1==0
>
>if sw1==0
then
timer1=0
endif
if sw2==0
>
>if sw2==0
and timer2>2
and timer2<30
then
@ -819,8 +817,8 @@ then
toggle2^=1
=>Power2 %toggle2%
endif
if sw2==0
>
>if sw2==0
then
timer2=0
endif