Skip to content

Commit

Permalink
🚀 Added reconnection interval functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
guido-visser committed Oct 17, 2020
1 parent 0c2b8c6 commit 8d82b0a
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/a_GLOBAL/a_GLOBAL.ino
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ String M_TALLY = "";
int VMIX_PORT = 8099; //USES THE TCP API PORT, THIS IS FIXED IN VMIX
int TALLY_NR = 1;
int BRIGHTNESS = 12; //100%
int CONN_INT = 0;

String semver = "2.1.0";
String semver = "2.2.0";
4 changes: 4 additions & 0 deletions src/a_GLOBAL/b_SETTINGS.ino
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ void loadSettings()
VMIX_IP = preferences.getString("vmix_ip");
}

if(preferences.getUInt("conn_int")){
CONN_INT = preferences.getUInt("conn_int");
}

if(preferences.getString("m_tally").length() > 0){
M_TALLY = preferences.getString("m_tally");
}
Expand Down
12 changes: 12 additions & 0 deletions src/a_GLOBAL/c_MAIN.ino
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ boolean started = false;
// Time measure
int interval = 5000;
unsigned long lastCheck = 0;
unsigned long lastConnCheck = 0;
unsigned long lastBattCheck = 0;
unsigned long lastAccCheck = 0;
int screenRotation = 3;
Expand Down Expand Up @@ -140,11 +141,19 @@ void loop()
renderBatteryLevel();
}
}

if (CONN_INT != 0 && !client.connected() && !apEnabled && millis() > lastConnCheck + (CONN_INT * 1000))
{
client.stop();
Serial.println("Reconnecting, based on given interval");
singleReconnect();
}

if (!client.connected() && !apEnabled && millis() > lastCheck + interval)
{
client.stop();
lastCheck = millis();
Serial.println("Not connected anymore");
noConnectionTovMix();
}
}
Expand Down Expand Up @@ -214,6 +223,9 @@ int getBatteryLevel(void)
}

void renderCurrentScreen(){
if(!client.connected()){
return;
}
if(screen == 0){
showTallyScreen();
} else if (screen == 1) {
Expand Down
17 changes: 17 additions & 0 deletions src/a_GLOBAL/d_VMIX.ino
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,23 @@ boolean connectTovMix(bool recursive)
}
}

void singleReconnect(){
resetScreen();
M5.Lcd.println("Connecting to vMix...");
if (client.connect(&(VMIX_IP[0]), VMIX_PORT))
{
lastConnCheck = millis();
connectedTovMix = true;
M5.Lcd.println("Connected to vMix!");

// Subscribe to the tally events
client.println("SUBSCRIBE TALLY");
showTallyScreen();
} else {
lastConnCheck = millis();
}
}

boolean retryConnectionvMix(int tryCount) {
cls();
M5.Lcd.setTextSize(1);
Expand Down
12 changes: 10 additions & 2 deletions src/a_GLOBAL/f_WEBSERVER.ino
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ String HEADER = "<!DOCTYPE html>\
<html lang='en'>\
<head>\
<meta charset='UTF-8'>\
<meta name='viewport' content='width=device-width, initial-scale=1.0, shrink-to-fit=no'>\
<meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no'>\
<title>vMix M5Stick-C Tally</title>\
<style>\
@import url(https://fonts.googleapis.com/css2?family=Open+Sans&display=swap);.wrapper,input[type=text],input[type=number],input[type=submit],select{width:100%;box-sizing:border-box}body,html{background:#2b2b2b;color:#eee;padding:0;margin:0;font-family:'Open Sans',verdana,sans-serif}.wrapper{padding:10px}.wrapper h1{text-align:center}input[type=text],input[type=number]{margin-bottom:10px}input,select{background-color:#6d6d6d;color:#f0f0f0;border:1px solid #000;font-size:18px;height:35px;padding:0 5px}input[type=submit]{height:50px;margin:10px auto}@media screen and (min-width:600px){.wrapper{width:600px;margin:0 auto}}\
Expand All @@ -21,7 +21,7 @@ void handle_root()
String tally = (String)TALLY_NR;
String bright = (String)BRIGHTNESS;
String HTML = HEADER;
HTML += "<div class=wrapper data-theme=light><h1>vMix M5Stack Tally Settings</h1><form action=/save id=frmData method=post onsubmit=return!1><div>SSID:<br><select id=ssid><option disabled selected>Scanning wifi...</select></div><div class=ssidCustomDiv style=display:none>Hidden SSID Name:<br><input id=ssidCustom type=text value='" + (String)WIFI_SSID + "'name=ssidCustom></div><div>Password:<br><input id=pwd type=text value='" + (String)WIFI_PASS + "'name=pwd></div><div>vMix IP Address:<br><input id=vmixip type=text value='" + (String)VMIX_IP + "'name=vmixip></div><div>Main Tally Number:<br><input id=tally_num type=number value='" + tally + "'name=tally_num max=1000 min=1></div><div>Multi Input (comma separated):<br><input id=m_tally type=text value='" + (String)M_TALLY + "'name=m_tally></div><div>Brightness:<br><select id=drpBright name=bright><option value=7>0%<option value=8>20%<option value=9>40%<option value=10>60%<option value=11>80%<option value=12>100%</select></div><input id=btnSave type=submit value=SAVE class='btn btn-primary'></form><h2>Reconnect to vMix</h2><form action=/reconnect id=frmReconnect method=post onsubmit=return!1><input id=btnReconnect type=submit value=RECONNECT></form></div><script>const btnSave = document.querySelector('#btnSave'); const btnReconnect = document.querySelector('#btnReconnect'); const drpBright = document.querySelector('#drpBright'); const ssidSelect = document.querySelector('#ssid'); drpBright.value = '" + bright + "'; btnSave.addEventListener('click', async function(e){ e.preventDefault(); let ssid = document.querySelector('#ssid').value; if(ssid === '__hidden__'){ ssid = document.querySelector('#ssidCustom').value; } const pwd = document.querySelector('#pwd').value; const vmixip = document.querySelector('#vmixip').value; const m_tally = document.querySelector('#m_tally').value; const frmData = document.querySelector('#frmData'); const tally_num = document.querySelector('#tally_num').value; let formData = new FormData(); formData.append('ssid', ssid.trim()); formData.append('pwd',pwd.trim()); formData.append('vmixip', vmixip.trim()); formData.append('m_tally', m_tally.trim().replace(/[^0-9,]+/g, '')); formData.append('tally_num', tally_num); btnSave.setAttribute('disabled', ''); const res = await fetch('/save', { method: 'POST', cache: 'no-cache', referrerPolicy: 'no-referrer', body: formData }); if(res.status === 200){ btnSave.value = 'SETTINGS SAVED!'; await setTimeout(()=>{btnSave.value = 'SAVE';}, 3000); } btnSave.removeAttribute('disabled'); }); btnReconnect.addEventListener('click', function(e){ e.preventDefault(); fetch('/reconnect'); }); ssidSelect.addEventListener('change', e => { const val = e.target.value; const ssidcd = document.querySelector('.ssidCustomDiv'); if(val === '__hidden__'){ ssidcd.style.display = 'block'; } else { ssidcd.style.display = 'none'; } }); document.addEventListener('DOMContentLoaded', async function(){ const res = await fetch('/scanNetwork'); res.text().then(text=>{ let networks = [text]; let str = ''; if(text.indexOf('|||') !== -1){ networks = text.split('|||'); } let sel = document.getElementById('ssid'); sel.innerHTML = ''; let existingNetwork = ''; networks.forEach(network => { let opt = document.createElement('option'); opt.appendChild( document.createTextNode(network) ); opt.value = network; if('" + (String)WIFI_SSID + "' === network){ existingNetwork = network; } sel.appendChild(opt); }); let opt = document.createElement('option'); opt.appendChild( document.createTextNode('Hidden network') ); opt.value = '__hidden__'; sel.appendChild(opt); if(existingNetwork !== ''){ sel.value = existingNetwork; } }); });</script>";
HTML += "<div class=wrapper data-theme=light><h1>vMix M5Stack Tally Settings</h1><form action=/save id=frmData method=post onsubmit=return!1><div>SSID:<br><select id=ssid><option disabled selected>Scanning wifi...</select></div><div class=ssidCustomDiv style=display:none>Hidden SSID Name:<br><input id=ssidCustom type=text value='" + (String)WIFI_SSID + "'name=ssidCustom></div><div>Password:<br><input id=pwd type=text value='" + (String)WIFI_PASS + "'name=pwd></div><div>vMix IP Address:<br><input id=vmixip type=text value='" + (String)VMIX_IP + "'name=vmixip></div><div>Main Tally Number:<br><input id=tally_num type=number value='" + tally + "'name=tally_num max=1000 min=1></div><div>Multi Input (comma separated):<br><input id=m_tally type=text value='" + (String)M_TALLY + "'name=m_tally></div><div>Reconnect interval (in seconds, 0 means no reconnection interval):<br><input id=conn_int type=number value='" + CONN_INT + "'name=conn_int></div><div>Brightness:<br><select id=drpBright name=bright><option value=7>0%<option value=8>20%<option value=9>40%<option value=10>60%<option value=11>80%<option value=12>100%</select></div><input id=btnSave type=submit value=SAVE class='btn btn-primary'></form><h2>Reconnect to vMix</h2><form action=/reconnect id=frmReconnect method=post onsubmit=return!1><input id=btnReconnect type=submit value=RECONNECT></form></div><script>const btnSave = document.querySelector('#btnSave'); const btnReconnect = document.querySelector('#btnReconnect'); const drpBright = document.querySelector('#drpBright'); const ssidSelect = document.querySelector('#ssid'); drpBright.value = '" + bright + "'; btnSave.addEventListener('click', async function(e){ e.preventDefault(); let ssid = document.querySelector('#ssid').value; if(ssid === '__hidden__'){ ssid = document.querySelector('#ssidCustom').value; } const pwd = document.querySelector('#pwd').value; const vmixip = document.querySelector('#vmixip').value; const m_tally = document.querySelector('#m_tally').value; const frmData = document.querySelector('#frmData'); const tally_num = document.querySelector('#tally_num').value; const conn_int = parseInt(document.querySelector('#conn_int').value, 10) || 0; let formData = new FormData(); formData.append('ssid', ssid.trim()); formData.append('pwd',pwd.trim()); formData.append('vmixip', vmixip.trim()); formData.append('m_tally', m_tally.trim().replace(/[^0-9,]+/g, '')); formData.append('tally_num', tally_num); formData.append('conn_int', conn_int); btnSave.setAttribute('disabled', ''); const res = await fetch('/save', { method: 'POST', cache: 'no-cache', referrerPolicy: 'no-referrer', body: formData }); if(res.status === 200){ btnSave.value = 'SETTINGS SAVED!'; await setTimeout(()=>{btnSave.value = 'SAVE';}, 3000); } btnSave.removeAttribute('disabled'); }); btnReconnect.addEventListener('click', function(e){ e.preventDefault(); fetch('/reconnect'); }); ssidSelect.addEventListener('change', e => { const val = e.target.value; const ssidcd = document.querySelector('.ssidCustomDiv'); if(val === '__hidden__'){ ssidcd.style.display = 'block'; } else { ssidcd.style.display = 'none'; } }); document.addEventListener('DOMContentLoaded', async function(){ const res = await fetch('/scanNetwork'); res.text().then(text=>{ let networks = [text]; let str = ''; if(text.indexOf('|||') !== -1){ networks = text.split('|||'); } let sel = document.getElementById('ssid'); sel.innerHTML = ''; let existingNetwork = ''; networks.forEach(network => { let opt = document.createElement('option'); opt.appendChild( document.createTextNode(network) ); opt.value = network; if('" + (String)WIFI_SSID + "' === network){ existingNetwork = network; } sel.appendChild(opt); }); let opt = document.createElement('option'); opt.appendChild( document.createTextNode('Hidden network') ); opt.value = '__hidden__'; sel.appendChild(opt); if(existingNetwork !== ''){ sel.value = existingNetwork; } }); });</script>";
HTML += FOOTER;

server.send(200, "text/html", HTML);
Expand All @@ -47,6 +47,7 @@ void handle_save()

String tally = server.arg("tally_num");
String bright = server.arg("bright");
String conn_int = server.arg("conn_int");

Serial.print("BRIGHTNESS: ");
Serial.println(bright);
Expand Down Expand Up @@ -75,6 +76,13 @@ void handle_save()

M_TALLY = server.arg("m_tally");
preferences.putString("m_tally", &(M_TALLY[0]));

if (conn_int != "")
{
CONN_INT = std::atoi(conn_int.c_str());
preferences.putUInt("conn_int", CONN_INT);
Serial.println("PUT CONN INT");
}

if (server.arg("vmixip") != "")
{
Expand Down
8 changes: 7 additions & 1 deletion src/a_GLOBAL/webui/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html lang='en'>
<head>
<meta charset='UTF-8'>
<meta name='viewport' content='width=device-width, initial-scale=1.0, shrink-to-fit=no'>
<meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no'>
<title>vMix M5Stick-C Tally</title>
<style>
@import url('https://fonts.googleapis.com/css2?family=Open+Sans&display=swap');
Expand Down Expand Up @@ -75,6 +75,10 @@ <h1>vMix M5Stack Tally Settings</h1>
Multi Input (comma separated):<br/>
<input type='text' id='m_tally' name='m_tally' value='" + (String)M_TALLY + "'>
</div>
<div>
Reconnect interval (in seconds, 0 means no reconnection interval):<br/>
<input type='number' id='conn_int' name='conn_int' value='" + CONN_INT + "'>
</div>
<div>
Brightness:<br/>
<select id='drpBright' name='bright'>
Expand Down Expand Up @@ -111,12 +115,14 @@ <h2>Reconnect to vMix</h2>
const m_tally = document.querySelector('#m_tally').value;
const frmData = document.querySelector('#frmData');
const tally_num = document.querySelector('#tally_num').value;
const conn_int = parseInt(document.querySelector('#conn_int').value, 10) || 0;
let formData = new FormData();
formData.append('ssid', ssid.trim());
formData.append('pwd',pwd.trim());
formData.append('vmixip', vmixip.trim());
formData.append('m_tally', m_tally.trim().replace(/[^0-9,]+/g, ''));
formData.append('tally_num', tally_num);
formData.append('conn_int', conn_int);
btnSave.setAttribute('disabled', '');
const res = await fetch('/save', {
method: 'POST',
Expand Down

0 comments on commit 8d82b0a

Please sign in to comment.