Web Blink
If you've worked through Two more blinks, you have a board whose LED blinks on a timer. Here the timer goes, and a web browser does the switching. You'll put the board on your WiFi network, find it from your computer, and see what it means for a board to have an open port.
The blink flow was a timer → function → gpio out. The timer gives way to an http in node, which runs a web server on the board, and the function gains a second output for the reply that goes back to the browser.
You can build the flow step by step, or start from the finished flow, http-blink.flow.json, in the tutorials folder, with File → Open…. Tutorial flows says what to change before you deploy.
If a step misbehaves on the way, start at Debugging & troubleshooting.
Build the flow
On the network the request is the event, so the timer goes and the LED holds the level you last asked for.
- Delete the timer.
- Drag an http in onto the canvas. Set port to
9999and path to/toggle. Leave method asGET. -
Select the function. Set outputs to
2and replace its code with:on = not context.get('on', False) context.set('on', on) msg['payload'] = on reply = dict(msg) reply['payload'] = 'LED ' + ('on' if on else 'off') return [msg, reply] -
Drag an http response onto the canvas.
- Wire http in → function. Wire the function's first output to gpio out and its second output to http response.
- Drag a wifi status and a debug onto the canvas. Tick full message on the debug node and wire wifi status → debug.
The board joins WiFi
The board joins the network named in the flow — so tell the flow which network. Flow files hold no
passwords or other secrets: the WiFi details live in the .thingstudio folder in your home directory
(~/.thingstudio/credentials/), as credentials. Each credential is named, and you can keep
several — one per network — and pick the right one per flow. See
The ThingStudio folder.
- On the
http innode's property panel, click + next to the WiFi field to make a new credential (✎ edits an existing one). Give the credential a name, your network's SSID and its password, and save. - The WiFi field on the
http innode (and on thewifi statusnode) picks that credential — one WiFi network per flow, shared by every WiFi node in it.
Compile stops with a one-line error if the flow's WiFi has no saved network behind it; the FAQ's needs-a-WiFi-network entry is that error.
Name the board, then deploy
- Connect the board over USB, as in Connect your board, and choose Tools → Board settings….
Give the board a network name, such as
blink. A name lets your computer find the board without an IP address. - Click Compile → Deploy. The board swaps in the flow and joins your WiFi.
A few seconds after the deploy, the console prints the wifi status message, with a payload of true, and the
node's dot shows connected (see node status when the dot is something else). The LED is dark:
nothing drives the pin until a request does.
Find the board
- By name: open
http://blink.local:9999/togglein your browser. The name is the one from Board settings plus.local; port 9999 and path/toggleare the port and path of thehttp innode. - By IP: the
debugnode prints the whole wifi status message, which includesip. (In the example file, tick full message on thedebugnode first; it prints only the payload otherwise.) The fields are listed under wifi status. Openhttp://<ip>:9999/toggle.
If .local names don't resolve on your network (they don't everywhere, and they don't across VPN tunnels at all —
multicast doesn't pass), the IP address is the dependable one.
The page answers LED on or LED off, and the LED does what it says. Expect the first answer after an idle stretch
to take up to a second: the board's WiFi wakes on the request (see
Why do http in requests take a second to answer?).
How it works
The request is the only event this flow gets, and each one is handled on its own: between requests the LED holds its
level, remembered in the context toggle from the last tutorial.
The function has two outputs. The first carries the new state to gpio out. The second carries reply to
http response, which answers the browser. return [msg, reply] routes one message to
each output, as in Node-RED; see the function node.
The server is open
http in runs an unauthenticated server on the board: whatever reaches that network can call every path. Keep those
routes non-sensitive, or keep the board off shared networks. See http in for what the server
does and doesn't do.
Next
- http in — routes, methods, timeouts and the server's limits.
- Blink from anywhere — the next tutorial: the same LED through an MQTT broker.
- mqtt publish and mqtt subscribe — the nodes behind it.
- Connecting over WiFi — the board as an editor peer on your network, not just a web server.