Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
enocean [16.03.2018 05:35] – [add enOcean] 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 the library: | ||
- | cd <arduino directory>/ | ||
- | git clone https:// | ||
- | |||
- | for my development machine to receive those messages i need to open a socket with '' | ||
- | 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 [[https:// | ||
- | <code cpp> | ||
- | while(_client-> | ||
- | delay(1); | ||
- | </ | ||
- | |||
- | let's add '' | ||
- | |||
- | <code cpp> | ||
- | #include < | ||
- | #include < | ||
- | #include < | ||
- | #include < | ||
- | #include < | ||
- | |||
- | bool enableOnlineDebugging=true; | ||
- | const char *debuggerHost=" | ||
- | |||
- | AsyncPrinter ap; | ||
- | WiFiManager wifiManager; | ||
- | bool debugOnline=false; | ||
- | bool firstround=true; | ||
- | |||
- | void setup() { | ||
- | // put your setup code here, to run once: | ||
- | Serial.begin(115200); | ||
- | wifiManager.autoConnect(); | ||
- | wifiManager.startWebPortal(); | ||
- | ap.connect(debuggerHost, | ||
- | if(enableOnlineDebugging){ | ||
- | //block for maximum 5 seconds trying to reach the debugger | ||
- | for ( int i = 0; i < 10 ; i++){ | ||
- | if(ap.connected()){ | ||
- | debugOnline=true; | ||
- | ap.println(" | ||
- | break; | ||
- | } else { | ||
- | delay(500); | ||
- | } | ||
- | } | ||
- | } | ||
- | } | ||
- | |||
- | void debug(const char *message){ | ||
- | if(debugOnline){ | ||
- | ap.println(message); | ||
- | } else { | ||
- | Serial.println(" | ||
- | } | ||
- | } | ||
- | void loop() { | ||
- | // put your main code here, to run repeatedly: | ||
- | wifiManager.process(); | ||
- | if(firstround){ | ||
- | debug(" | ||
- | firstround=false; | ||
- | } | ||
- | } | ||
- | </ | ||
- | you should now see "setup done" and "hello world" on your terminal with '' | ||
==== add enOcean ==== | ==== add enOcean ==== | ||
Line 235: | Line 165: | ||
==== MQTT ==== | ==== MQTT ==== | ||
next up is MQTT support throught he [[https:// | next up is MQTT support throught he [[https:// | ||
+ | I should have done this right after implementing the WiFiManager as one could also use mqtt for debugging messages rather than trying to use AsyncPrinter which did not work in conjunction with mqtt and was painful to figure out in the first place. so now this contains a solution for debugging via mqtt. | ||
download the [[https:// | download the [[https:// | ||
+ | of course you will need an mqtt broker. you can subscribe to the enocean topic on your development machine using for example mosquitto client: | ||
+ | mosquitto_sub -h mqtt.psuter.ch -v -t enocean/# | ||
+ | <code cpp> | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | int enableDebugging=2; | ||
+ | const char *mqttServer=" | ||
+ | int mqttPort=1883; | ||
+ | |||
+ | EnOceanMsg _Msg; | ||
+ | WiFiManager wifiManager; | ||
+ | bool firstround=true; | ||
+ | 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() { | ||
+ | // put your setup code here, to run once: | ||
+ | Serial.begin(57600); | ||
+ | if(enableDebugging!=1){ | ||
+ | wifiManager.setDebugOutput(false); | ||
+ | } | ||
+ | wifiManager.autoConnect(); | ||
+ | wifiManager.startWebPortal(); | ||
+ | mqtt.onConnect(onMqttConnect); | ||
+ | mqtt.onPublish(onMqttPublish); | ||
+ | mqtt.setServer(mqttServer, | ||
+ | mqtt.connect(); | ||
+ | } | ||
+ | |||
+ | void debug(const char *message){ | ||
+ | switch (enableDebugging) { | ||
+ | case 1: | ||
+ | Serial.println(message); | ||
+ | break; | ||
+ | case 2: | ||
+ | mqtt.publish(String(mqttBaseTopic+"/ | ||
+ | break; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | void debugHex(int payload){ | ||
+ | switch (enableDebugging) { | ||
+ | case 1: | ||
+ | Serial.println(payload, | ||
+ | break; | ||
+ | case 2: | ||
+ | mqtt.publish(String(mqttBaseTopic+"/ | ||
+ | break; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | |||
+ | void loop() { | ||
+ | // put your main code here, to run repeatedly: | ||
+ | int payload=0; | ||
+ | uint32_t senderId=0; | ||
+ | | ||
+ | wifiManager.process(); | ||
+ | if(firstround){ | ||
+ | debug(" | ||
+ | firstround=false; | ||
+ | } | ||
+ | _Msg.decode(); | ||
+ | if(_Msg.dataAvailable() == true){ | ||
+ | payload=_Msg.getPayload(); | ||
+ | if(payload!=lastPayload){ | ||
+ | debug(" | ||
+ | debugHex(payload); | ||
+ | lastPayload=payload; | ||
+ | senderId=_Msg.getSenderId(); | ||
+ | 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(), | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | delay(50); | ||
+ | } | ||
+ | </ |