MQTT Things
We use the PCA9685 i2c module and have 16 servos, so, even though the board has the "ports" labeled as 0 through 15, we are going to stick to non-zero based numbers for devices. And, to keep to 2 digits, we will only allow 6 modules (either PCA9685 for servos or MCP23017 for I/Os), thus we will have /01 through /96 max.
That gives us /00 as the control point for either information about the node, or a place to set things for the node.
/00:
A request command ends in a '?', which the node will replace with a '_', so it does not need to answer the same question again when it reboots.
VER? - software version
MCM? - number of modules attached
RPT? - report the state of all pins or servos
ENA? - report all enables pins or servos
IPA? - what is your IP Address
DIR? - pin directions, i.e. inputs or outputs
LCK? - locked pins or servos
PCFG? - spit out the JSON config string for the modules
SAV! - save the node and module info
Servos:
/01 - /16: (Active is Thrown and Inactive is Closed)
THROWN/ON/1/Active/ACTIVE/preset_active - move to the THROWN position
CLOSED/OFF/0/Inactive/INACTIVE/preset_inactive - move to the CLOSED position
Tnnn - set the the THROWN servo position and move it there, need 00/SAV1
T+nnn - increase the THROWN servo position and move it there, need 00/SAV1
T-nnn - decrease the THROWN servo position and move it there, need 00/SAV1
Cnnn - set the the CLOSED servo position and move it there, need 00/SAV1
C+nnn - increase the CLOSED servo position and move it there, need 00/SAV1
C-nnn - decrease the CLOSED servo position and move it there, need 00/SAV1
L - lock this servo (lock does not persist, cycling power removes lock)
U - unlock this servo
WebSockets to MQTT broker:
HTML for MQTT with mqttws31.js in same folder:
<html>
<head>
<title>Windhoek Humpyard</title>
</head>
<body>
<script src="mqttws31.js" type="text/javascript"></script>
<script type="text/javascript">
client = new Paho.MQTT.Client( "10.10.10.13", 9001, "client_id" );
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
var options = {
onSuccess:onConnect, onFailure:doFail
} // options
// connect the client
client.connect( options );
// called when the client connects
function onConnect( ) {
console.log( "onConnect" );
client.subscribe( "TxNamib/servo/0001/01" );
} // onConnect()
function doFail(e){
console.log(e);
} // doFail()
function publish( topic, payload ) {
message = new Paho.MQTT.Message( payload );
message.destinationName = topic;
client.send(message);
} // publish()
// called when the client loses its connection
function onConnectionLost( responseObject ) {
if( responseObject.errorCode !== 0 ) {
console.log( "onConnectionLost:" + responseObject.errorMessage );
} // if( )
} // onConnectionLost( ) // called when a message arrives
function onMessageArrived(message) {
console.log( "onMessageArrived:" + message.payloadString );
} // onMessageArrived()
function HillWVB( ) {
publish( "TxNamib/servo/0001/01", "1" );
publish( "TxNamib/servo/0001/02", "0" );
publish( "TxNamib/servo/0001/04", "0" );
document.getElementById('route').innerHTML = 'Walvisbaai';
}
function HillKZB( ) {
publish( "TxNamib/servo/0001/01", "0" );
publish( "TxNamib/servo/0001/02", "0" );
publish( "TxNamib/servo/0001/04", "0" );
document.getElementById('route').innerHTML = 'Kranzberg';
}
function HillOTA( ) {
publish( "TxNamib/servo/0001/01", "0" );
publish( "TxNamib/servo/0001/02", "0" );
publish( "TxNamib/servo/0001/06", "1" );
document.getElementById('route').innerHTML = 'Otavi';
}
function HillOTJ( ) {
publish( "TxNamib/servo/0001/01", "0" );
publish( "TxNamib/servo/0001/02", "0" );
publish( "TxNamib/servo/0001/05", "1" );
document.getElementById('route').innerHTML = 'Otjiwarongo';
}
function HillWDH3( ) {
publish( "TxNamib/servo/0001/01", "0" );
publish( "TxNamib/servo/0001/02", "0" );
publish( "TxNamib/servo/0001/05", "0" );
document.getElementById('route').innerHTML = 'Windhoek 3';
}
function HillWDH2( ) {
publish( "TxNamib/servo/0001/01", "0" );
publish( "TxNamib/servo/0001/02", "0" );
publish( "TxNamib/servo/0001/03", "1" );
document.getElementById('route').innerHTML = 'Windhoek 2';
}
function HillWDH1() {
publish( "TxNamib/servo/0001/01", "0" );
publish( "TxNamib/servo/0001/02", "0" );
publish( "TxNamib/servo/0001/03", "0" );
document.getElementById('route').innerHTML = 'Windhoek 1';
}
</script>
<div> Humpyard Hill </div>
<button onclick="HillWVB()" style="height:30px; width:150px">Walvisbaai</button><br>
<button onclick="HillKZB()" style="height:30px; width:150px">Kranzberg</button><br>
<button onclick="HillOTA()" style="height:30px; width:150px">Otavi</button><br>
<button onclick="HillOTJ()" style="height:30px; width:150px">Otjiwarongo</button><br>
<button onclick="HillWDH3()" style="height:30px; width:150px">Windhoek #3</button><br>
<button onclick="HillWDH2()" style="height:30px; width:150px">Windhoek #2</button><br>
<button onclick="HillWDH1()" style="height:30px; width:150px" autofocus>Windhoek #1</button><br>
<br>
<p id="route"></p>
</body>
</html>