Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| enocean [13.03.2018 22:43] – [add the async printer] Pascal Suter | enocean [16.03.2018 06:18] (current) – [MQTT] Pascal Suter | ||
|---|---|---|---|
| Line 24: | Line 24: | ||
| here are a few libraries i have found beforehand and intend on trying out: | here are a few libraries i have found beforehand and intend on trying out: | ||
| * [[https:// | * [[https:// | ||
| - | * [[https:// | + | * <del>[[https:// | 
| * [[https:// | * [[https:// | ||
| * [[https:// | * [[https:// | ||
| + | * [[https:// | ||
| ===== setup and development ===== | ===== setup and development ===== | ||
| Line 62: | Line 63: | ||
| </ | </ | ||
| - | ==== add the async printer ==== | ||
| - | we use [[https:// | ||
| - | first we need to install | + | ==== add enOcean ==== | 
| - | cd <arduino directory>/ | + | now we can finally add the enocean receiver | 
| - | git clone https:// | + | |
| - | for my development machine to receive those messages i need to open a socket with '' | + | next we need the [[http://djynet.net/?p=635|enOceanMsg]] library.. however, i have modified | 
| - | nc -l 3333 | + | |
| - | and leave the terminal open. | + | |
| - | + | ||
| - | now we could integrate it as is into our code as you can see in the example | + | |
| - | <code cpp> | + | |
| - | while(_client-> | + | |
| - | delay(1); | + | |
| - | </ | + | |
| - | + | ||
| - | let's add '' | + | |
| + | extract the contents of this ZIP to the '' | ||
| <code cpp> | <code cpp> | ||
| #include < | #include < | ||
| Line 87: | Line 76: | ||
| #include < | #include < | ||
| #include < | #include < | ||
| + | #include < | ||
| bool enableOnlineDebugging=true; | bool enableOnlineDebugging=true; | ||
| const char *debuggerHost=" | const char *debuggerHost=" | ||
| + | EnOceanMsg _Msg; | ||
| AsyncPrinter ap; | AsyncPrinter ap; | ||
| WiFiManager wifiManager; | WiFiManager wifiManager; | ||
| bool debugOnline=false; | bool debugOnline=false; | ||
| bool firstround=true; | bool firstround=true; | ||
| + | int lastPayload=0; | ||
| void setup() { | void setup() { | ||
| // put your setup code here, to run once: | // put your setup code here, to run once: | ||
| - | Serial.begin(115200); | + | Serial.begin(57600); | 
| + | if(enableOnlineDebugging){ | ||
| + | wifiManager.setDebugOutput(false); | ||
| + | } | ||
| wifiManager.autoConnect(); | wifiManager.autoConnect(); | ||
| wifiManager.startWebPortal(); | wifiManager.startWebPortal(); | ||
| Line 107: | Line 101: | ||
| if(ap.connected()){ | if(ap.connected()){ | ||
| debugOnline=true; | debugOnline=true; | ||
| - | ap.println(" | + | ap.println(" | 
| break; | break; | ||
| } else { | } else { | ||
| Line 121: | Line 115: | ||
| } else { | } else { | ||
| Serial.println(" | Serial.println(" | ||
| + | } | ||
| + | _Msg.decode(); | ||
| + | if (_Msg.dataAvailable() == true){ | ||
| + |  | ||
| } | } | ||
| } | } | ||
| + | |||
| + | void debugHex(int payload){ | ||
| + | if(debugOnline){ | ||
| + | ap.println(payload, | ||
| + | } | ||
| + | } | ||
| + | |||
| + | |||
| void loop() { | void loop() { | ||
| // put your main code here, to run repeatedly: | // put your main code here, to run repeatedly: | ||
| + | int payload=0; | ||
| wifiManager.process(); | wifiManager.process(); | ||
| if(firstround){ | if(firstround){ | ||
| Line 130: | Line 137: | ||
| firstround=false; | firstround=false; | ||
| } | } | ||
| + | _Msg.decode(); | ||
| + | if(_Msg.dataAvailable() == true){ | ||
| + | payload=_Msg.getPayload(); | ||
| + | if(payload!=lastPayload){ | ||
| + | debug(" | ||
| + | debugHex(payload); | ||
| + | lastPayload=payload; | ||
| + | debugHex(_Msg.getSenderId()); | ||
| + | } | ||
| + | } | ||
| + | delay(50); | ||
| } | } | ||
| </ | </ | ||
| - | you should | + | now you should see output like this on your netcat console after you started the sketch | 
| + | < | ||
| + | connected | ||
| + | hello world | ||
| + | new payload received: | ||
| + | 70 | ||
| + | 3102F7 | ||
| + | new payload received: | ||
| + | 0 | ||
| + | 3102F7 | ||
| + | </ | ||
| + | '' | ||
| - | ==== add enOcean | + | ==== MQTT ==== | 
| - | now we can finally add the enocean receiver | + | next up is MQTT support throught he [[https:// | 
| + | I should have done this right after implementing | ||
| + | |||
| + | download | ||
| + | |||
| + | of course you will need an mqtt broker. you can subscribe | ||
| + | mosquitto_sub -h mqtt.psuter.ch -v -t enocean/# | ||
| - | next we need the [[http:// | ||
| - | extract the contents of this ZIP to the '' | ||
| <code cpp> | <code cpp> | ||
| #include < | #include < | ||
| Line 145: | Line 178: | ||
| #include < | #include < | ||
| #include < | #include < | ||
| - | #include < | ||
| #include < | #include < | ||
| + | #include < | ||
| - | bool enableOnlineDebugging=true; | + | int enableDebugging=2; // 0 = off, 1=Serial, 2=mqtt | 
| - | const char *debuggerHost=" | + | const char *mqttServer=" | 
| + | int mqttPort=1883; | ||
| EnOceanMsg _Msg; | EnOceanMsg _Msg; | ||
| - | AsyncPrinter ap; | ||
| WiFiManager wifiManager; | WiFiManager wifiManager; | ||
| - | bool debugOnline=false; | ||
| bool firstround=true; | bool firstround=true; | ||
| int lastPayload=0; | int lastPayload=0; | ||
| + | AsyncMqttClient mqtt; | ||
| + | bool mqttConnected=false; | ||
| + | String mqttBaseTopic=" | ||
| + | |||
| + | |||
| + | String topic; | ||
| + | |||
| + | void onMqttConnect(bool sessionPresent){ | ||
| + | debug(" | ||
| + | mqttConnected=true; | ||
| + | uint16_t packetIdPub1 = mqtt.publish(String(mqttBaseTopic+"/ | ||
| + | } | ||
| + | void onMqttPublish(uint16_t packetId) { | ||
| + | if(enableDebugging==1){ | ||
| + | Serial.println(" | ||
| + | } | ||
| + | } | ||
| + | |||
| void setup() { | void setup() { | ||
| // put your setup code here, to run once: | // put your setup code here, to run once: | ||
| Serial.begin(57600); | Serial.begin(57600); | ||
| - | if(enableOnlineDebugging){ | + | if(enableDebugging!=1){ | 
| wifiManager.setDebugOutput(false); | wifiManager.setDebugOutput(false); | ||
| } | } | ||
| wifiManager.autoConnect(); | wifiManager.autoConnect(); | ||
| wifiManager.startWebPortal(); | wifiManager.startWebPortal(); | ||
| - |  | + |  | 
| - |  | + |  | 
| - | //block for maximum 5 seconds trying to reach the debugger | + | mqtt.setServer(mqttServer, mqttPort); | 
| - | for ( int i = 0; i < 10 ; i++){ | + | mqtt.connect(); | 
| - | if(ap.connected()){ | + | |
| - | debugOnline=true; | + | |
| - | ap.println(" | + | |
| - | break; | + | |
| - | } else { | + | |
| - | delay(500); | + | |
| - | } | + | |
| - | } | + | |
| - | } | + | |
| } | } | ||
| void debug(const char *message){ | void debug(const char *message){ | ||
| - |  | + |  | 
| - |  | + |  | 
| - | } else { | + | Serial.println(message); | 
| - |  | + |  | 
| - | } | + |  | 
| - | _Msg.decode(); | + | mqtt.publish(String(mqttBaseTopic+"/debug").c_str(), 1, true, message); | 
| - | if (_Msg.dataAvailable() == true){ | + |  | 
| - | + | ||
| } | } | ||
| } | } | ||
| void debugHex(int payload){ | void debugHex(int payload){ | ||
| - |  | + |  | 
| - |  | + |  | 
| + | Serial.println(payload, | ||
| + | break; | ||
| + | case 2: | ||
| + | mqtt.publish(String(mqttBaseTopic+"/ | ||
| + | break; | ||
| } | } | ||
| } | } | ||
| Line 202: | Line 247: | ||
| // put your main code here, to run repeatedly: | // put your main code here, to run repeatedly: | ||
| int payload=0; | int payload=0; | ||
| + | uint32_t senderId=0; | ||
| + |  | ||
| wifiManager.process(); | wifiManager.process(); | ||
| if(firstround){ | if(firstround){ | ||
| - | debug(" | + | debug(" | 
| firstround=false; | firstround=false; | ||
| } | } | ||
| Line 214: | Line 261: | ||
| debugHex(payload); | debugHex(payload); | ||
| lastPayload=payload; | lastPayload=payload; | ||
| - |  | + |  | 
| + | debugHex(senderId); | ||
| + | if(senderId > 0 && mqttConnected){ | ||
| + | if(payload > 0){ | ||
| + | topic = String(mqttBaseTopic+"/" | ||
| + | debug(" | ||
| + | uint16_t packetIdPub1 = mqtt.publish(topic.c_str(), | ||
| + | } else { | ||
| + | debug(" | ||
| + | uint16_t packetIdPub1 = mqtt.publish(topic.c_str(), | ||
| + | } | ||
| + | } | ||
| } | } | ||
| } | } | ||
| Line 220: | Line 278: | ||
| } | } | ||
| </ | </ | ||
| - | now you should see output like this on your netcat console after you started the sketch and then pressed a button: | ||
| - | < | ||
| - | connected | ||
| - | hello world | ||
| - | new payload received: | ||
| - | 70 | ||
| - | 3102F7 | ||
| - | new payload received: | ||
| - | 0 | ||
| - | 3102F7 | ||
| - | </ | ||
| - | '' | ||