Last active
September 26, 2019 19:19
-
-
Save abobija/e69c371027c5293a14cc970146d591d8 to your computer and use it in GitHub Desktop.
Code written in YouTube video https://youtu.be/2mlmr_HwWgo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | |
<title>ESP32</title> | |
<style> | |
button { | |
padding: 20px; | |
font-size: 20px; | |
margin: 5px; | |
border-radius: 30px; | |
border: 2px solid #949494; | |
background: #e4e4e4; | |
} | |
button.on { | |
background: #92ff92; | |
} | |
</style> | |
</head> | |
<body> | |
<h1>ESP32</h1> | |
<div id="buttons"></div> | |
<script> | |
const btns = document.getElementById('buttons'); | |
function renderStates(states) { | |
for(let i = 0; i < 5; i++) { | |
const btn = document.getElementById(`b${i+1}`); | |
btn.classList.remove('on'); | |
if(states[i]) { | |
btn.classList.add('on'); | |
} | |
} | |
} | |
for(let i = 1; i <= 5; i++) { | |
const btn = document.createElement('button'); | |
btn.id = `b${i}`; | |
btn.innerText = `LED ${i}`; | |
btn.addEventListener('click', () => { | |
fetch('/toggle-led', { | |
method: 'POST', | |
body: JSON.stringify({ 'id': i }) | |
}).then(resp => resp.json().then(states => renderStates(states))); | |
}); | |
btns.appendChild(btn); | |
} | |
setTimeout(() => fetch('/states') | |
.then(resp => resp.json().then(states => renderStates(states))), 500); | |
</script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local api = nil | |
local LED_GPIOS = { 27, 26, 25, 33, 32 } | |
for _, pin in pairs(LED_GPIOS) do | |
gpio.config({ gpio = pin, dir = gpio.IN_OUT }) | |
gpio.write(pin, 0) | |
end | |
local function states() | |
local res = {} | |
for _, pin in pairs(LED_GPIOS) do | |
table.insert(res, gpio.read(pin)) | |
end | |
return res | |
end | |
local function init() | |
if api ~= nil then return end | |
api = require('api32') | |
.create() | |
.on_get('/', 'index.html') | |
.on_post('/toggle-led', function(jreq) | |
if jreq == nil | |
or type(jreq.id) ~= 'number' | |
or jreq.id > #LED_GPIOS then return end | |
local led_gpio = LED_GPIOS[jreq.id] | |
if gpio.read(led_gpio) == 1 then | |
gpio.write(led_gpio, 0) | |
else | |
gpio.write(led_gpio, 1) | |
end | |
return states() | |
end) | |
.on_get('/states', states) | |
end | |
wifi.mode(wifi.STATION) | |
wifi.sta.config({ | |
ssid = 'Renault 1.9D', | |
pwd = 'renault19', | |
auto = false | |
}) | |
wifi.sta.on('got_ip', init) | |
wifi.start() | |
wifi.sta.connect() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment