Unit B · What your device can sense
What we are making: a page with one button that switches a relay on and off, and remembers which way round it is.
Last session's page had two links because the board did not know what state it was in. Today there is one button, and it flips whatever the current state happens to be — which means the board has to remember.
That is what bool isOn is for. One variable, kept between requests, holding the
truth about the machine. Every device you use has some version of this, and getting it wrong
is why a light switch app sometimes shows the wrong state.
isOn = !isOn flips it.That last step is worth two minutes of discussion. The second phone shows the old state until it refreshes, because a web page is a photograph, not a window — it shows how things were at the moment it loaded.
Everything in Unit C exists to deal with this.
| What you see | Why | What to do |
|---|---|---|
| No click | No power to the module, or the wrong pin. | VCC on VIN, IN on GPIO 26, GND shared. |
| It clicks twice per press | The browser requested the address twice. | Use the redirect shown, not a plain page reply. |
| The page always says OFF | isOn is being reset. | Declare it outside every function, not inside one. |
| Board resets on the click | The relay coil is drawing too much. | Power the module from a separate battery, sharing GND. |
In this session