enocean

Differences

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

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
enocean [16.03.2018 05:42] – [esp8266] Pascal Suterenocean [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://github.com/marvinroger/async-mqtt-client|Async MQTT client]] for mqtt obviously   * [[https://github.com/marvinroger/async-mqtt-client|Async MQTT client]] for mqtt obviously
-  * [[https://github.com/me-no-dev/ESPAsyncTCP|ESPAsyncTCP]] to plot debug messages to a netcat server while working with the enOcean module on the serial port.. turns out i could still use Serial for debugging as well as to receive enOcean messages, so this was not needed. As a matter of fact it prevented the MQTT Client class to operate correctly. However, I wrote a little gide on Using the [[ESP AsyncPrinter]] just to keep my example of how i got it to work for later reference. +  * <del>[[https://github.com/me-no-dev/ESPAsyncTCP|ESPAsyncTCP]] to plot debug messages to a netcat server while working with the enOcean module on the serial port.. turns out i could still use Serial for debugging as well as to receive enOcean messages, so this was not needed.</del> As a matter of fact it prevented the MQTT Client class to operate correctly. However, I wrote a little gide on Using the [[ESP AsyncPrinter]] just to keep my example of how i got it to work for later reference. 
   * [[https://github.com/tzapu/WiFiManager|WiFiManager]] seems to do all we need to make the final device easy to get configured without a serial connection (has automatic fall back to AP mode with captive portal config page).    * [[https://github.com/tzapu/WiFiManager|WiFiManager]] seems to do all we need to make the final device easy to get configured without a serial connection (has automatic fall back to AP mode with captive portal config page). 
   * [[https://bitbucket.org/charly37/arduino_enocean_lib/downloads/|arduino enocean library]] with a [[http://djynet.net/?p=635|description from the developer]]   * [[https://bitbucket.org/charly37/arduino_enocean_lib/downloads/|arduino enocean library]] with a [[http://djynet.net/?p=635|description from the developer]]
 +  * [[https://github.com/KoljaWindeler/ESP8266_mqtt_pwm_pir_temp/tree/master/JKW_MQTT_PWM_PIR_TEMP/src|a similar project]] i found only after starting this. maybe i could just expand that one rather than writing my own? a shame i found it so late. 
  
 ===== setup and development ===== ===== setup and development =====
Line 164: 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
  
 +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 <ESP8266WiFi.h>          //ESP8266 Core WiFi Library (you most likely already have this in your sketch)
 +#include <DNSServer.h>            //Local DNS Server used for redirecting all requests to 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 <EnOceanMsg.h>           //http://djynet.net/?p=635
 +#include <AsyncMqttClient.h>       //https://github.com/marvinroger/async-mqtt-client
 +
 +int enableDebugging=2; // 0 = off, 1=Serial, 2=mqtt
 +const char *mqttServer="192.168.168.1";
 +int mqttPort=1883;
 +
 +EnOceanMsg _Msg;
 +WiFiManager wifiManager;
 +bool firstround=true;
 +int lastPayload=0;
 +AsyncMqttClient mqtt;
 +bool mqttConnected=false;
 +String mqttBaseTopic="enocean";
 +
 +
 +String topic;
 +
 +void onMqttConnect(bool sessionPresent){
 +  debug("connected to mqtt server");
 +  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.");
 +  }
 +}
 +
 +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, mqttPort);
 +  mqtt.connect();
 +}
 +
 +void debug(const char *message){
 +  switch (enableDebugging) {
 +    case 1:
 +      Serial.println(message);
 +      break;
 +    case 2:
 +      mqtt.publish(String(mqttBaseTopic+"/debug").c_str(), 1, true, message);
 +      break;
 +  }
 +}
 +
 +void debugHex(int payload){
 +  switch (enableDebugging) {
 +    case 1:
 +      Serial.println(payload,HEX);
 +      break;
 +    case 2:
 +      mqtt.publish(String(mqttBaseTopic+"/debug").c_str(), 1, true, String(payload,HEX).c_str());
 +      break;
 +  }  
 +}
 +
 +
 +void loop() {
 +  // put your main code here, to run repeatedly:
 +  int payload=0;
 +  uint32_t senderId=0;
 +  
 +  wifiManager.process();
 +  if(firstround){
 +    debug("loop started");
 +    firstround=false;
 +  }
 +  _Msg.decode();
 +  if(_Msg.dataAvailable() == true){
 +    payload=_Msg.getPayload();
 +    if(payload!=lastPayload){
 +      debug("new payload received:");
 +      debugHex(payload);
 +      lastPayload=payload;
 +      senderId=_Msg.getSenderId();
 +      debugHex(senderId);
 +      if(senderId > 0 && mqttConnected){
 +        if(payload > 0){
 +          topic = String(mqttBaseTopic+"/"+senderId+"/"+payload);
 +          debug("publish ON message");
 +          uint16_t packetIdPub1 = mqtt.publish(topic.c_str(), 1, false, "ON");
 +        } else {
 +          debug("publish OFF message");
 +          uint16_t packetIdPub1 = mqtt.publish(topic.c_str(), 1, false, "OFF");
 +        }
 +      }
 +    }
 +  }
 +  delay(50);
 +}
 +</code>
  • enocean.1521175355.txt.gz
  • Last modified: 16.03.2018 05:42
  • by Pascal Suter