Merge pull request #533 from pimoroni/automini-webio-fixes

fixed example for automation2040wmini
pull/686/head
Philip Howard 2023-02-23 16:41:08 +00:00 zatwierdzone przez GitHub
commit 206d4089d7
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
2 zmienionych plików z 374 dodań i 18 usunięć

Wyświetl plik

@ -0,0 +1,323 @@
<!DOCTYPE html>
<html>
<!-- HTML Codes by Quackit.com -->
<title>
</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<head>
<style>
body {background-color:#050505;background-repeat:no-repeat;background-position:top left;background-attachment:fixed;}
h1{font-family:Arial, sans-serif;color:#ffffff;}
h2{font-family:Arial, sans-serif;color:#ffffff;}
p {font-family:Arial, serif;font-size:16px;font-style:normal;font-weight:normal;color:#ffffff}
table.GeneratedTable {
width: 100%;
background-color: #ffffff;
border-collapse: collapse;
border-width: 2px;
border-color: #8f8f8f;
border-style: solid;
color: #000000;
}
table.GeneratedTable td, table.GeneratedTable th {
border-width: 2px;
border-color: #8f8f8f;
border-style: solid;
padding: 3px;
width: 50%;
}
table.GeneratedTable thead {
background-color: #fafafa;
}
</style>
</head>
<body>
<h1>Automation 2040 W Mini IO Interface</h1>
<p>This displays the status of all the Automation 2040 W Mini inputs and outputs.</p>
<p id="LedOn" ></p>
<h2>ADC Readings</h2>
<table class="GeneratedTable">
<thead>
<tr>
<th>IO</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<tr>
<td>ADC1</td>
<td id="ADC1"></td>
</tr>
<tr>
<td>ADC2</td>
<td id="ADC2"></td>
</tr>
<tr>
<td>ADC3</td>
<td id="ADC3"></td>
</tr>
</tbody>
</table>
<h2>Input Readings</h2>
<table class="GeneratedTable">
<thead>
<tr>
<th>IO</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<tr>
<td>INPUT1</td>
<td id="INPUT1"></td>
</tr>
<tr>
<td>INPUT2</td>
<td id="INPUT2"></td>
</tr>
</tbody>
</table>
<h2>Button Readings</h2>
<table class="GeneratedTable">
<thead>
<tr>
<th>IO</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<tr>
<td>Button A</td>
<td id="SW_A"></td>
</tr>
<tr>
<td>Button B</td>
<td id="SW_B"></td>
</tr>
</tbody>
</table>
<h2>Outputs</h2>
<table class="GeneratedTable">
<thead>
<tr>
<th>IO</th>
<th>Status</th>
<th>Toggle</th>
</tr>
</thead>
<tbody>
<tr>
<td>OUT1</td>
<td id="OUT1"></td>
<td><input type="button" onclick="toggleOutput('one')" value="Toggle"></input></td>
</tr>
<tr>
<td>OUT2</td>
<td id="OUT2"></td>
<td><input type="button" onclick="toggleOutput('two')" value="Toggle"></input></td>
</tr>
</tbody>
</table>
<h2>Relays</h2>
<table class="GeneratedTable">
<thead>
<tr>
<th>IO</th>
<th>Status</th>
<th>Toggle</th>
</tr>
</thead>
<tbody>
<tr>
<td>RELAY1</td>
<td id="RELAY1"></td>
<td><input type="button" onclick="toggleRelay('one')" value="Toggle"></input></td>
</tr>
</tbody>
</table>
<!-- Javascripts-->
<script>
var getUrl = window.location;
var baseUrl = getUrl .protocol + "//" + getUrl.host;
//prototype arrays for holding current state information
var outputState;
var relayState;
var ADCState;
var inputState;
var buttonState;
var LEDState;
//functions for updating current states
function updateOutputStates(){
fetch(baseUrl+ '/outputs')
.then((Response) => {
return Response.json()
})
.then((data) =>{
outputState = data;
console.log('outputs', outputState);
})
}
function updateRelayStates(){
fetch(baseUrl+ '/relays')
.then((Response) => {
return Response.json()
})
.then((data) =>{
relayState = data;
console.log('relays', relayState);
})
}
function updateInputStates(){
fetch(baseUrl+ '/inputs')
.then((Response) => {
return Response.json()
})
.then((data) =>{
inputState = data;
console.log('inputs', inputState);
})
}
function updateADCStates(){
fetch(baseUrl+ '/adcs')
.then((Response) => {
return Response.json()
})
.then((data) =>{
ADCState = data;
console.log('ADCs', ADCState);
})
}
function updateLEDStates(){
fetch(baseUrl+ '/leds')
.then((Response) => {
return Response.json()
})
.then((data) =>{
LEDState = data;
console.log('LEDs', LEDState);
})
}
function updateButtonStates(){
fetch(baseUrl+ '/buttons')
.then((Response) => {
return Response.json()
})
.then((data) =>{
buttonState = data;
console.log('Buttons', buttonState);
})
}
function getCurrentStates(){
updateOutputStates()
updateRelayStates()
updateInputStates()
updateADCStates()
updateButtonStates()
}
setInterval(getCurrentStates, 3000);
setInterval(displayADCValues, 1000);
setInterval(displayButtonValues, 1000);
setInterval(displayOutputValues, 1000);
setInterval(displayInputValues, 1000);
setInterval(displayRelayValues, 1000);
function displayADCValues(){
document.getElementById('ADC1').innerHTML = ADCState['one']
document.getElementById('ADC2').innerHTML = ADCState['two']
document.getElementById('ADC3').innerHTML = ADCState['three']
}
function displayInputValues(){
document.getElementById('INPUT1').innerHTML = inputState['one']
document.getElementById('INPUT2').innerHTML = inputState['two']
}
function displayButtonValues(){
document.getElementById('SW_A').innerHTML = buttonState['SW_A']
document.getElementById('SW_B').innerHTML = buttonState['SW_B']
}
function displayOutputValues(){
document.getElementById('OUT1').innerHTML = outputState['one']
document.getElementById('OUT2').innerHTML = outputState['two']
}
function displayRelayValues(){
document.getElementById('RELAY1').innerHTML = relayState['one']
}
//output Handlers
function updateRemote(funct, attribute, value ){
fetch(baseUrl+"/"+funct+"?"+attribute+"="+value)
.then(response => {
// indicates whether the response is successful (status code 200-299) or not
if (!response.ok) {
throw new Error(`Request failed with status ${reponse.status}`)
}
return response.json()
})
.then(data => {
console.log(data)
})
.catch(error => console.log(error))
}
function toggleOutput(outNumb){
var newState = '0'
var currentOutputState = outputState[outNumb]
if (currentOutputState == newState){
newState = '1';
}
else{
newState = '0'
}
updateRemote('outputs', outNumb, newState )
updateOutputStates()
}
function toggleRelay(outNumb){
var newState = '0'
var currentRelayState = relayState[outNumb]
if (currentRelayState == newState){
newState = '1';
}
else{
newState = '0'
}
updateRemote('relays', outNumb, newState )
updateRelayStates()
}
updateOutputStates()
updateRelayStates()
</script>
</body>
</html>

Wyświetl plik

@ -8,13 +8,21 @@ import time
board = Automation2040W()
is_mini = False
# Uncomment for Automation2040WMini
'''
from automation import Automation2040WMini
board = Automation2040WMini()
is_mini = True
'''
def status_handler(mode, status, ip):
print("Network: {}".format(WIFI_CONFIG.SSID))
status_text = "Connecting..."
board.conn_led(20.0)
board.conn_led(20)
if status is not None:
if status:
status_text = "Connection successful!"
@ -46,7 +54,10 @@ app = webserver()
# Static page
html_file = open('index.html', 'r')
if is_mini:
html_file = open('index_mini.html', 'r')
else:
html_file = open('index.html', 'r')
# WIFI settings
WIFI_COUNTRY = "GB" # Changeme!
@ -72,7 +83,9 @@ class LEDs:
if 'two' in data.keys():
board.output(1, int(data['two']))
if 'three' in data.keys():
board.output(2, int(data['three']))
if not is_mini:
board.output(2, int(data['three']))
return {'message': 'outputs updated'}, 201
@ -82,7 +95,10 @@ class inputs:
return {'message': 'no data provided'}, 404
def get(self, data):
return {"one": board.read_input(0), "two": board.read_input(1), "three": board.read_input(2), "four": board.read_input(3)}, 201
if not is_mini:
return {"one": board.read_input(0), "two": board.read_input(1), "three": board.read_input(2), "four": board.read_input(3)}, 201
else:
return {"one": board.read_input(0), "two": board.read_input(1), "three": "N/A", "four": "N/A"}, 201
def post(self, data):
return {'message': 'outputs updated'}, 201
@ -118,21 +134,28 @@ class outputs:
return {'message': 'no data provided'}, 404
def get(self, data):
if 'one' in data.keys():
board.output(0, bool(data['one']))
print(int(data['one']))
board.output(0, bool(int(data['one'])))
if 'two' in data.keys():
board.output(1, bool(data['two']))
board.output(1, bool(int(data['two'])))
if 'three' in data.keys():
board.output(2, bool(data['three']))
return {"one": bool(board.output(0)), "two": bool(board.output(1)), "three": bool(board.output(2))}, 201
if not is_mini:
board.output(2, bool(int(data['three'])))
if not is_mini:
return {"one": bool(board.output(0)), "two": bool(board.output(1)), "three": bool(board.output(2))}, 201
else:
return {"one": bool(board.output(0)), "two": bool(board.output(1)), "three": "N/A"}, 201
def post(self, data):
if 'one' in data.keys():
board.output(0, int(data['one']))
board.output(0, bool(int(data['one'])))
if 'two' in data.keys():
board.output(1, int(data['two']))
board.output(1, bool(int(data['two'])))
if 'three' in data.keys():
board.output(2, int(data['three']))
if not is_mini:
board.output(2, int(data['three']))
return {'message': 'outputs updated'}, 201
@ -143,20 +166,30 @@ class relays:
def get(self, data):
if 'one' in data.keys():
board.relay(0, int(data['one']))
if not is_mini:
board.relay(0, int(data['one']))
else:
board.relay(int(data['one']))
if 'two' in data.keys():
board.relay(1, int(data['two']))
if not is_mini:
board.relay(1, int(data['two']))
if 'three' in data.keys():
board.relay(2, int(data['three']))
return {"one": board.relay(0), "two": board.relay(1), "three": board.relay(2)}, 201
if not is_mini:
board.relay(2, int(data['three']))
if not is_mini:
return {"one": bool(board.relay(0)), "two": bool(board.relay(1)), "three": bool(board.relay(2))}, 201
else:
return {"one": bool(board.relay()), "two": "N/A", "three": "N/A"}, 201
def post(self, data):
if 'one' in data.keys():
board.relay(0, int(data['one']))
if 'two' in data.keys():
board.relay(1, int(data['two']))
if not is_mini:
board.relay(1, int(data['two']))
if 'three' in data.keys():
board.relay(2, int(data['three']))
if not is_mini:
board.relay(2, int(data['three']))
return {'message': 'outputs updated'}, 201
@ -188,7 +221,7 @@ def run():
app.add_resource(buttons, '/buttons')
# Connect to Wifi network
uasyncio.get_event_loop().run_until_complete(network_manager.client(WIFI_CONFIG.SSID, WIFI_CONFIG.PSK))
while (not network_manager.isconnected()):
while not network_manager.isconnected():
time.sleep(0.1)
app.run(host='0.0.0.0', port=80)