Client MQTT event with Adafruit_MQTT.h

Hi there,
I am working on publishing a temt6000 light intensity results through MQTT. It works so far sending the raw values as float. :partying_face:
May I missed something in the documentation, but I am having issues doing an "event" actually to use it on openhab3.
Can somebody pls guide me to a tutorial or HowTo in regards to "mqtt events"?

Publishing a value is an MQTT event. I don't understand what your problem is.

May I expressed myself not exact enough,
I would like to send a ON and OFF by MQTT to be used as "thing event" trigger in Openhab3.
I am failing sending the ON by

  Lichtevent.publish('ON');

through Adafruit MQTT library
MQTT.fx is receiving a "20302" only.

Try:

  Lichtevent.publish("ON");

Hi wildbill,
does not change the result

I tried this

 if (lichtsensor < 60) {
    Lichtevent.publish("ON");
  }

  if (lichtsensor > 100) {
    Lichtevent.publish("OFF");
  }

What value does lichtsensor have?

Posting your code would help too.

// Todo: WIFI connection to happen only 3 times
#include <Wire.h>
#include <SPI.h>
#include <SHT1x.h>
#include <ESP8266WiFi.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include "ThingSpeak.h"
#include "secrets.h"

WiFiClient  client;

#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BME280 bme; // I2C
unsigned long delayTime;

//*************General Variables
//uint32_t x = 0;
//uint32_t y = 0;


//PIN Definition
int Sens_pow1 = D5; //Sensor power supply um Sensor 1 abzuschalten
int Sens_pow2 = D6; //Sensor power supply um Sensor 2 abzuschalten
int Sens_pow3 = D7; //Sensor power supply um Sensor 2 abzuschalten
const int analogInPin = A0;  // ESP8266 Analog Pin für Lichtsensor
float lichtsensor = 0;


//*******SHT15
#define dataPin  D4
#define clockPin D3
SHT1x sht1x(dataPin, clockPin);


//************WLAN
const char* SSID = "******";
const char* PSK = "*********";

//Thingspeak
unsigned long myChannelNumber = 459831;
const char * myWriteAPIKey = "9T82EMEH6RFJCO9A";

int number1 = 0;
int number2 = random(0, 100);
int number3 = random(0, 100);
int number4 = random(0, 100);
String myStatus = "";


//************************* Adafruit.io MQTT Setup
#define AIO_SERVER      "192.168.0.19"
#define AIO_SERVERPORT  1883                   // use 8883 for SSL
#define AIO_USERNAME    "****"
#define AIO_KEY         "******"
#define ITEMNAME        "ESP8266-Wetter"


// Setup the MQTT client class
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);

//****************************** MQTT Feeds ***************************************/
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
Adafruit_MQTT_Publish AussenTemp = Adafruit_MQTT_Publish(&mqtt, ITEMNAME "/aussen/temp");
Adafruit_MQTT_Publish AussenHum = Adafruit_MQTT_Publish(&mqtt, ITEMNAME "/aussen/hum");
Adafruit_MQTT_Publish InnenTemp = Adafruit_MQTT_Publish(&mqtt, ITEMNAME "/innen/temp");
Adafruit_MQTT_Publish InnenHum = Adafruit_MQTT_Publish(&mqtt, ITEMNAME "/innen/hum");
Adafruit_MQTT_Publish Luftdruck = Adafruit_MQTT_Publish(&mqtt, ITEMNAME "/aussen/druck");
Adafruit_MQTT_Publish Licht = Adafruit_MQTT_Publish(&mqtt, ITEMNAME "/aussen/licht");
Adafruit_MQTT_Publish Lichtevent = Adafruit_MQTT_Publish(&mqtt, ITEMNAME "/aussen/lichtevent");

// Setup a feed called 'onoff' for subscribing to changes.
//Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/onoff");

/*************************** Sketch Code ************************************/

void MQTT_connect();

void setup() {
  Serial.begin(9600);
  delay(10);
  //*****************Start WLAN

  WiFi.mode(WIFI_STA);
  Serial.println("WIFI started");

  //*******************Start ThingspeakM
  ThingSpeak.begin(client);  // Initialize ThingSpeak

  //*********Sensoren
  while (!Serial);   // time to get serial running
  unsigned status;


  pinMode(Sens_pow1, OUTPUT);
  pinMode(Sens_pow2, OUTPUT);
  pinMode(Sens_pow3, OUTPUT);

  digitalWrite(Sens_pow1, HIGH);
  digitalWrite(Sens_pow2, HIGH);
  digitalWrite(Sens_pow3, HIGH);

  delay(1000);
  status = bme.begin(0x76);
}

void loop() {
  digitalWrite(Sens_pow1, HIGH);
  digitalWrite(Sens_pow2, HIGH);
  digitalWrite(Sens_pow3, HIGH);

  delay(1000); //Sensoren wachwerden lassen
  unsigned status;

  status = bme.begin(0x76);
  Serial.println("****************************************** ");
  Serial.print("Wachgeworden. Der Microcontroller läuft seit ");
  Serial.print(millis());
  Serial.println(" Millisekunden.");
  Serial.println(" ");
  Serial.println(" ");
  Serial.println(" ");

  // Sensor1 lesen
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h1 = sht1x.readHumidity(); // Read temperature as Celsius (the default)
  float t1 = sht1x.readTemperatureC();// Read temperature as Fahrenheit (isFahrenheit = true)


  // Sensor2 lesen
  float h2 = bme.readHumidity(); // Read humidty
  float t2 = bme.readTemperature();// Read temperature as degC(
  float p2 = (bme.readPressure() / 100.0F); //Read pressure level

  //Lichtsensor einlesen
  lichtsensor = analogRead(analogInPin);


  // Compute heat index in Celsius (isFahreheit = false)
  //  float hic1 = dht1.computeHeatIndex(t1, h1, false);
  Serial.print(F("INNEN Feuchte: "));
  Serial.print(h1);
  Serial.print(F("% INNEN Temperatur : "));
  Serial.print(t1);
  Serial.println(F("°C "));

  Serial.print(F("AUSSEN Feuchte: "));
  Serial.print(h2);
  Serial.print(F("%  AUSSEN Temperatur: "));
  Serial.print(t2);
  Serial.println(F("°C "));
  Serial.println("");

  Serial.print(p2);
  Serial.println(F("Pa "));
  Serial.println("");


  Serial.print("Licht= ");
  Serial.println(lichtsensor);
  Serial.println("");
  Serial.println("");





  //*************Sende an MQTT
  MQTT_connect();
  InnenHum.publish(h1);
  delay(50);
  InnenTemp.publish(t1);
  delay(50);
  AussenHum.publish(h2);
  delay(50);
  AussenTemp.publish(t2);
  delay(50);
  Luftdruck.publish(p2);
  delay(50);
  Licht.publish(lichtsensor);
  delay(50);

  if (lichtsensor < 60) {
    Lichtevent.publish("ON");
  }

  if (lichtsensor > 100) {
    Lichtevent.publish("OFF");
  }
  delay(50);


  //**************Sende ab Thingspeak
  //*********Set fields in Thingspeak
  ThingSpeak.setField(1, t2);        //Aussentemperatur
  ThingSpeak.setField(2, t1);   //Innentemperatur
  ThingSpeak.setField(3, h2);        //Ausssenfeuchte
  ThingSpeak.setField(4, h1);   //Innenfeuchte
  ThingSpeak.setField(5, p2);   //Luftdruck
  ThingSpeak.setField(6, lichtsensor);        //Licht


  // set the status
  ThingSpeak.setStatus(myStatus);

  // write to the ThingSpeak channel
  int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
  if (x == 200) {
    Serial.println("THINGSPEAK update successful.");
  }
  else {
    Serial.println("Problem updating Thingspeak. HTTP error code " + String(x));
  }


  Serial.print("Leg mich schlafen. Der Microcontroller lief für ");
  Serial.print(millis() / 1000);
  Serial.println(" Sekunden.");
  Serial.println("");
  Serial.println(" ");
  Serial.println("");
  //********Schalte die Sensor Power supply aus bevor es in den DeepSleep geht
  digitalWrite(Sens_pow1, LOW);
  digitalWrite(Sens_pow2, LOW);
  digitalWrite(Sens_pow3, LOW);

  ESP.deepSleep(6e+8);  //1,2e+7= 12sek; 1,2e+8= 120sek
  //1min= 6e+7; 5 Min=3e+8; 10 Min= 6e+8

}




void setup_wifi() {

  WiFi.begin(SSID, PSK);
  IPAddress ip(192, 168, 0, 71);
  IPAddress gateway(192, 168, 0, 1);
  IPAddress subnet(255, 255, 255, 0);
  WiFi.config(ip, gateway, subnet);

  while (WiFi.status() != WL_CONNECTED) {
    delay(100);
  }
  Serial.print("Der Arduino ist mit folgender IP Adresse im WLAN angemeldet: ");
  Serial.println(WiFi.localIP());
}

// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.


void MQTT_connect() {
  int8_t ret;

  // Stop if already connected.
  if (mqtt.connected()) {
    return;
  }

  Serial.print("Connecting to MQTT... ");

  uint8_t retries = 3;
  while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
    Serial.println(mqtt.connectErrorString(ret));
    Serial.println("Retrying MQTT connection in 5 seconds...");
    mqtt.disconnect();
    delay(1000);  // wait 5 seconds
    retries--;
    if (retries == 0) {
      // basically die and wait for WDT to reset me
      while (1);
    }
  }
  Serial.println("MQTT Connected!");
}

Are you running your own MQTT server and if so, can you see the ON/OFF appearing there?

I have an own MQTT server and the data are coming in as described. I proved that to my self thorugh MQTT.fx. The data coming in from the Arduino to MQTT.fx and Openhab are exactly the same- it does not receive the ON, but the numbers only. This is actually my issue...

Then it sounds like a configuration issue on openhab (about which I know nothing).

Why that? The issue appears on MQTT.fx which reads the MQTT stream only...

Oh, that's clearer. In that case I suggest that you write a little test program that just sends ON and OFF to MQTT for that topic to help you debug it.

Does that mean you get a value below 60 on "/aussen/licht" but no value on "/aussen/lichtevent" on your MQTT server?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.