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 revisionBoth sides next revision
enocean [16.03.2018 05:42] – [esp8266] Pascal Suterenocean [16.03.2018 05:45] – [MQTT] Pascal Suter
Line 167: Line 167:
 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
  
 +<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 <AsyncPrinter.h>
 +#include <EnOceanMsg.h>           //http://djynet.net/?p=635
 +#include <AsyncMqttClient.h>       //https://github.com/marvinroger/async-mqtt-client
  
 +bool enableOnlineDebugging=true;
 +const char *debuggerHost="192.168.168.48";
 +const char *mqttServer="192.168.168.1";
 +int mqttPort=1883;
 +
 +EnOceanMsg _Msg;
 +AsyncPrinter ap;
 +WiFiManager wifiManager;
 +bool debugOnline=false;
 +bool firstround=true;
 +int lastPayload=0;
 +AsyncMqttClient mqtt;
 +bool mqttConnected=false;
 +String mqttBaseTopic="enOcean";
 +
 +void onMqttConnect(bool sessionPresent){
 +  debug("connected to mqtt server");
 +  mqttConnected=true;
 +}
 +
 +void setup() {
 +  // put your setup code here, to run once:
 +  Serial.begin(57600);
 +  if(enableOnlineDebugging){
 +    wifiManager.setDebugOutput(false);
 +  }
 +  wifiManager.autoConnect();
 +  wifiManager.startWebPortal();
 +  ap.connect(debuggerHost,3333);
 +  mqtt.setServer(mqttServer,mqttPort);
 +  mqtt.onConnect(onMqttConnect);
 +  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("connected");
 +        debug("initiate mqtt connection");
 +        mqtt.connect();
 +        break;
 +      } else {
 +        delay(500);
 +      }
 +    }
 +  }
 +}
 +
 +void debug(const char *message){
 +  if(debugOnline){
 +    ap.println(message);
 +  } else {
 +    Serial.println("debugger is not online");
 +  }
 +  _Msg.decode();
 +  if (_Msg.dataAvailable() == true){
 +    
 +  }
 +}
 +
 +void debugHex(int payload){
 +  if(debugOnline){
 +    ap.println(payload,HEX);
 +  }  
 +}
 +
 +
 +void loop() {
 +  // put your main code here, to run repeatedly:
 +  int payload=0;
 +  uint32_t senderId=0;
 +  char * topic;
 +  
 +  wifiManager.process();
 +  if(firstround){
 +    debug("hello world");
 +    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(payload > 0 && senderId > 0 && mqttConnected){
 +        //String t = String(mqttBaseTopic+"/"+senderId+"/"+payload);
 +        //char topic[sizeof(t)];
 +        //t.toCharArray(topic,sizeof(topic));
 +        debug("publish ON message");
 +        uint16_t packetIdPub1 = mqtt.publish("enocean/button", 1, true, "ON");
 +      } else {
 +        debug("publish OFF message");
 +        uint16_t packetIdPub1 = mqtt.publish("enocean/button", 1, true, "OFF");
 +      }
 +    }
 +  }
 +  delay(50);
 +}
 +</code>
  • enocean.txt
  • Last modified: 16.03.2018 06:18
  • by Pascal Suter