2 WeMos D1 minis wont work at the same time

Everything about esp8266 and more.
Post Reply
stephenmhall
Posts: 9
Joined: Saturday 10 June 2017 22:23
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

2 WeMos D1 minis wont work at the same time

Post by stephenmhall » Saturday 15 July 2017 17:17

I am running some sonoff plugs over MQTT, for one of them I have a D1 mini set up to control it like a Dash button, It sits in deep sleep until you hit the reset button when it wakes up connects and sends the toggle command to the sonoff plug. This all works fine.

However I just added another D1 mini powered device that shows the weather conditions from the weather underground plugin, when it gets updated the details are pushed to MQTT, the D1 is subscribed to Domoticz/out and picks up the update parses out the IDX to check its the right device and updates the temp, humidity, pressure and dew point on it's LCD screen. This also works OK until I activate the sonoff toggle device then the weather display just stops working.

If I am monitoring the com port I can see all MQTT messages coming from Domoticz until the sonoff toggle D1 is triggered then just nothing.

I put the weather display on a static IP in case they were both getting the same address for some reason but no difference.

Also when I first had both running they somehow interacted and sent the sonoff plug into a MQTT toggle frenzy, but I stopped that with some code changes.

Whatever is happening seems to occur before the MQTT updtae for the switch is sent out from Domoticz as the weather device does not show the update before stopping working.

Oh I can still see the weather device on the network and ping it when its stopped.

Here is the weather device code

Code: Select all

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

// For LCD
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display

const char* ssid     = "ssid";
const char* password = "password";

const char* mqtt_server = "192.168.0.39";
const String domoticzID = "60"; // idx of weather underground

IPAddress arduino_ip ( 192,  168,   0,  150);
IPAddress gateway_ip ( 192,  168,   0,   1);
IPAddress subnet_mask(255, 255, 255,   0);

String data = "";   // String with json data

WiFiClient espClient;
PubSubClient client(espClient);

void setup() {
  Serial.begin(115200);
  delay(10);

  lcd.init();                      // initialize the lcd
  //lcd.init();
  // Print a message to the LCD.
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("Warming UP!");
  delay(1000);
  lcd.clear();

  setup_wifi();

  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);

  //Serial.println("Subscribing");
  //client.subscribe("domoticz/out");
  yield();
  if (!client.connected()) {
    reconnect();
  }

}

void setup_wifi() {

  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  lcd.setCursor(0, 0);
  lcd.print("Connecting to ");
  lcd.setCursor(0, 1);
  lcd.print(ssid);
  
  WiFi.config(arduino_ip, gateway_ip, subnet_mask);
  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());

  // Print a message to the LCD.
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("IP Address");
  lcd.setCursor(0, 1);
  lcd.print(WiFi.localIP());
  delay(4000);
  lcd.clear();
}

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

  DynamicJsonBuffer jsonBuffer;
  JsonObject& root = jsonBuffer.parseObject(payload);

  if (!root.success())
  {
    Serial.println("parseObject() failed");
    return;
  }

  int idx = root["idx"];

  if (String(idx) == domoticzID) {
    float temperature = root["svalue1"];
    int humidity = root["svalue2"];
    int pressure = root["svalue4"];
    float dewpoint = (temperature - ((100 - humidity) / 5));
    int cursorloc = 9;
    int dewLoc = 6;

    if (pressure < 1000) {
      int cursorloc = 10;
    }

    if (dewpoint  < 10.00) {
      int dewLoc = 7;
    }



    Serial.println(idx);
    Serial.println(temperature);
    Serial.println(humidity);
    // but actually the LED is on; this is because
    // it is acive low on the ESP-01)

    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print(temperature);
    lcd.print((char)223);
    lcd.print("C");

    lcd.setCursor(cursorloc, 0);
    lcd.print(pressure);
    lcd.print("hPa");

    lcd.setCursor(0, 1);
    lcd.print(humidity);
    lcd.print("%");

    lcd.setCursor(dewLoc, 1);
    lcd.print("Dew");
    lcd.print(dewpoint);
    lcd.print((char)223);
    lcd.print("C");
  }
  else{
    Serial.println("IDX Not relevant");
  }
  delay(500);
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Connect to MQTT");
    // Attempt to connect
    if (client.connect("ESP8266Client")) {
      Serial.println("connected");
      // Once connected, publish an announcement...

      String payload = "{\"command\": \"getdeviceinfo\", \"idx\": " + domoticzID + "}";
      client.publish("domoticz/in", payload.c_str());
      // ... and resubscribe
      client.subscribe("domoticz/out");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
  delay(10);
}

void loop() {
  delay(10);
  client.loop();
}
and the sonoff toggle code

Code: Select all

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

// Update these with values suitable for your network.

const char* ssid = "ssid";
const char* password = "password";
const char* mqtt_server = "192.168.0.39";

const String domoticzID = "69";

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
int tryCount = 0;

//For battery voltage
unsigned int raw = 0;
float volt = 0.0;

void setup() {
  Serial.begin(115200);
  Serial.setTimeout(2000);

  // Wait for serial to initialize.
  while (!Serial) { }

  Serial.println("Device Started");
  Serial.println("-------------------------------------");
  Serial.println("Running Deep Sleep Firmware!");
  Serial.println("-------------------------------------");

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

  reconnect();
}

void setup_wifi() {

  delay(10);
  // 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();

  StaticJsonBuffer<200> jsonBuffer;
  JsonObject& root = jsonBuffer.parseObject(payload);

  if (!root.success())
  {
    Serial.println("parseObject() failed");
    return;
  }

  int idx = root["idx"];
  String switchType = root["switchType"];

  Serial.println(idx);
  Serial.println(switchType);


  if (String(idx) == domoticzID && switchType == "On/Off") {
    Serial.println("Entering deep sleep mode");
    ESP.deepSleep(0, WAKE_RF_DEFAULT); // infinite deepsleep

  } else {
    Serial.println("No repy received");
    delay(500);
    if(tryCount <= 2) {
      reconnect();
    }
    else{
      ESP.deepSleep(0, WAKE_RF_DEFAULT); // infinite deepsleep
    }
    
  }
}

void reconnect() {
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("ESP8266Client")) {
      Serial.println("connected");
      // Once connected, publish an announcement...

      // Get battery Voltage
      raw = analogRead(A0);
      volt = raw / 1023.0;
      volt = volt * 4.5;
      Serial.print(volt);
      Serial.println("V");
      unsigned char level = map((volt * 1000), 3000, 4200, 0, 100);
      Serial.print(level);
      Serial.println("%");

      String payload = "{\"command\": \"switchlight\", \"idx\": " + domoticzID + ", \"switchcmd\": \"Toggle\", \"Battery\": " + level + "}";

      client.publish("domoticz/in", (char*) payload.c_str());
      // ... and resubscribe
      Serial.println("Message sent");
      Serial.println(payload);
      Serial.println("Subscribing");
      client.subscribe("domoticz/out");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
  tryCount += 1;
}

void loop() {

  client.loop();
}
Help me Obi-Wan Kenobies

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest