Temperatuur waarde in een string zetten ?

Hallo

Ik wil 2 waardes uitlezen in home assistent via MQTT
een counter en tempsensor.
De counter werkt maar de (tempC) kan ik niet in de string zetten.....
MQTTclient.publish("DEMO/iWaarde", String(tempC).c_str());
maar met Serial.println(tempC); is de waarde /temperatuur prima te zien


#include <OneWire.h>
#include <DallasTemperature.h>
#include <ESP8266WiFi.h>
#include <ArduinoOTA.h>
#include <PubSubClient.h>
// #include "arduino_secrets.h"
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>

int iWiFiTry = 0;          
int iMQTTTry = 0;
String sClient_id;

int iCount;   // For DEMO
int iWaarde;
// int tempC;
const char* ssid = "";
const char* password = "";
const char* HostName = "Arduino_Mqtt_OTA";

const char* mqtt_broker = "192.168.178.65";
const char* mqtt_user = "mqtt-gebruiker";
const char* mqtt_password = "1234";
#define ONE_WIRE_BUS 12
OneWire oneWire(ONE_WIRE_BUS);
WiFiClient espClient;
PubSubClient MQTTclient(espClient); // MQTT Client
DallasTemperature sensors(&oneWire);

void setup() {
  Serial.begin(115200);
  sensors.begin();
  pinMode(LED_BUILTIN, OUTPUT);
  while (!Serial) { ; }  // wait for serial port to connect. Needed for native USB port only           
  Connect2WiFi();
  Connect2MQTT();
  iCount = 0;
  iWaarde = -129;
  
  // Start up the library
  
}

void Connect2WiFi() { 
  //Connect to WiFi
  // WiFi.mode(WIFI_STA);  //in case of an ESP32
  iWiFiTry = 0;
  WiFi.begin(ssid, password);
  WiFi.setHostname(HostName);
  Serial.print("Connecting to WiFi ");
  while (WiFi.status() != WL_CONNECTED && iWiFiTry < 11) { //Try to connect to WiFi for 11 times
    ++iWiFiTry;
    Serial.print(".");
    delay(500);
  }
  Serial.println("");
  Serial.print("Got IP: ");  Serial.println(WiFi.localIP());
  // ArduinoOTA.setPort(8266); // Port defaults to 8266
  
  ArduinoOTA.setHostname(HostName); 
 // ArduinoOTA.setPassword((const char *)OTAPassword); // Optional password

  ArduinoOTA.onStart([]() { Serial.println("Start"); });
  ArduinoOTA.onEnd([]()   { Serial.println("\nEnd"); });
  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
    Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  });
  ArduinoOTA.onError([](ota_error_t error) {
    Serial.printf("Error[%u]: ", error);
    if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
    else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
    else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
    else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
    else if (error == OTA_END_ERROR) Serial.println("End Failed");
  });
  ArduinoOTA.begin();
  Serial.println("Ready");

  //Unique MQTT Device name
  sClient_id = "esp-client-" + String(WiFi.macAddress());
  Serial.print("ESP Client name: "); Serial.println(sClient_id);
}

void Connect2MQTT() {
  // Connect to the MQTT server
  iMQTTTry=0;
  if (WiFi.status() != WL_CONNECTED) { 
    Connect2WiFi; 
  }

  Serial.print("Connecting to MQTT ");
  MQTTclient.setServer(mqtt_broker, 1883);
  while (!MQTTclient.connect(sClient_id.c_str(), mqtt_user, mqtt_password) && iMQTTTry < 11) { //Try to connect to MQTT for 11 times
    ++iMQTTTry;
    Serial.print(".");
    delay(500);
  }
  Serial.println("");
}

void loop() {
  ArduinoOTA.handle();

 if (!MQTTclient.connect(sClient_id.c_str(), mqtt_user, mqtt_password)) {
    Connect2MQTT(); }


   digitalWrite(LED_BUILTIN, LOW);  // turn the LED on 
   Serial.println("Begin Loop");    
  MQTTclient.publish("DEMO/iCount", String(iCount).c_str());
  MQTTclient.publish("DEMO/iWaarde", String(tempC).c_str());
  Serial.println(iCount);
  ++iCount;

  if(iCount>99) {
    iCount = 0;
    
    
  }
delay(500);  
digitalWrite(LED_BUILTIN, HIGH);  // turn the LED off
 delay(1500); 

  sensors.requestTemperatures(); // Send the command to get temperatures
    float tempC = sensors.getTempCByIndex(0);
        Serial.println(tempC);
 


    // If you want to reboot your device when WiFi is not working
    if (iWiFiTry > 10){
      Serial.println("REBOOTING");
      Serial.println(" Reboot in 2 seconds");
      Serial.println(""); 
      delay(2000);
      ESP.restart(); 
    }
  }
  

Here you create a new variable. You set the value. But the old variable with the same name will not be updated.
Try to delete 'float'.

Hi

the problem is that i get an error
the Compilation error: 'tempC' was not declared in this scope

if i delete -> tempC
MQTTclient.publish("DEMO/iWaarde", String(tempC).c_str());
the error is gone

Make tempC global.
Before loop() add:
float tempC;

:heart_eyes:

it seems to work -127 C

Now attach a sensor

Currently you are reading the temperature after sending the temperature via MQTT...
You will see the first real temperature in the second loop... in the first you should see 0.0.

yes I see, not a problem in itself.

this is a clipping of various working sketch parts. Now add a fan controller sketch and then it is complete.

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