LED On/OFF Recipe
This recipe uses a single button as an input to switch a LED on and off.
This recipe can run this on a board like the NodeMCU development boards with a button and a LED.
Here the FLASH button (D3) is used to switch on and off one of the LEDs (D4).
This is can be achieved by using 3 elements:
- The digital input element captures the signal from the momentary button
- The switch element handles the on/off logic
- The digital output element controls the voltage level of the output pin with the LED.
The digital input elements transfers the physical signal (LOW when the button in pressed) to a logical level (1 when the button is pressed) using the “invert” property with 1. The internal pullup is enabled to keep the input on HIGH when no button is pressed. On nodemcu boards there is also an external resistor doing this but on other pins this is required.
When the input signal goes low an action is sent to the switch for toggling.
The switch element exactly was built to get the wanted on/off toggling. The onValue action will be send every time the output value is changing and the action with the actual value is sent to the digital output.
The digital output element is configured to create a LOW level when the LED should light up.
env.json Configuration
The env.json
can be taken from the board description because it has no special logic for this recipe:
{
"device": {
"main": {
"name": "nodeding",
"description": "nodeMCU board config",
"reboottime": "24h",
"button": "D3",
"led": "D4",
"I2C-SDA": "D2",
"I2C-SCL": "D1"
}
},
"ota": {
"main": {
"port": 8266,
"passwd": "123",
"description": "allow Over the Air Updates"
}
},
"ssdp": {
"0": {
"manufacturer": "Matthias Hertel"
}
}
}
config.json Configuration
{
"digitalin": {
"in": {
"description": "Input momentary button",
"pin": "D3",
"invert": 1,
"pullup": 1,
"onLow": "switch/light?toggle=1"
}
},
"switch": {
"light": {
"description": "Control light level",
"value": 0,
"onValue": "digitalout/led?value=$v"
}
},
"digitalout": {
"led": {
"pin": "D4",
"invert": "true",
"description": "Builtin LED on Port D4"
}
}
}