Initial commit

This commit is contained in:
fauresystems
2019-11-03 09:34:43 +01:00
commit 3fe6e980c2
36 changed files with 3188 additions and 0 deletions
@@ -0,0 +1,109 @@
/* BlinkOnBridgeProps.ino
MIT License (c) Marie Faure <dev at faure dot systems>
Adapt the Blink example (https://www.arduino.cc/en/tutorial/blink) as a
simple MQTT props. Avoid delay() calls (except short ones) in loop() to
ensure CPU for MQTT protocol. Use PropsAction checks instead.
Copy and change it to build your fist Arduino connected props, you will
only be limited by your imagination.
Requirements: install ArduinoProps.zip library.
*/
#include <Bridge.h>
#include "ArduinoProps.h"
// If you're running our Escape Room control software (Room 2.0) you have to respect
// prpos inbox/outbox syntax Room/[escape room name]/Props/[propsname]/inbox|outbox
// https://live-escape.net/go/room
BridgeProps props(u8"Arduino Contrôleur", // as MQTT client id, should be unique per client for given broker
u8"Room/Demoniak/Props/Arduino Contrôleur/inbox",
u8"Room/Demoniak/Props/Arduino Contrôleur/outbox",
"192.168.1.42", // your MQTT server IP address
1883); // your MQTT server port;
PropsDataLogical clignoter(u8"clignote", u8"oui", u8"non", true);
PropsDataLogical led(u8"led");
void clignote(); // forward
PropsAction clignoteAction = PropsAction(1000, clignote);
void setup()
{
Bridge.begin();
//updateBrokerAdressFromFile("/root/broker", &props); // if you're running our Escape Room control software (Room 2.0)
props.addData(&clignoter);
props.addData(&led);
props.begin(InboxMessage::run);
pinMode(LED_BUILTIN, OUTPUT); // initialize digital pin LED_BUILTIN as an output
// At this point, the broker is not connected yet
}
void loop()
{
props.loop();
led.setValue(digitalRead(LED_BUILTIN)); // read I/O
clignoteAction.check(); // do your stuff, don't freeze the loop with delay() calls
}
void clignote()
{
if (clignoter.value()) {
led.setValue(!led.value());
digitalWrite(LED_BUILTIN, led.value() ? HIGH : LOW);
}
}
void InboxMessage::run(String a) {
if (a == u8"app:startup")
{
props.sendAllData();
props.sendDone(a);
}
else if (a == "clignoter:1")
{
clignoter.setValue(true);
props.sendAllData(); // all data change, we don't have to be selctive then
props.sendDone(a); // acknowledge props command action
}
else if (a == "clignoter:0")
{
clignoter.setValue(false);
props.sendAllData(); // all data change, we don't have to be selctive then
props.sendDone(a); // acknowledge props command action
}
else
{
// acknowledge omition of the props command
props.sendOmit(a);
}
}
void updateBrokerAdressFromFile(const char* broker_file, BridgeProps* props)
{
// broker IP address is stored in Linino file systems and updated with ssh command by Room 2.0
IPAddress ip;
Process _process;
_process.begin("cat");
_process.addParameter(broker_file); // for ssh remotely set broker address
_process.run(); // run the process and wait for its termination
String b;
while (_process.available() > 0) {
char c = _process.read();
b += c;
}
b.trim();
if (ip.fromString(b.c_str())) props->setBrokerIpAddress(ip);
}
@@ -0,0 +1,220 @@
/* BlinkOnBridgePubSub.ino
MIT License (c) Marie Faure <dev at faure dot systems>
Adapt the Blink example (https://www.arduino.cc/en/tutorial/blink) as a
simple MQTT props with PubSubClient.
*/
#include <Bridge.h>
#include <BridgeClient.h>
#include <Process.h>
#include <PubSubClient.h>
#include <VariableTimedAction.h>
#define BROKER "192.168.1.42" // your MQTT server IP address
#define PROPS_NAME u8"Arduino Contrôleur" // as MQTT client id, should be unique per client for given broker
// If you're running our Escape Room control software (Room 2.0) you have to respect
// props inbox/outbox syntax Room/[escape room name]/Props/[propsname]/inbox|outbox
// https://live-escape.net/go/room
#define PROPS_INBOX u8"Room/Demoniak/Props/Arduino Contrôleur/inbox"
#define PROPS_OUTBOX u8"Room/Demoniak/Props/Arduino Contrôleur/outbox"
// Yun can store broker IP address in Linino file systems, and updatred with ssh command
#define YUN_BROKER_FILE "/root/broker"
void publishAll(); // forward
void publishChanges(); // forward
class Blinking : public VariableTimedAction {
private:
//this method will be called at your specified interval
unsigned long run() {
//swicth LED if blinking mode
if (blink) {
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
}
//return code of 0 indicates no change to the interval
//if the interval must be changed, then return the new interval
return 0;
}
public:
bool blink;
};
class PublishAllData : public VariableTimedAction {
private:
unsigned long run() {
publishAll();
return 0;
}
};
class PublishChangedData : public VariableTimedAction {
private:
unsigned long run() {
publishChanges();
return 0;
}
};
Blinking blinking;
bool led_ref;
bool blink_ref;
BridgeClient _ethClient;
PubSubClient _client(_ethClient);
IPAddress _brokerAddress;
void callback(char*, byte*, unsigned int); // forward
// MQTT
unsigned long lastReconnection(0L);
PublishAllData publishAllData;
PublishChangedData publishChangedData;
void setup()
{
Bridge.begin();
Process p;
p.begin("cat");
p.addParameter(YUN_BROKER_FILE);
p.run(); // run the process and wait for its termination
String b;
while (p.available() > 0) {
char c = p.read();
b += c;
}
b.trim();
if (!_brokerAddress.fromString(b.c_str())) _brokerAddress.fromString(BROKER);
_client.setServer(_brokerAddress, 1883);
_client.setCallback(callback);
publishAllData.start(30000);
publishChangedData.start(400);
blinking.blink = true;
blinking.start(1000);
blink_ref = false;
pinMode(LED_BUILTIN, OUTPUT); // initialize digital pin LED_BUILTIN as an output
digitalWrite(LED_BUILTIN, HIGH);
led_ref = LOW;
// At this point, the broker is not connected yet
}
void loop()
{
if (_client.connected())
{
_client.loop();
}
else if (millis() > lastReconnection)
{
lastReconnection += 5000L;
if (_client.connect(PROPS_NAME, PROPS_OUTBOX, 2, true, "DISCONNECTED"))
{
_client.publish(PROPS_OUTBOX, "CONNECTED", true);
_client.subscribe(PROPS_INBOX, 1); // max QoS is 1 for PubSubClient subsciption
lastReconnection = 0L;
}
}
// automation code should be here (nothing to do for this example)
VariableTimedAction::updateActions();
}
void publishAll()
{
String buf = "DATA";
bool led = digitalRead(LED_BUILTIN);
buf += u8" led=" + (led ? String("1") : String("0"));
led_ref = led;
buf += u8" clignote=" + (blinking.blink ? String("oui") : String("non"));
blink_ref = blinking.blink;
_client.publish(PROPS_OUTBOX, buf.c_str());
}
void publishChanges()
{
String buf = "DATA";
bool led = digitalRead(LED_BUILTIN);
if (led != led_ref)
{
buf += u8" led=" + (led ? String("1") : String("0"));
led_ref = led;
}
if (blinking.blink != blink_ref)
{
buf += u8" clignote=" + (blinking.blink ? String("oui") : String("non"));
blink_ref = blinking.blink;
}
if (buf.length() > 4)
_client.publish(PROPS_OUTBOX, buf.c_str());
}
void publishDone(String a)
{
a = "DONE " + a;
_client.publish(PROPS_OUTBOX, a.c_str());
}
void onInboxMessage(String a) {
if (a == u8"app:startup")
{
publishAll();
publishDone(a);
}
else if (a == "clignoter:1")
{
digitalWrite(LED_BUILTIN, HIGH);
blinking.blink = true;
publishAll(); // all data change, we don't have to be selctive then
publishDone(a); // acknowledge props command action
}
else if (a == "clignoter:0")
{
digitalWrite(LED_BUILTIN, LOW);
blinking.blink = false;
publishAll(); // all data change, we don't have to be selctive then
publishDone(a); // acknowledge props command action
}
else
{
// acknowledge omition of the props command
_client.publish(PROPS_OUTBOX, String("OMIT " + a).c_str());
}
}
void callback(char* topic, byte* payload, unsigned int len)
{
if (len)
{
char* p = (char*)malloc(len + 1);
memcpy(p, payload, len);
p[len] = '\0';
if (String(p) == "@PING")
_client.publish(PROPS_OUTBOX, "PONG");
else
onInboxMessage(p);
free(p);
}
}
@@ -0,0 +1,113 @@
/* BlinkOnEthernetProps.ino
MIT License (c) Marie Faure <dev at faure dot systems>
Adapt the Blink example (https://www.arduino.cc/en/tutorial/blink) as a
simple MQTT props. Avoid delay() calls (except short ones) in loop() to
ensure CPU for MQTT protocol. Use PropsAction checks instead.
Copy and change it to build your fist Arduino connected props, you will
only be limited by your imagination.
Requirements: install ArduinoProps.zip library.
*/
#include <Ethernet.h>
#include <IPAddress.h>
#include "ArduinoProps.h"
byte mac[] = { 0xFC, 0xFA, 0xEA, 0x00, 0x00, 0x02 }; //<<< MAKE SURE IT'S UNIQUE IN YOUR NETWORK!!!
String ip = "192.168.1.19"; //<<< ENTER YOUR IP ADDRESS HERE ("" for DHCP)
// Builtin led is not available with the shield
#undef LED_BUILTIN
#define LED_BUILTIN 8
// If you're running our Escape Room control software (Room 2.0) you have to respect
// prpos inbox/outbox syntax Room/[escape room name]/Props/[propsname]/inbox|outbox
// https://live-escape.net/go/room
EthernetProps props(u8"Arduino Contrôleur", // as MQTT client id, should be unique per client for given broker
u8"Room/Demoniak/Props/Arduino Contrôleur/inbox",
u8"Room/Demoniak/Props/Arduino Contrôleur/outbox",
"192.168.1.42", // your MQTT server IP address
1883); // your MQTT server port;
PropsDataLogical clignoter(u8"clignote", u8"oui", u8"non", true);
PropsDataLogical led(u8"led");
void clignote(); // forward
PropsAction clignoteAction = PropsAction(1000, clignote);
void setup()
{
// can do more static IP configuration
//Ethernet.setSubnetMask(IPAddress());
//Ethernet.setGatewayIP(IPAddress());
//Ethernet.setDnsServerIP(IPAddress());
IPAddress ipa;
if (!ipa.fromString(ip)) {
// attempt a DHCP connection:
if (!Ethernet.begin(mac)) {
// if DHCP fails, start with a hard-coded address:
Ethernet.begin(mac, IPAddress(10, 90, 90, 239));
}
}
else {
Ethernet.begin(mac, ipa);
}
delay(1500); // time for shield stuff
props.addData(&clignoter);
props.addData(&led);
props.begin(InboxMessage::run);
pinMode(LED_BUILTIN, OUTPUT); // initialize digital pin LED_BUILTIN as an output
// At this point, the broker is not connected yet
}
void loop()
{
props.loop();
led.setValue(digitalRead(LED_BUILTIN)); // read I/O
clignoteAction.check(); // do your stuff, don't freeze the loop with delay() calls
}
void clignote()
{
if (clignoter.value()) {
led.setValue(!led.value());
digitalWrite(LED_BUILTIN, led.value() ? HIGH : LOW);
}
}
void InboxMessage::run(String a) {
if (a == u8"app:startup")
{
props.sendAllData();
props.sendDone(a);
}
else if (a == "clignoter:1")
{
clignoter.setValue(true);
props.sendAllData(); // all data change, we don't have to be selctive then
props.sendDone(a); // acknowledge props command action
}
else if (a == "clignoter:0")
{
clignoter.setValue(false);
props.sendAllData(); // all data change, we don't have to be selctive then
props.sendDone(a); // acknowledge props command action
}
else
{
// acknowledge omition of the props command
props.sendOmit(a);
}
}
@@ -0,0 +1,105 @@
/* BlinkOnWifiProps.ino
MIT License (c) Marie Faure <dev at faure dot systems>
Adapt the Blink example (https://www.arduino.cc/en/tutorial/blink) as a
simple MQTT props. Avoid delay() calls (except short ones) in loop() to
ensure CPU for MQTT protocol. Use PropsAction checks instead.
Copy and change it to build your fist Arduino connected props, you will
only be limited by your imagination.
Requirements: install ArduinoProps.zip library.
*/
#include "ArduinoProps.h"
// Setup your WiFi network
const char* ssid = "";
const char *passphrase = "";
// Builtin led is not available with the shield
#undef LED_BUILTIN
#define LED_BUILTIN 8
// If you're running our Escape Room control software (Room 2.0) you have to respect
// prpos inbox/outbox syntax Room/[escape room name]/Props/[propsname]/inbox|outbox
// https://live-escape.net/go/room
WifiProps props(u8"Arduino Contrôleur", // as MQTT client id, should be unique per client for given broker
u8"Room/Demoniak/Props/Arduino Contrôleur/inbox",
u8"Room/Demoniak/Props/Arduino Contrôleur/outbox",
"192.168.1.42", // your MQTT server IP address
1883); // your MQTT server port;
PropsDataLogical clignoter(u8"clignote", u8"oui", u8"non", true);
PropsDataLogical led(u8"led");
void clignote(); // forward
PropsAction clignoteAction = PropsAction(1000, clignote);
void setup()
{
// can do more static IP configuration
//WiFi.config(IPAddress(), IPAddress(), IPAddress());
//WiFi.config(IPAddress(), IPAddress());
//WiFi.config(IPAddress());
WiFi.begin(ssid, passphrase);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
props.addData(&clignoter);
props.addData(&led);
props.begin(InboxMessage::run);
pinMode(LED_BUILTIN, OUTPUT); // initialize digital pin LED_BUILTIN as an output
// At this point, the broker is not connected yet
}
void loop()
{
props.loop();
led.setValue(digitalRead(LED_BUILTIN)); // read I/O
clignoteAction.check(); // do your stuff, don't freeze the loop with delay() calls
}
void clignote()
{
if (clignoter.value()) {
led.setValue(!led.value());
digitalWrite(LED_BUILTIN, led.value() ? HIGH : LOW);
}
}
void InboxMessage::run(String a) {
if (a == u8"app:startup")
{
props.sendAllData();
props.sendDone(a);
}
else if (a == "clignoter:1")
{
clignoter.setValue(true);
props.sendAllData(); // all data change, we don't have to be selctive then
props.sendDone(a); // acknowledge props command action
}
else if (a == "clignoter:0")
{
clignoter.setValue(false);
props.sendAllData(); // all data change, we don't have to be selctive then
props.sendDone(a); // acknowledge props command action
}
else
{
// acknowledge omition of the props command
props.sendOmit(a);
}
}