DS1820B Temperature + MQTT

Hi all,

I am using a Wemos D1 mini with temp sensor, an LED screen and also a CloudMQTT connection.

I use the Dallas lib to run the temp sensor DS1820B. I have used an original example from the lib, the sensor works perfectly but after flashing the Wemos at around 10 times, the sensor gives me errors, ie. -127 values.

Repeated this same process a couple of times, getting the original lib and amending the sketch and works and later on fails again after those 10 flashing times.

Also, I am trying to connect to the CloudMQTT and I got stuck on connection. Error rc=5, read it is an Authentication failure.

How I set up the MQTT on the Wemos? I created an instance on my Cloud account, copy details as user, passw and port and enter them on the Wemos.

After that I created a device(user/client) with the same name as the Wemos, ie. "espClient" but on the cloud it requires a password...
Under the code on the Wemos, can't see any client authentication so not sure if this could cause the failure on the connection as shown below on the Cloud log, it detects the connection but fails...

LOG from the cloud shows the below;

: Socket error on client , disconnecting.
: New connection from 146.90.xxx.xxx on port 19686

Thanks, any ideas how to set up this properly?

MQTT_XProjectX.ino (7.09 KB)

#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS D3 // Data wire is plugged into port 2 on the Arduino
OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature.
//DeviceAddress insideThermometer; // arrays to hold device address

//TEMPERATURE//
#include "SevenSegmentTM1637.h"
#include "TM1637Display.h"
#define CLK D6
#define DIO D5
TM1637Display display(CLK, DIO);

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

// Update these with values suitable for your network.

const char* ssid = "My WIFI"; // Connect to WIFI
const char* password = "WIFI password"; // Connect to WIFI

const char* mqtt_server = "m23.cloudmqtt.com"; // Connect to MQTT m23.cloudmqtt.com
const char* mqtt_Port = "19686"; // 19686
const char* mqtt_user = "user"; // Connect to MQTT
const char* mqtt_password = "password"; // Connect to MQTT

WiFiClient espClient; // Create an ESP8266 WifiClient class to connect to the MQTT server
PubSubClient client(espClient); //

const char* outTopic = "sensor/temperature";
const char* inTopic = "sensor/temperature";

//Connect to WIFI........................................................................................

void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
} // We start by connecting to a WiFi network

randomSeed(micros());

Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP()); //Print the IP Address
} //Connect to WIFI
//Connect to WIFI........................................................................................

void callback(char* topic, byte* payload, unsigned int length) { //Subscribe to the Topic, Payload
Serial.print("Message arrived [");
Serial.print(topic); //Print the Topic
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload*);*

  • }*

  • // Switch on the LED if an 1 was received as first character*

  • if ((char)payload[0] == '1') {*

  • digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level*

  • // but actually the LED is on; this is because*

  • // it is active low on the ESP-01)*

  • } else {*

  • digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH*

  • }*
    }
    /*/
    /*/
    void reconnect() { // Loop until we're reconnected DO NOT TOUCH.........

  • while (!client.connected()) {*

  • Serial.print("Attempting MQTT connection...");*

  • // String clientId ="espClient"; // "ESP8266Client-"; // Create a random client ID*

  • //clientId += String(random(0xffff), HEX);*

  • if (client.connect("espClient")){ //(clientId.c_str())) { // Attempt to connect*

  • Serial.println("connected");*
    *//// RECONNECTED..... *

  • client.publish("outTopic", "hello world"); // Once connected, publish an announcement...*

  • client.subscribe("inTopic"); // ... and resubscribe*

  • } else {*

  • Serial.print("failed, rc=");*

  • Serial.print(client.state());*

  • Serial.println(" try again in 5 seconds");*

  • // Wait 5 seconds before retrying*

  • delay(5000);*

  • }*

  • }//*
    }
    //
    void setup() {

  • pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output*

  • Serial.begin(115200); // Connect to WIFI*
    WiFi.mode(WIFI_STA);

  • setup_wifi(); // Connect to WIFI*

  • client.setServer(mqtt_server, 19686); //Connect to MQTT*
    client.setCallback(callback);
    display.setBrightness(0x0f); // set brightness to the LED screen

  • digitalWrite(D3,HIGH); // Activate internal Pull Up resistor*

  • pinMode(D3,INPUT_PULLUP); // Activate internal Pull Up resistor*
    // Serial.begin(115200); // start serial port

  • //sensors.setResolution(insideThermometer, 9); // set the resolution to 9 bit (Each Dallas/Maxim device is capable of several different resolutions)*

  • digitalWrite(D3,HIGH); // Activate internal Pull Up resistor*

  • pinMode(D3,INPUT_PULLUP); // Activate internal Pull Up resistor*

  • sensors.begin(); // Start temp sensors*
    }
    //
    void loop()
    {

  • sensors.requestTemperatures(); // Send the command to get temperatures // call sensors.requestTemperatures() to issue a global temperature*

  • Serial.print("Temperature for the device 1 is: ");*

  • Serial.print(sensors.getTempCByIndex(0)); // printTemperature(insideThermometer); // Use a simple function to print out the data*

  • Serial.println();*

  • delay(2000);*
    // Display temperature
    display.clear();
    display.showNumberDec (int (sensors.getTempCByIndex(0)), false); // LED Screen
    delay(2000);
    /////////

  • if (!client.connected()) {*

  • reconnect();*

  • }*

  • client.loop();*
    long lastMsg = 0;
    float temp = 0.0;
    //float hum = 0.0;
    float diff = 1.0;

  • long now = millis();*

  • if (now - lastMsg > 2000) {*

  • lastMsg = now;*

  • float newTemp = sensors.getTempCByIndex(0);*

  • // float newHum = dht.readHumidity();*

  • if (checkBound(newTemp, temp, diff)) {*

  • temp = newTemp;*

  • Serial.print("New temperature:");*

  • Serial.println(String(temp).c_str());*

  • client.publish(outTopic, String(temp).c_str(), true);*

  • }*
    }//
    }
    bool checkBound(float newValue, float prevValue, float maxDiff) {

  • return !isnan(newValue) &&*

  • (newValue < prevValue - maxDiff || newValue > prevValue + maxDiff);*
    }

Serg069:
I use the Dallas lib to run the temp sensor DS1820B. I have used an original example from the lib, the sensor works perfectly but after flashing the Wemos at around 10 times, the sensor gives me errors, ie. -127 values.

Its not at all clear what you are doing here.

Are you saying the first 10 times you load a program into the Wemos, it works OK, but if you carry on uploading the same program it then stops working ?

Yes that is right :frowning:

If I want the sensor to work again, I need to access to the example and start changing the code accordingly to my project...

at around 10 times, the sensor gives me errors, ie. -127 values.

-127 indicates "no connection". In your case, that suggests sloppy wiring.

Yes...!!! was an issue with one wire... Made it work again without flashing...

But still not able to connect to the CloudMQTT through PubSubclient

This might help