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.