Node to Node Communication
-
declare the variable you're sending as int or convert it by casting
gw.send (msgHum.set(static_cast<int>(humidity)));http://www.learncpp.com/cpp-tutorial/44-type-conversion-and-casting/
@hek said:
declare the variable you're sending as int or convert it by casting
gw.send (msgHum.set(static_cast<int>(humidity)));http://www.learncpp.com/cpp-tutorial/44-type-conversion-and-casting/
@hek Converting my sensor to send the data as an Integer worked and it is now parsing correctly, however I also have a humidity child sensor on that node I would like to send to node 11 as well, how would I capture that in the case statement?
-
@hek said:
declare the variable you're sending as int or convert it by casting
gw.send (msgHum.set(static_cast<int>(humidity)));http://www.learncpp.com/cpp-tutorial/44-type-conversion-and-casting/
@hek Converting my sensor to send the data as an Integer worked and it is now parsing correctly, however I also have a humidity child sensor on that node I would like to send to node 11 as well, how would I capture that in the case statement?
@cleight I posted code to do just that
#define BEDROOM_NODE_ID 42 #define KITCHEN_NODE_ID 44 #define TEMP 0 #define HUM 1 float bdtemp = 0, bdhum =0, kntemp = 0, knhum =0; void incomingMessage(const MyMessage &message) { switch (message.sender) { case BEDROOM_NODE_ID: switch (message.sensor){ case TEMP bdtemp = message.getFloat(); break; case HUM bdhum = message.getFloat(); break; } break; case KITCHEN_NODE_ID: switch (message.sensor){ case TEMP kntemp = message.getFloat(); break; case HUM knhum = message.getFloat(); break; } break; } updateDisplay(); } -
@cleight I posted code to do just that
#define BEDROOM_NODE_ID 42 #define KITCHEN_NODE_ID 44 #define TEMP 0 #define HUM 1 float bdtemp = 0, bdhum =0, kntemp = 0, knhum =0; void incomingMessage(const MyMessage &message) { switch (message.sender) { case BEDROOM_NODE_ID: switch (message.sensor){ case TEMP bdtemp = message.getFloat(); break; case HUM bdhum = message.getFloat(); break; } break; case KITCHEN_NODE_ID: switch (message.sensor){ case TEMP kntemp = message.getFloat(); break; case HUM knhum = message.getFloat(); break; } break; } updateDisplay(); } -
@Chaotic I missed your code earlier, thank you for this, although while compiling is it saying I am missing a colon before knhum for some reason, need to dig deeper.
-
@cleight that is what I get for coding on the fly
change
case TEMPand
case HUMwith
case TEMP:and
case HUM: -
Ok last issue to be worked out and I think I am good to go, thanks again to @Chaotic and @hek for there assistance with my project.
Last issue is with the sensor node, it sends the data to the LCD Node (node 11) but only sends the data to the gateway on boot, it will not send it to the gateway after it starts sending to Node 11. I would like it to send to both the Node and the Gateway all the time, that way Vera see the information as well as my LCD.
Here is the output of the Serial Monitor, also for some reason on each temp update it sends to node 11 twice for some reason:
sensor started, id 1
send: 1-1-0-0 s=255,c=0,t=17,pt=0,l=5,st=ok:1.4.1
send: 1-1-0-0 s=255,c=3,t=6,pt=1,l=1,st=ok:0
read: 0-0-1 s=255,c=3,t=6,pt=0,l=1:I
send: 1-1-0-0 s=255,c=3,t=11,pt=0,l=15,st=ok:Hot Tub Monitor
send: 1-1-0-0 s=255,c=3,t=12,pt=0,l=3,st=ok:2.0
send: 1-1-0-0 s=0,c=0,t=6,pt=0,l=5,st=ok:1.4.1
send: 1-1-0-0 s=1,c=0,t=7,pt=0,l=5,st=ok:1.4.1
send: 1-1-0-0 s=2,c=0,t=6,pt=0,l=5,st=ok:1.4.1
send: 1-1-0-0 s=0,c=1,t=0,pt=7,l=5,st=ok:67.2
send: 1-1-0-11 s=0,c=1,t=0,pt=2,l=2,st=ok:67
T2: 67.20
send: 1-1-0-0 s=2,c=1,t=0,pt=7,l=5,st=ok:66.2
T: 66.20
send: 1-1-0-0 s=1,c=1,t=1,pt=7,l=5,st=ok:36.0
send: 1-1-0-11 s=1,c=1,t=1,pt=2,l=2,st=ok:36
H: 36.00
send: 1-1-0-11 s=0,c=1,t=0,pt=7,l=5,st=ok:75.7
send: 1-1-0-11 s=0,c=1,t=0,pt=2,l=2,st=ok:75
T2: 75.70
send: 1-1-0-11 s=0,c=1,t=0,pt=7,l=5,st=ok:80.1
send: 1-1-0-11 s=0,c=1,t=0,pt=2,l=2,st=ok:80
T2: 80.10
send: 1-1-0-11 s=0,c=1,t=0,pt=7,l=5,st=ok:81.6
send: 1-1-0-11 s=0,c=1,t=0,pt=2,l=2,st=ok:81
T2: 81.60
send: 1-1-0-11 s=0,c=1,t=0,pt=7,l=5,st=ok:81.7
send: 1-1-0-11 s=0,c=1,t=0,pt=2,l=2,st=ok:81
T2: 81.70
send: 1-1-0-11 s=0,c=1,t=0,pt=7,l=5,st=ok:79.8
send: 1-1-0-11 s=0,c=1,t=0,pt=2,l=2,st=ok:79
T2: 79.80
send: 1-1-0-11 s=0,c=1,t=0,pt=7,l=5,st=ok:78.1
send: 1-1-0-11 s=0,c=1,t=0,pt=2,l=2,st=ok:78
T2: 78.10
send: 1-1-0-11 s=0,c=1,t=0,pt=7,l=5,st=ok:76.7
send: 1-1-0-11 s=0,c=1,t=0,pt=2,l=2,st=ok:76
T2: 76.70
send: 1-1-0-11 s=0,c=1,t=0,pt=7,l=5,st=ok:75.5
send: 1-1-0-11 s=0,c=1,t=0,pt=2,l=2,st=ok:75
T2: 75.50Here is the sketch:
#include <SPI.h> #include <MySensor.h> #include <DHT.h> #include <DallasTemperature.h> #include <OneWire.h> #define CHILD_ID_HUM 1 #define CHILD_ID_TEMP 2 #define CHILD_ID_HTTEMP 3 #define HUMIDITY_SENSOR_DIGITAL_PIN 3 //Waterproof Sensor addition #define ONE_WIRE_BUS 4 //Pin where waterproof temp sensor is connected #define MAX_ATTACHED_DS18B20 16 unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds) OneWire oneWire(ONE_WIRE_BUS); DallasTemperature sensors(&oneWire); MySensor gw; DHT dht; float lastTemperature[MAX_ATTACHED_DS18B20]; int numSensors=0; boolean receivedConfig = false; float lastTemp; float lastHum; boolean metric = false; MyMessage msgHum(CHILD_ID_HUM, V_HUM); MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP); MyMessage msgHTTemp(CHILD_ID_HTTEMP, V_TEMP); void setup() { // Startup OneWire sensors.begin(); gw.begin(); dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); // Send the Sketch Version Information to the Gateway gw.sendSketchInfo("Hot Tub Monitor", "2.0"); // 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++) { gw.present(i, S_TEMP); } // Register all sensors to gw (they will be created as child devices) gw.present(CHILD_ID_HUM, S_HUM); gw.present(CHILD_ID_TEMP, S_TEMP); metric = gw.getConfig().isMetric; } void loop() { // Process incoming messages (like config from server) gw.process(); //Fetch temperatures from Dallas sensors sensors.requestTemperatures(); // Red temperatures and send them to the controller for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) { // Fetch and round temperature to one decimal float temperature = static_cast<float>(static_cast<int>((sensors.getTempFByIndex(i)) * 10.)) / 10.; //Only send data if temperature has changed and no error if (lastTemperature[i] != temperature && temperature != -127.00) { // Send in the new temperature delay(100); gw.send(msgHTTemp.setSensor(i).set(temperature,1)); gw.send(msgHTTemp.setDestination(11).set(static_cast<int>(temperature))); Serial.print("T2: "); Serial.println(temperature); lastTemperature[i]=temperature; } } delay(dht.getMinimumSamplingPeriod()); //float temperature = dht.getTemperature(); float temperature = dht.getTemperature()*9/5 + 32; if (isnan(temperature)) { Serial.println("Failed reading temperature from DHT"); } else if (temperature != lastTemp) { lastTemp = temperature; if (!metric) { //temperature = dht.toFahrenheit(temperature); temperature = dht.getTemperature()*9/5 + 32; } delay(100); gw.send(msgTemp.set(temperature, 1)); Serial.print("T: "); Serial.println(temperature); } float humidity = dht.getHumidity(); if (isnan(humidity)) { Serial.println("Failed reading humidity from DHT"); } else if (humidity != lastHum) { lastHum = humidity; delay(100); gw.send(msgHum.set(humidity, 1)); gw.send(msgHum.setDestination(11).set(static_cast<int>(humidity))); Serial.print("H: "); Serial.println(humidity); } gw.sleep(SLEEP_TIME); //sleep a bit } -
Ok last issue to be worked out and I think I am good to go, thanks again to @Chaotic and @hek for there assistance with my project.
Last issue is with the sensor node, it sends the data to the LCD Node (node 11) but only sends the data to the gateway on boot, it will not send it to the gateway after it starts sending to Node 11. I would like it to send to both the Node and the Gateway all the time, that way Vera see the information as well as my LCD.
Here is the output of the Serial Monitor, also for some reason on each temp update it sends to node 11 twice for some reason:
sensor started, id 1
send: 1-1-0-0 s=255,c=0,t=17,pt=0,l=5,st=ok:1.4.1
send: 1-1-0-0 s=255,c=3,t=6,pt=1,l=1,st=ok:0
read: 0-0-1 s=255,c=3,t=6,pt=0,l=1:I
send: 1-1-0-0 s=255,c=3,t=11,pt=0,l=15,st=ok:Hot Tub Monitor
send: 1-1-0-0 s=255,c=3,t=12,pt=0,l=3,st=ok:2.0
send: 1-1-0-0 s=0,c=0,t=6,pt=0,l=5,st=ok:1.4.1
send: 1-1-0-0 s=1,c=0,t=7,pt=0,l=5,st=ok:1.4.1
send: 1-1-0-0 s=2,c=0,t=6,pt=0,l=5,st=ok:1.4.1
send: 1-1-0-0 s=0,c=1,t=0,pt=7,l=5,st=ok:67.2
send: 1-1-0-11 s=0,c=1,t=0,pt=2,l=2,st=ok:67
T2: 67.20
send: 1-1-0-0 s=2,c=1,t=0,pt=7,l=5,st=ok:66.2
T: 66.20
send: 1-1-0-0 s=1,c=1,t=1,pt=7,l=5,st=ok:36.0
send: 1-1-0-11 s=1,c=1,t=1,pt=2,l=2,st=ok:36
H: 36.00
send: 1-1-0-11 s=0,c=1,t=0,pt=7,l=5,st=ok:75.7
send: 1-1-0-11 s=0,c=1,t=0,pt=2,l=2,st=ok:75
T2: 75.70
send: 1-1-0-11 s=0,c=1,t=0,pt=7,l=5,st=ok:80.1
send: 1-1-0-11 s=0,c=1,t=0,pt=2,l=2,st=ok:80
T2: 80.10
send: 1-1-0-11 s=0,c=1,t=0,pt=7,l=5,st=ok:81.6
send: 1-1-0-11 s=0,c=1,t=0,pt=2,l=2,st=ok:81
T2: 81.60
send: 1-1-0-11 s=0,c=1,t=0,pt=7,l=5,st=ok:81.7
send: 1-1-0-11 s=0,c=1,t=0,pt=2,l=2,st=ok:81
T2: 81.70
send: 1-1-0-11 s=0,c=1,t=0,pt=7,l=5,st=ok:79.8
send: 1-1-0-11 s=0,c=1,t=0,pt=2,l=2,st=ok:79
T2: 79.80
send: 1-1-0-11 s=0,c=1,t=0,pt=7,l=5,st=ok:78.1
send: 1-1-0-11 s=0,c=1,t=0,pt=2,l=2,st=ok:78
T2: 78.10
send: 1-1-0-11 s=0,c=1,t=0,pt=7,l=5,st=ok:76.7
send: 1-1-0-11 s=0,c=1,t=0,pt=2,l=2,st=ok:76
T2: 76.70
send: 1-1-0-11 s=0,c=1,t=0,pt=7,l=5,st=ok:75.5
send: 1-1-0-11 s=0,c=1,t=0,pt=2,l=2,st=ok:75
T2: 75.50Here is the sketch:
#include <SPI.h> #include <MySensor.h> #include <DHT.h> #include <DallasTemperature.h> #include <OneWire.h> #define CHILD_ID_HUM 1 #define CHILD_ID_TEMP 2 #define CHILD_ID_HTTEMP 3 #define HUMIDITY_SENSOR_DIGITAL_PIN 3 //Waterproof Sensor addition #define ONE_WIRE_BUS 4 //Pin where waterproof temp sensor is connected #define MAX_ATTACHED_DS18B20 16 unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds) OneWire oneWire(ONE_WIRE_BUS); DallasTemperature sensors(&oneWire); MySensor gw; DHT dht; float lastTemperature[MAX_ATTACHED_DS18B20]; int numSensors=0; boolean receivedConfig = false; float lastTemp; float lastHum; boolean metric = false; MyMessage msgHum(CHILD_ID_HUM, V_HUM); MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP); MyMessage msgHTTemp(CHILD_ID_HTTEMP, V_TEMP); void setup() { // Startup OneWire sensors.begin(); gw.begin(); dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); // Send the Sketch Version Information to the Gateway gw.sendSketchInfo("Hot Tub Monitor", "2.0"); // 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++) { gw.present(i, S_TEMP); } // Register all sensors to gw (they will be created as child devices) gw.present(CHILD_ID_HUM, S_HUM); gw.present(CHILD_ID_TEMP, S_TEMP); metric = gw.getConfig().isMetric; } void loop() { // Process incoming messages (like config from server) gw.process(); //Fetch temperatures from Dallas sensors sensors.requestTemperatures(); // Red temperatures and send them to the controller for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) { // Fetch and round temperature to one decimal float temperature = static_cast<float>(static_cast<int>((sensors.getTempFByIndex(i)) * 10.)) / 10.; //Only send data if temperature has changed and no error if (lastTemperature[i] != temperature && temperature != -127.00) { // Send in the new temperature delay(100); gw.send(msgHTTemp.setSensor(i).set(temperature,1)); gw.send(msgHTTemp.setDestination(11).set(static_cast<int>(temperature))); Serial.print("T2: "); Serial.println(temperature); lastTemperature[i]=temperature; } } delay(dht.getMinimumSamplingPeriod()); //float temperature = dht.getTemperature(); float temperature = dht.getTemperature()*9/5 + 32; if (isnan(temperature)) { Serial.println("Failed reading temperature from DHT"); } else if (temperature != lastTemp) { lastTemp = temperature; if (!metric) { //temperature = dht.toFahrenheit(temperature); temperature = dht.getTemperature()*9/5 + 32; } delay(100); gw.send(msgTemp.set(temperature, 1)); Serial.print("T: "); Serial.println(temperature); } float humidity = dht.getHumidity(); if (isnan(humidity)) { Serial.println("Failed reading humidity from DHT"); } else if (humidity != lastHum) { lastHum = humidity; delay(100); gw.send(msgHum.set(humidity, 1)); gw.send(msgHum.setDestination(11).set(static_cast<int>(humidity))); Serial.print("H: "); Serial.println(humidity); } gw.sleep(SLEEP_TIME); //sleep a bit }@cleight I think the easiest solution (all theory btw) would be to duplicate the message for each sensor
MyMessage msgHum(CHILD_ID_HUM, V_HUM); MyMessage msgHumLCD(CHILD_ID_HUM, V_HUM); MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP); MyMessage msgTempLCD(CHILD_ID_TEMP, V_TEMP); MyMessage msgHTTemp(CHILD_ID_HTTEMP, V_TEMP); MyMessage msgHTTempLCD(CHILD_ID_HTTEMP, V_TEMP);then in the setup()
msgHumLCD.setDestination(11); msgTempLCD.setDestination(11); msgHTTPempLCD.setDestination(11);Then in the loop when you are doing the gw.send just do a second one for the LCD message (you shouldn't need the setDestination)
-
@cleight I think the easiest solution (all theory btw) would be to duplicate the message for each sensor
MyMessage msgHum(CHILD_ID_HUM, V_HUM); MyMessage msgHumLCD(CHILD_ID_HUM, V_HUM); MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP); MyMessage msgTempLCD(CHILD_ID_TEMP, V_TEMP); MyMessage msgHTTemp(CHILD_ID_HTTEMP, V_TEMP); MyMessage msgHTTempLCD(CHILD_ID_HTTEMP, V_TEMP);then in the setup()
msgHumLCD.setDestination(11); msgTempLCD.setDestination(11); msgHTTPempLCD.setDestination(11);Then in the loop when you are doing the gw.send just do a second one for the LCD message (you shouldn't need the setDestination)