Skip to content

When a second NodeMCU ESP8266 is powered up, NEITHER will send readings to MQTT server #4765

Closed
@fritserasmus

Description

@fritserasmus

Basic Infos

  • This issue complies with the issue POLICY doc.
  • I have read the documentation at readthedocs and the issue is not addressed there.
  • I have tested that the issue is present in current master branch (aka latest git).
  • I have searched the issue tracker for a similar issue.
  • If there is a stack dump, I have decoded it.
  • I have filled out all fields below.

Platform

Settings in IDE

  • Module: [NodeMCU 0.0 (ESP-12 Module)]
  • Flash Mode: [4M(1M SPIFFS]
  • Flash Size: [4MB]
  • lwip Variant: [v2 Prebuilt (MSS=536]
  • Reset Method: [nodemcu]
  • Flash Frequency: [80Mhz]
  • CPU Frequency: [80Mhz]
  • Upload Using: [OTA|SERIAL]
  • Upload Speed: [115200]

Problem Description

Detailed problem description goes here.
I have two Node-MCU's and will be adding two more.
UBUNTU 16.04 Server is my MQTT server also running mySQL

I have been using the same IDE for both temp sensors: DS18B20 but changing the topic to be different so I can have separated INSERT SQL Scripts.

When only one Noe-MCU is connected either via PC or directly from power it will send the reading out. It will show on the Serial monitor and MQTT SUB and update mySQL table. This is the rule for either of the two Node-MCU's connected.
The IP Address stays the same when connected to PC or direct to power supply
(I checked with (Advanced IP Scanner to make sure I have the unit by confirming MAC Addresses)
As soon as the second Node-MCU starts up, NO Temperature readings reaches the Serial port or MQTT or mySQL

With Node-MCU's connected in turn for a a minute it will update with the correct record that distinguish between the different sensors.

MCVE Sketch

// DS18B20_WITH_ESP8266_NODEMCU_Upload_OWN_MQTT_FER_V2_G1

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Streaming.h>

#define ONE_WIRE_BUS D4 // DS18B20 pin

const char* ssid = "myaccesspoint";
const char* password = "accesspointpw";
const char* mqtt_server = "192.168.1.110";
const char* mqtt_username = "mqttuser";
const char* mqtt_password = "0000";
const char* mqtt_topic = "/18561/geyser1/temp";

WiFiClient espClient;
PubSubClient client(espClient);

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);

char temperatureString[6];

void setup() {
// setup serial port
Serial.begin(115200);

// setup WiFi
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);

// setup OneWire bus
DS18B20.begin();
}

void setup_wifi() {
delay(300);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}

Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}

void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP8266Client", mqtt_username, mqtt_password)) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 2 seconds before retrying
delay(2000);
}
}
}

float getTemperature() {
Serial << "Requesting DS18B20 temperature..." << endl;
float temp;
do {
DS18B20.requestTemperatures();
temp = DS18B20.getTempCByIndex(0);
delay(500);
} while (temp == 85.0 || temp == (-127.0));
return temp;
}

void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();

float temperature = getTemperature();
// convert temperature to a string with two digits before the comma and 2 digits for precision
dtostrf(temperature, 2, 2, temperatureString);
// send temperature to the serial console
Serial << "Sending temperature: " << temperatureString << endl;
// send temperature to the MQTT topic
client.publish(mqtt_topic, temperatureString);

delay(500);

}

Debug Messages

None

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions