So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By fritserasmus
#76106 I want to take temperature readings from five separate sensors each on own NodeMCU ESP8266 and INSERT the results in an SQL database.

So one at a time works quite well but as soon as the second NodeMCU ESP8266 is powered up, NEITHER will send readings to MQTT server.

I have the LoLin version
Using IDE on Windows 7

IDE Settings:
Module: [NodeMCU 0.0 (ESP-12 Module)]
Flash Mode: [4M(1M SPIFFS]
Flash Size: [4MB]
lwip Variant: [v2 Prebuilt (MSS=536]
Reset Method: [nodemcu]
Upload Speed: [115200]

Problem Description:
I have two Node-MCU's and will be adding some more.
UBUNTU 16.04 Server is my MQTT server also running mySQL

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

When only one NodeMCU 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 for each 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)
So readings is INSERTed in the db when the NodeMCU has power.
As soon as the second Node-MCU starts up, NO Temperature readings the MQTT server or mySQL db.

When I take away power from a NodeMCU the other updates. Sp with any ONE AT A TIME NodeMCU's connected in turn for a minute it will update with the correct record that distinguish between the different sensors.



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);

}

==========================================================================

I was told:

your problem is pretty simple: you're starting your Nodemcus as both AP and STA and so they end up trying to connect to one-another. Use WiFi.mode(WIFI_STA); before WiFi.begin(ssid, password); to make them act as plain STA.


BUT:
The Arduino just prints dots in the serial monitor, so it is not connecting at all.......




Someone that can offer help
I will appreciate it very much.
User avatar
By fritserasmus
#76130 Thanks @pablo2048 for helping,

So I added the code that @WereCatf suggested.
At first the upload failed, but after rebooting the PC, it uploaded without issue:

I have four notepad docs:

1. The sketch
2. The serial screen display result when I upload the sketch
3. This same serial screen display when I power up another NodeMCU.
4. While that second NodeMCU is still sunning and I re-download the sketch to make it try to connect
You do not have the required permissions to view the files attached to this post.
User avatar
By fritserasmus
#76135 @Pablo2048
Thank you for helping: (As I said, I am not a developer myself, just truing to do home-automation self, as in this

part of the woods there is no-one able to assist - Phase two I will be connecting a relay to that board which will

be be controlled by code running on the UBUNTU box depending on readings logged in the SQL db.. Yes I will add

other type of sensors as well)

I have for files to attach to make it easier.
1. The complete sketch
2. The serial screen with one NodeMCU connected and sending readings happily to MQTT
3. The serial screen while the first one is going and I power-up a second NodeMCU and it stops sending readings to

MQTT.
4. The serial screen when I upload the Sketch to a NodeMCU WHILE a second (Another NodeMCU running this sketch) is

already running and happily sending readings to MQTT.

As soon as I have TWO NodeMCU's connected, neither sends readings.
You do not have the required permissions to view the files attached to this post.