MySensors with 2 seperate nodemcu reporting temperature - Problem

I have successfully integrated a DS18B20 temp sensor ( which is very precise) to a Nodemcu board and using MQTT to report to HA. It works great!

Now I would like to setup a seperate sensor on a seperate Nodemcu. This is the problem. I cannot seem to understand how to make it work! It seems the HA thinks they are one in the same, or reports that it cannot assign the node as it already exists. I have tried to understand defining the nodes and childs, but I just cannot understand how to do it.

Maybe it is not possible since I am limited to one MQTT?

Any help would be appreciated.
Here is the sketch that is working fine
/**
* The MySensors Arduino library handles the wireless radio link and protocol
* between your home built sensors/actuators and HA controller of choice.
* The sensors forms a self healing radio network with optional repeaters. Each
* repeater and gateway builds a routing tables in EEPROM which keeps track of the
* network topology allowing messages to be routed to nodes.
*
* Created by Henrik Ekblad [email protected]
* Copyright © 2013-2015 Sensnology AB
* Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
*
* Documentation: http://www.mysensors.org
* Support Forum: http://forum.mysensors.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
*******************************
*
* REVISION HISTORY
* Version 1.0 - Henrik Ekblad
*
* DESCRIPTION
* The ESP8266 MQTT gateway sends radio network (or locally attached sensors) data to your MQTT broker.
* The node also listens to MY_MQTT_TOPIC_PREFIX and sends out those messages to the radio network
*
* LED purposes:
* - To use the feature, uncomment any of the MY_DEFAULT_xx_LED_PINs in your sketch
* - RX (green) - blink fast on radio message recieved. In inclusion mode will blink fast only on presentation recieved
* - TX (yellow) - blink fast on radio message transmitted. In inclusion mode will blink slowly
* - ERR (red) - fast blink on error during transmission error or recieve crc error
*
* See http://www.mysensors.org/build/esp8266_gateway for wiring instructions.
* nRF24L01+ ESP8266
* VCC VCC
* CE GPIO4
* CSN/CS GPIO15
* SCK GPIO14
* MISO GPIO12
* MOSI GPIO13
*
* Not all ESP8266 modules have all pins available on their external interface.
* This code has been tested on an ESP-12 module.
* The ESP8266 requires a certain pin configuration to download code, and another one to run code:
* - Connect REST (reset) via 10K pullup resistor to VCC, and via switch to GND (‘reset switch’)
* - Connect GPIO15 via 10K pulldown resistor to GND
* - Connect CH_PD via 10K resistor to VCC
* - Connect GPIO2 via 10K resistor to VCC
* - Connect GPIO0 via 10K resistor to VCC, and via switch to GND (‘bootload switch’)
*
* Inclusion mode button:
* - Connect GPIO5 via switch to GND (‘inclusion switch’)
*
* Hardware SHA204 signing is currently not supported!
*
* Make sure to fill in your ssid and WiFi password below for ssid & pass.
*/

// Enable debug prints to serial monitor
//#define MY_DEBUG

// Use a bit lower baudrate for serial prints on ESP8266 than default in MyConfig.h
#define MY_BAUD_RATE 9600

// Enables and select radio type (if attached)
//#define MY_RADIO_NRF24
//#define MY_RADIO_RFM69

#define MY_GATEWAY_MQTT_CLIENT
#define MY_GATEWAY_ESP8266

// Set this node's subscribe and publish topic prefix
#define MY_MQTT_PUBLISH_TOPIC_PREFIX "mygateway1-out"
#define MY_MQTT_SUBSCRIBE_TOPIC_PREFIX "mygateway1-in"

// Set MQTT client id
#define MY_MQTT_CLIENT_ID "mysensors-2"

// Enable these if your MQTT broker requires usenrame/password
#define MY_MQTT_USER "XXXX"
#define MY_MQTT_PASSWORD "XXXXX"

// Set WIFI SSID and password
#define MY_ESP8266_SSID "xxx"
#define MY_ESP8266_PASSWORD "xxx"

// Set the hostname for the WiFi Client. This is the hostname
// it will pass to the DHCP server if not static.
#define MY_ESP8266_HOSTNAME "mqtt-sensor-gateway"

// Enable MY_IP_ADDRESS here if you want a static ip address (no DHCP)
//#define MY_IP_ADDRESS 192,168,178,87

// If using static ip you need to define Gateway and Subnet address as well
//#define MY_IP_GATEWAY_ADDRESS 192,168,178,1
//#define MY_IP_SUBNET_ADDRESS 255,255,255,0


// MQTT broker ip address.
#define MY_CONTROLLER_IP_ADDRESS 192, 168, 0, 35

// The MQTT broker port to to open
#define MY_PORT 1883

/*
// Enable inclusion mode
#define MY_INCLUSION_MODE_FEATURE
// Enable Inclusion mode button on gateway
#define MY_INCLUSION_BUTTON_FEATURE
// Set inclusion mode duration (in seconds)
#define MY_INCLUSION_MODE_DURATION 60
// Digital pin used for inclusion mode button
#define MY_INCLUSION_MODE_BUTTON_PIN  3
// Set blinking period
#define MY_DEFAULT_LED_BLINK_PERIOD 300
// Flash leds on rx/tx/err
#define MY_DEFAULT_ERR_LED_PIN 16  // Error led pin
#define MY_DEFAULT_RX_LED_PIN  16  // Receive led pin
#define MY_DEFAULT_TX_LED_PIN  16  // the PCB, on board LED
*/

#include <ESP8266WiFi.h>
//#include <MySensors.h>


/**
 * The MySensors Arduino library handles the wireless radio link and protocol
 * between your home built sensors/actuators and HA controller of choice.
 * The sensors forms a self healing radio network with optional repeaters. Each
 * repeater and gateway builds a routing tables in EEPROM which keeps track of the
 * network topology allowing messages to be routed to nodes.
 *
 * Created by Henrik Ekblad <[email protected]>
 * Copyright (C) 2013-2015 Sensnology AB
 * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
 *
 * Documentation: http://www.mysensors.org
 * Support Forum: http://forum.mysensors.org
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 *******************************
 *
 * DESCRIPTION
 *
 * Example sketch showing how to send in DS1820B OneWire temperature readings back to the controller
 * http://www.mysensors.org/build/temp
 */


// Enable debug prints to serial monitor
#define MY_DEBUG 
#define MY_NODE_ID 0

// Enable and select radio type attached
//#define MY_RADIO_NRF24
//#define MY_RADIO_RFM69

#include <SPI.h>
#include <MySensors.h>  
#include <DallasTemperature.h>
#include <OneWire.h>

#define COMPARE_TEMP 1 // Send temperature only if changed? 1 = Yes 0 = No

#define ONE_WIRE_BUS D4 // Pin where dallase sensor is connected 
#define MAX_ATTACHED_DS18B20 16
unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)
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 the oneWire reference to Dallas Temperature. 
float lastTemperature[MAX_ATTACHED_DS18B20];
int numSensors=0;
bool receivedConfig = false;
bool metric = false;
// Initialize temperature message
MyMessage msg(0,V_TEMP);

void before()
{
  // Startup up the OneWire library
  sensors.begin();
}

void setup() 
 
{ 
  // requestTemperatures() will not block current thread
  sensors.setWaitForConversion(false);
}

void presentation() {
  // Send the sketch version information to the gateway and Controller
  sendSketchInfo("Temperature Sensor", "1.1");

  // Fetch the number of attached temperature sensors  
  numSensors = sensors.getDeviceCount();

  // Present all sensors to controller
  for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {   
     present(i, S_TEMP);
  }
}

void loop()     
{     
  // Fetch temperatures from Dallas sensors
  sensors.requestTemperatures();

  // query conversion time and sleep until conversion completed
  int16_t conversionTime = sensors.millisToWaitForConversion(sensors.getResolution());
  // sleep() call can be replaced by wait() call if node need to process incoming messages (or if node is repeater)
  sleep(conversionTime);

  // Read temperatures and send them to controller 
  for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {
 
    // Fetch and round temperature to one decimal
    float temperature = sensors.getTempFByIndex(0);
 
    // Only send data if temperature has changed and no error
    #if COMPARE_TEMP == 1
    if (lastTemperature[i] != temperature && temperature != -127.00 && temperature != 200.00) {
    #else
    if (temperature != -127.00 && temperature != 200.00) {
    #endif
 
      // Send in the new temperature
      send(msg.setSensor(i).set(temperature,1));
      // Save new temperatures for next compare
      lastTemperature[i]=temperature;
    }
  }
  sleep(SLEEP_TIME);
}

Here is the part from the configuration.yaml file

mysensors:
gateways:
- device: mqtt
topic_in_prefix: ‘mygateway1-out’
topic_out_prefix: ‘mygateway1-in’
optimistic: false
persistence: true
retain: false
version: 2.0

well you have to change the topic for the second nodeMCU… if both report their data in the same form in the same topic, obviously HA won’t be able to distinguish…

For instance the code in nodemcu 1 will have
#define MY_MQTT_PUBLISH_TOPIC_PREFIX “mygateway1-out”
#define MY_MQTT_SUBSCRIBE_TOPIC_PREFIX “mygateway1-in”

and you will retreive data in HA using

gateways:
- device: mqtt
  topic_in_prefix: 'mygateway1-out'
  topic_out_prefix: 'mygateway1-in'
  optimistic: false
  persistence: true
  retain: false
  version: 2.0

and for your second nodemcu you will have
#define MY_MQTT_PUBLISH_TOPIC_PREFIX “mygateway2-out”
#define MY_MQTT_SUBSCRIBE_TOPIC_PREFIX “mygateway2-in”

and you will retreive data in HA using

gateways2:
- device: mqtt
  topic_in_prefix: 'mygateway2-out'
  topic_out_prefix: 'mygateway2-in'
  optimistic: false
  persistence: true
  retain: false
  version: 2.0

evidently mysensors doesn’t like the idea of gateways and gateways2…
I tried various combinations and it chokes…
I understand what you are saying about the mqtt topics, but I can’t find a way to get it to accept it.

I do not use mysensor myself so maybe you should try only one declaration of gateway but like this

gateways:
- device: mqtt
  topic_in_prefix: 'mygateway1-out'
  topic_out_prefix: 'mygateway1-in'
  optimistic: false
  persistence: true
  retain: false
  version: 2.0
- device: mqtt
  topic_in_prefix: 'mygateway2-out'
  topic_out_prefix: 'mygateway2-in'
  optimistic: false
  persistence: true
  retain: false
  version: 2.0

Anyway, one thing I am sure is that you must set different topics for your different nodemcu to allow HASS to distinguish them

Yes you are correct, but it doesn’t like 2 mqtt devices under gateways, so it must be something to do with the node id and child id…

thank you for trying to help me…

Hi @harfordhawk
Based on what you say here:

There is not a solution for your situation in my answer but I think you’ll understand a bit better the diference between gateway, node, child, and so on.

Let me tell you my experience with HA and MySensors. (Disclaimer: I’m still learning too.)
I have a MySensors gateway connected through USB to a Raspberry Pi running HA. This is what I have in the configuration.yaml file:

mysensors:
  gateways:
    - device: '/dev/mysgw'
      persistence_file: '/mnt/mydisk/mysensors.json'
      baud_rate: 115200
  optimistic: false
  persistence: true
  version: 2.0

Then I have several MySensors nodes around the house (a door switch, a temperature sensor). The nodes talk to the gateway through radio signals (using NRF24L01+ in my case). A node may have one or more sensors (child Id). For example, my node 3 (what I call a temperature sensor) has 2 children (child Id 0: temperature and child Id 1: humidity).

Ok. I think what you have created is a MySensors MQTT gateway. As you know, this gateway talks to HA using the MQTT protocol. Moreover, you have also added a temperature sensor to the gateway, which is fine and it works as you reported. The problem I see in your situation is that, in fact, you have created 2 gateways and I doubt this can be managed by the MySensors component (although in the example they include several gateways at the same time, I cannot see two MQTT gateways together). I may be wrong (remember my disclaimer).

So, what to do?

Scenario A
If you are going to use ESP8266 devices as sensors, I suggest to connect them directly to HA and declare them as MQTT sensors. For example, I have two Sonoff devices, which are defined as:

- platform: mqtt
  name: "Sonoff 01 Power"
  state_topic: "home/sonoff/01/power"
  qos: 1
  unit_of_measurement: "W"
  value_template: "{{ value_json.Power }}"
- platform: mqtt
  name: "Sonoff 02 Power"
  state_topic: "home/sonoff/02/power"
  qos: 1
  unit_of_measurement: "W"
  value_template: "{{ value_json.Power }}"

Each one with its own MQTT topic, as you can see. But this has nothing to do with the MySensors component.

Scenario B
If you are going to build a MySensors network, I think you only need one MQTT gateway and now you have to build the other sensors to talk to the gateway with, for instance, NRF24L01+ devices.

Each scenario has pros and cons. In fact, as I advanced before, I have both situations at home. Sorry if you already knew all this and I misunderstood your problem.

1 Like

Thank you for the explanation! it helps… so I am now trying to do the mqtt sensor as you suggested…

In your code…

float temperature = sensors.getTempFByIndex(0);

… it is already in Fahrenheit. In Celsius it would be:

float temperature = sensors.getTempCByIndex(0);

thanks for that yeah I got confused as usual

Two mqtt gateways are supposed to be supported. I have only done minimal testing of that though.

mysensors:
  gateways:
    - device: mqtt
      persistence_file: 'path/mysensors1.json'
      topic_in_prefix: 'mygateway1-out'
      topic_out_prefix: 'mygateway1-in'
    - device: mqtt
      persistence_file: 'path/mysensors2.json'
      topic_in_prefix: 'mygateway2-out'
      topic_out_prefix: 'mygateway2-in'
  optimistic: false
  persistence: true
  retain: true
  version: 2.0
2 Likes

Thanks for the heads-up. It’s good to know. Also big thanks for the development of the MySensors component

1 Like