Simple relay control, offering onscreen On and Off buttons with LED indicator.
Obviously you are not likely to need the LED as well as the coloured button, Also a Toggle button that changes colour from green when off to red when on. but better to offer both options so you can choose what to keep or remove. The Toggle button styling can be applied to the On and Off buttons if wished.
Configured for the standard Sonoff pins - 0 for button, 12 for relay, 13 for LED. The hardware button toggles the relay On or Off, the LED echoes relay status.
Basic:
title$ = "Simple Relay"
led = 1 'Onscreen LED$ variable, 0=on 1=off, ledpin = 13 'Sonoff green LED (active low) ledoff = 1 'Sonoff LED Off state pin.mode ledpin, output pin(ledpin) = ledoff relaypin = 12 'Sonoff relay (active high) relayoff = 0 'Sonoff Relay Off state pin.mode relaypin, output pin(relaypin) = 0 buttonpin = 0 'Sonoff button (active low) pin.mode buttonpin, input, pullup interrupt buttonpin, pressed ledstat$ = "green" 'Toggle button colour gosub paint onhtmlreload paint wait paint: cls a$ = a$ + |<br><div style='display: table; margin-right:auto;margin-left:auto;text-align:center;'>| a$ = a$ + title$ + "<br><br>" a$ = a$ + led$(led) a$ = a$ + "<br><br>" + button$("Instant On",relayon) + string$(9," ") + button$("Toggle", toggle, "butled") + string$(9," ") + button$("Instant Off",relayoff) a$ = a$ + cssid$("butled", "height:3em; font-size:1.5em; border-radius:.4em; padding:.5em; color:white; background:" + ledstat$ + ";") a$ = a$ + |</div>| html a$ a$ = "" return pressed: if pin(buttonpin) = 0 then gosub toggle return relayon: if pin(relaypin) = relayoff then if relayoff = 0 then pin(relaypin) = 1 else pin(relaypin) = 0 endif pin(ledpin) = 1 - ledoff html cssid$("butled", "background:red;") led = 0 refresh return relayoff: refresh if pin(relaypin) <> relayoff then pin(relaypin) = relayoff pin(ledpin) = ledoff html cssid$("butled", "background:green;") led = 1 refresh return toggle: if pin(relaypin) = relayoff then gosub relayon else gosub relayoff return '-------------------- End --------------------- |