Getting started with OH2: How to use info from Temperature sensor

Hello everyone! Hope y’all are having a good day.

Been thinging about adding this to a project.
I would like to read my temparature sensor value thanks to mqtt but i still don’t know how to use the info i have .
i have a raspberry pi 3 with openhabian, mosquitto ,a mqtt client and an esp8266 , a temperature sensor(dht11) , I use arduino IE. reading the temperature value is easy but taking that info to openhab is what i still don’t know how to do.
can you help me a little bit ?

Thanks to everyone one of you for always taking the time to help beginners. Class A forum! @bartus

Use the pubsubclient library (It comes with examples)
Publish your value on an MQTT topic can subscribe to that topic in an item in OH

Have a go yourself instead of copying and pasting code, you’ll learn more that way. If it doesn’t work try again. If it doesn’t work again post your code here and we’ll point you in the right direction

2 Likes

Hi , I know how to plublish a topic : but this far i have only beeen publishing text not actual value that can change over time

tried this //
// hum = dht.readHumidity();// Valeur de l’humidite
temp= dht.readTemperature(); //Valeur de la temperature

client.publish(tempTopic,temp);
  delay(50);
}

but it doesn’t work said i can’t directly publish the value

so i tried this
//
// hum = dht.readHumidity();// Valeur de l’humidite
temp= dht.readTemperature(); //Valeur de la temperature

client.publish(tempTopic,String(temp));
  delay(50);
}

All i am getting is NAN because is doesn’t see value

the whole code is down here . The door sensor is ok i receive 'closed ’ and ‘open’ messages but i just can’t publish the value of the temperature

#include <ESP8266WiFi.h>
#include <MQTTClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <ESP8266HTTPUpdateServer.h>
#include <DHT.h>;
#include <Adafruit_Sensor.h>

/* ---------- DO NOT EDIT ANYTHING ABOVE THIS LINE ---------- */

//Only edit the settings in this section

/* WIFI Settings /
// Name of wifi network
const char
ssid = “Lamtoro-Guests”;

// Password to wifi network
const char* password = “1234567890”;

/* Web Updater Settings /
// Host Name of Device
const char
host = “MK-DoorSensor1”;

// Path to access firmware update page (Not Neccessary to change)
const char* update_path = “/firmware”;

// Username to access the web update page
const char* update_username = “admin”;

// Password to access the web update page
const char* update_password = “admin”;

/* MQTT Settings /
// Topic which listens for commands
char
outTopic = “/FrontDoor/DoorSensor”;
char* tempTopic = “/FamilyRoom/Temperature”;

//MQTT Server IP Address
const char* server = “192.168.1.43”;

//Unique device ID
const char* mqttDeviceID = “MK-SmartHouseDevice1”;

/* ---------- DO NOT EDIT ANYTHING BELOW THIS LINE ---------- */

//the time when the sensor outputs a low impulse
long unsigned int lowIn;

//the amount of milliseconds the sensor has to be low
//before we assume all detection has stopped
long unsigned int pause = 100;

//sensor variables
boolean lockLow = true;
boolean takeLowTime;

//the digital pin connected to the door sensor’s output
int sensorPin = 2;

// Temperature sensor constants
#define DHTPIN 16 // Numero de pin du capteur de temperature
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE); //// Initialisee le capteur DHT ( 16mhz Arduino )

//Variables
int chk;
float hum; //Stock la valeur de l’humidité
float temp; //Stock la valeur de la temperature
unsigned long previousMillis = 0;
unsigned long interval = 5000;
//webserver
ESP8266WebServer httpServer(80);
ESP8266HTTPUpdateServer httpUpdater;

//MQTT
WiFiClient net;
MQTTClient client;

//Time Variable
unsigned long lastMillis = 0;

//Connect to WiFI and MQTT
void connect();

void messageReceived(String &topic, String &payload)
{
//This sensor does not recieve anything from MQTT Server so this is blank
}
//Setup pins, wifi, webserver and MQTT
void setup()
{
pinMode(sensorPin, INPUT);
digitalWrite(sensorPin, LOW);
pinMode(DHTPIN, INPUT);
digitalWrite(DHTPIN, LOW);
dht.begin();

WiFi.mode(WIFI_STA);

WiFi.begin(ssid, password);
client.begin(server, net);
client.onMessage(messageReceived);

connect();

MDNS.begin(host);

httpUpdater.setup(&httpServer, update_path, update_username, update_password);
httpServer.begin();

MDNS.addService(“http”, “tcp”, 80);
}

//Connect to wifi and MQTT
void connect()
{
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
}

while (!client.connect(mqttDeviceID))
{
delay(1000);
}
}

void loop()
{
// MQTT Loop
client.loop();
delay(10);

// Make sure device is connected
if(!client.connected())
{
connect();
}

httpServer.handleClient();

//Sensor Detection

if(digitalRead(sensorPin) == HIGH)
{
if(lockLow)
{
//makes sure we wait for a transition to LOW before any further output is made:
lockLow = false;
client.publish(outTopic, “OPEN”);
delay(50);
}
takeLowTime = true;
}

if(digitalRead(sensorPin) == LOW)
{
if(takeLowTime)
{
lowIn = millis(); //save the time of the transition from high to LOW
takeLowTime = false; //make sure this is only done at the start of a LOW phase
}
//if the sensor is low for more than the given pause,
//we assume that no more detection is going to happen
if(!lockLow && millis() - lowIn > pause)
{
//makes sure this block of code is only executed again after
//a new detection sequence has been detected
lockLow = true;
client.publish(outTopic, “CLOSED”);
delay(50);
}
}
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {

  previousMillis = currentMillis;
  

//

// hum = dht.readHumidity();// Valeur de l’humidite
temp= dht.readTemperature(); //Valeur de la temperature

client.publish(tempTopic,String(temp));
  delay(50);
}

}

client.publish(tempTopic,String(temp).c_str());

i still get ‘nan’ when i suscribe to the topic