enocean

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
enocean [16.03.2018 06:15] – [esp8266] Pascal Suterenocean [16.03.2018 06:18] (current) – [MQTT] Pascal Suter
Line 165: Line 165:
 ==== MQTT ==== ==== MQTT ====
 next up is MQTT support throught he [[https://github.com/marvinroger/async-mqtt-client|Async MQTT Library]].  next up is MQTT support throught he [[https://github.com/marvinroger/async-mqtt-client|Async MQTT Library]]. 
 +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://github.com/marvinroger/async-mqtt-client/releases|latest release]] as zip file and then add it via Sketch->Include Library->Add .ZIP library download the [[https://github.com/marvinroger/async-mqtt-client/releases|latest release]] as zip file and then add it via Sketch->Include Library->Add .ZIP library
  
-this is just a copy paste of a code that somewhat worked.. before i clean it out and re-post here+of course you will need an mqtt brokeryou 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> <code cpp>
Line 175: Line 178:
 #include <ESP8266WebServer.h>     //Local WebServer used to serve the configuration portal #include <ESP8266WebServer.h>     //Local WebServer used to serve the configuration portal
 #include <WiFiManager.h>          //https://github.com/tzapu/WiFiManager WiFi Configuration Magic #include <WiFiManager.h>          //https://github.com/tzapu/WiFiManager WiFi Configuration Magic
-#include <AsyncPrinter.h> 
 #include <EnOceanMsg.h>           //http://djynet.net/?p=635 #include <EnOceanMsg.h>           //http://djynet.net/?p=635
 #include <AsyncMqttClient.h>       //https://github.com/marvinroger/async-mqtt-client #include <AsyncMqttClient.h>       //https://github.com/marvinroger/async-mqtt-client
  
-bool enableOnlineDebugging=true; +int enableDebugging=2// 0 off, 1=Serial, 2=mqtt
-const char *debuggerHost="192.168.168.48";+
 const char *mqttServer="192.168.168.1"; const char *mqttServer="192.168.168.1";
 int mqttPort=1883; 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; AsyncMqttClient mqtt;
 bool mqttConnected=false; bool mqttConnected=false;
-String mqttBaseTopic="enOcean";+String mqttBaseTopic="enocean"
 + 
 + 
 +String topic;
  
 void onMqttConnect(bool sessionPresent){ void onMqttConnect(bool sessionPresent){
   debug("connected to mqtt server");   debug("connected to mqtt server");
   mqttConnected=true;   mqttConnected=true;
 +  uint16_t packetIdPub1 = mqtt.publish(String(mqttBaseTopic+"/online").c_str(), 1, true, "now");
 +}
 +void onMqttPublish(uint16_t packetId) {
 +  if(enableDebugging==1){
 +    Serial.println("Publish acknowledged.");
 +  }
 } }
  
Line 202: Line 210:
   // 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();
-  ap.connect(debuggerHost,3333); 
-  mqtt.setServer(mqttServer,mqttPort); 
   mqtt.onConnect(onMqttConnect);   mqtt.onConnect(onMqttConnect);
-  if(enableOnlineDebugging){ +  mqtt.onPublish(onMqttPublish); 
-    //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("connected"); +
-        debug("initiate mqtt connection"); +
-        mqtt.connect(); +
-        break; +
-      } else { +
-        delay(500); +
-      } +
-    } +
-  }+
 } }
  
 void debug(const char *message){ void debug(const char *message){
-  if(debugOnline){ +  switch (enableDebugging) { 
-    ap.println(message); +    case 1: 
-  } else { +      Serial.println(message); 
-    Serial.println("debugger is not online")+      break; 
-  } +    case 2: 
-  _Msg.decode()+      mqtt.publish(String(mqttBaseTopic+"/debug").c_str(), 1, true, message); 
-  if (_Msg.dataAvailable() == true){ +      break;
-    +
   }   }
 } }
  
 void debugHex(int payload){ void debugHex(int payload){
-  if(debugOnline){ +  switch (enableDebugging) { 
-    ap.println(payload,HEX);+    case 1: 
 +      Serial.println(payload,HEX)
 +      break; 
 +    case 2: 
 +      mqtt.publish(String(mqttBaseTopic+"/debug").c_str(), 1, true, String(payload,HEX).c_str()); 
 +      break;
   }     }  
 } }
Line 249: Line 248:
   int payload=0;   int payload=0;
   uint32_t senderId=0;   uint32_t senderId=0;
-  char * topic; 
      
   wifiManager.process();   wifiManager.process();
   if(firstround){   if(firstround){
-    debug("hello world");+    debug("loop started");
     firstround=false;     firstround=false;
   }   }
Line 265: Line 263:
       senderId=_Msg.getSenderId();       senderId=_Msg.getSenderId();
       debugHex(senderId);       debugHex(senderId);
-      if(payload > 0 && senderId > 0 && mqttConnected){ +      if(senderId > 0 && mqttConnected){ 
-        //String t = String(mqttBaseTopic+"/"+senderId+"/"+payload); +        if(payload > 0){ 
-        //char topic[sizeof(t)]; +          topic = String(mqttBaseTopic+"/"+senderId+"/"+payload); 
-        //t.toCharArray(topic,sizeof(topic)); +          debug("publish ON message"); 
-        debug("publish ON message"); +          uint16_t packetIdPub1 = mqtt.publish(topic.c_str(), 1, false, "ON"); 
-        uint16_t packetIdPub1 = mqtt.publish("enocean/button", 1, true, "ON"); +        } else { 
-      } else { +          debug("publish OFF message"); 
-        debug("publish OFF message"); +          uint16_t packetIdPub1 = mqtt.publish(topic.c_str(), 1, false, "OFF"); 
-        uint16_t packetIdPub1 = mqtt.publish("enocean/button", 1, true, "OFF");+        }
       }       }
     }     }
  • enocean.txt
  • Last modified: 16.03.2018 06:18
  • by Pascal Suter