messages from sensor doesn't seem to appear on gateway, st=ok [solved]



  • Hi,

    I'm trying to put very basic config together for the first time and it seem that I'm missing something very obvious here.

    I have made a serial GW connected to RPi2 running OpenHAB 1.7.0 on top of Ubuntu server and sensor node with 3 sensors attached: DHT temperature and humidity, BMP180 pressure and simple soil moisture. Both Adruinos are Pro Mini. I have downloaded 1.4.1 MySensors library, I use Arduino IDE 1.6.4 on OS X.

    I have put together sensor node sketch from library examples. After some tinkering it seem to me that I've got communication flowing, but messages seem empty! I've looked at it for a while, tried to google it but with no result so far.

    Please help me with advice! I will read detailed API and protocol descriptions meanwhile 🙂

    Here is sensor node sketch:

    #include <SPI.h>
    #include <MySensor.h>  
    #include <DHT.h>  
    #include <Wire.h>
    #include <Adafruit_BMP085.h>
    
    #define CHILD_ID_HUM 0
    #define CHILD_ID_DHT_TEMP 1
    #define CHILD_ID_BARO 2
    #define CHILD_ID_BMP_TEMP 3
    #define CHILD_ID_SOIL_MOIST 4
    
    #define HUMIDITY_SENSOR_DIGITAL_PIN 4
    
    #define SOIL_MOISTURE_SENSOR_ANALOG_PIN 0
    
    const float ALTITUDE = 20; // <-- adapt this value to your own location's altitude.
    
    const unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)
    
    const char *weather[] = { "stable", "sunny", "cloudy", "unstable", "thunderstorm", "unknown" };
    enum FORECAST
    {
    	STABLE = 0,			// "Stable Weather Pattern"
    	SUNNY = 1,			// "Slowly rising Good Weather", "Clear/Sunny "
    	CLOUDY = 2,			// "Slowly falling L-Pressure ", "Cloudy/Rain "
    	UNSTABLE = 3,		// "Quickly rising H-Press",     "Not Stable"
    	THUNDERSTORM = 4,	// "Quickly falling L-Press",    "Thunderstorm"
    	UNKNOWN = 5			// "Unknown (More Time needed)
    };
    
    Adafruit_BMP085 bmp = Adafruit_BMP085();      // BMP180 Digital Pressure Sensor 
    DHT dht;      // DHT Temperature and humidity sensor
    MySensor gw;
    
    float lastDHTTemp = -1;
    float lastHum = -1;
    float DHTtemperature = -1;
    float humidity = -1;
    
    boolean metric = true;
    
    float lastPressure = -1;
    float lastBMPTemp = -1;
    int lastForecast = -1;
    
    const int LAST_SAMPLES_COUNT = 5;
    float lastPressureSamples[LAST_SAMPLES_COUNT];
    
    // this CONVERSION_FACTOR is used to convert from Pa to kPa in forecast algorithm
    // get kPa/h be dividing hPa by 10 
    #define CONVERSION_FACTOR (1.0/10.0)
    
    int minuteCount = 0;
    bool firstRound = true;
    float pressureAvg[7];
    
    float dP_dt;
    
    int lastSoilMoisture = -1;
    int soilMoisture = -1;
    
    MyMessage msgHum(CHILD_ID_HUM, V_HUM);
    MyMessage msgDHTTemp(CHILD_ID_DHT_TEMP, V_TEMP);
    MyMessage msgBMPTemp(CHILD_ID_BMP_TEMP, V_TEMP);
    MyMessage msgPressure(CHILD_ID_BARO, V_PRESSURE);
    MyMessage msgForecast(CHILD_ID_BARO, V_FORECAST);
    
    MyMessage msgSoilMoisture(CHILD_ID_SOIL_MOIST, V_RAIN);
    
    
    void setup()  
    { 
      gw.begin();
    
      dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); 
    
      if (!bmp.begin()) 
      {
        Serial.println("Could not find a valid BMP085 sensor, check wiring!");
        while (1) {}
      }
    
      // Send the Sketch Version Information to the Gateway
      gw.sendSketchInfo("Outside sensor", "0.01");
    
      // Register all sensors to gw (they will be created as child devices)
      gw.present(CHILD_ID_HUM, S_HUM);
      gw.present(CHILD_ID_DHT_TEMP, S_TEMP);
      gw.present(CHILD_ID_BARO, S_BARO);
      gw.present(CHILD_ID_BMP_TEMP, S_TEMP);
      gw.present(CHILD_ID_SOIL_MOIST, S_RAIN);
    
      metric = gw.getConfig().isMetric;
    }
    
    void loop()      
    {  
    
      // ****** DHT ******
    
      delay(dht.getMinimumSamplingPeriod());
    
      DHTtemperature = dht.getTemperature();
      if (isnan(DHTtemperature)) {
          Serial.println("Failed reading temperature from DHT");
      } else if (DHTtemperature != lastDHTTemp) {
        lastDHTTemp = DHTtemperature;
        if (!metric) {
          DHTtemperature = dht.toFahrenheit(DHTtemperature);
        }
        gw.send(msgDHTTemp.set(DHTtemperature, 1));
        Serial.print("DHT Temperature: ");
        Serial.println(DHTtemperature);
      }
      
      humidity = dht.getHumidity();
      if (isnan(humidity)) {
          Serial.println("Failed reading humidity from DHT");
      } else if (humidity != lastHum) {
          lastHum = humidity;
          gw.send(msgHum.set(humidity, 1));
          Serial.print("Humidity: ");
          Serial.println(humidity);
      }
    
      // ****** BMP ******
      
      float pressure = bmp.readSealevelPressure(ALTITUDE) / 100.0;
      float BMPtemperature = bmp.readTemperature();
      
      if (!metric) 
      {
        // Convert to fahrenheit
        BMPtemperature = BMPtemperature * 9.0 / 5.0 + 32.0;
      }
      
      int forecast = sample(pressure);
      
      Serial.print("BMP Temperature = ");
      Serial.print(BMPtemperature);
      Serial.println(metric ? " *C" : " *F");
      Serial.print("Pressure = ");
      Serial.print(pressure);
      Serial.println(" hPa");
      Serial.print("Forecast = ");
      Serial.println(weather[forecast]);
      
      if (BMPtemperature != lastBMPTemp) 
      {
        gw.send(msgBMPTemp.set(BMPtemperature, 1));
        lastBMPTemp = BMPtemperature;
      }
      
      if (pressure != lastPressure) 
      {
        gw.send(msgPressure.set(pressure, 0));
        lastPressure = pressure;
      }
      
      if (forecast != lastForecast)
      {
        gw.send(msgForecast.set(weather[forecast]));
        lastForecast = forecast;
      }
    
      // ****** Soil moisture ******
      
      soilMoisture = analogRead(SOIL_MOISTURE_SENSOR_ANALOG_PIN);
      
      Serial.print("Soil moisture (0-1023) = ");
      Serial.println(soilMoisture);
      
      if (soilMoisture != lastSoilMoisture) 
      {
        gw.send(msgSoilMoisture.set(soilMoisture, 1));
        lastSoilMoisture = soilMoisture;
      }
      
      gw.sleep(SLEEP_TIME); //sleep a bit
    }
    
    float getLastPressureSamplesAverage()
    {
    	float lastPressureSamplesAverage = 0;
    	for (int i = 0; i < LAST_SAMPLES_COUNT; i++)
    	{
    		lastPressureSamplesAverage += lastPressureSamples[i];
    	}
    	lastPressureSamplesAverage /= LAST_SAMPLES_COUNT;
    
    	return lastPressureSamplesAverage;
    }
    
    // Algorithm found here
    // http://www.freescale.com/files/sensors/doc/app_note/AN3914.pdf
    // Pressure in hPa -->  forecast done by calculating kPa/h
    int sample(float pressure)
    {
    	// Calculate the average of the last n minutes.
    	int index = minuteCount % LAST_SAMPLES_COUNT;
    	lastPressureSamples[index] = pressure;
    
    	minuteCount++;
    	if (minuteCount > 185)
    	{
    		minuteCount = 6;
    	}
    
    	if (minuteCount == 5)
    	{
    		pressureAvg[0] = getLastPressureSamplesAverage();
    	}
    	else if (minuteCount == 35)
    	{
    		pressureAvg[1] = getLastPressureSamplesAverage();
    		float change = (pressureAvg[1] - pressureAvg[0]) * CONVERSION_FACTOR;
    		if (firstRound) // first time initial 3 hour
    		{
    			dP_dt = change * 2; // note this is for t = 0.5hour
    		}
    		else
    		{
    			dP_dt = change / 1.5; // divide by 1.5 as this is the difference in time from 0 value.
    		}
    	}
    	else if (minuteCount == 65)
    	{
    		pressureAvg[2] = getLastPressureSamplesAverage();
    		float change = (pressureAvg[2] - pressureAvg[0]) * CONVERSION_FACTOR;
    		if (firstRound) //first time initial 3 hour
    		{
    			dP_dt = change; //note this is for t = 1 hour
    		}
    		else
    		{
    			dP_dt = change / 2; //divide by 2 as this is the difference in time from 0 value
    		}
    	}
    	else if (minuteCount == 95)
    	{
    		pressureAvg[3] = getLastPressureSamplesAverage();
    		float change = (pressureAvg[3] - pressureAvg[0]) * CONVERSION_FACTOR;
    		if (firstRound) // first time initial 3 hour
    		{
    			dP_dt = change / 1.5; // note this is for t = 1.5 hour
    		}
    		else
    		{
    			dP_dt = change / 2.5; // divide by 2.5 as this is the difference in time from 0 value
    		}
    	}
    	else if (minuteCount == 125)
    	{
    		pressureAvg[4] = getLastPressureSamplesAverage();
    		float change = (pressureAvg[4] - pressureAvg[0]) * CONVERSION_FACTOR;
    		if (firstRound) // first time initial 3 hour
    		{
    			dP_dt = change / 2; // note this is for t = 2 hour
    		}
    		else
    		{
    			dP_dt = change / 3; // divide by 3 as this is the difference in time from 0 value
    		}
    	}
    	else if (minuteCount == 155)
    	{
    		pressureAvg[5] = getLastPressureSamplesAverage();
    		float change = (pressureAvg[5] - pressureAvg[0]) * CONVERSION_FACTOR;
    		if (firstRound) // first time initial 3 hour
    		{
    			dP_dt = change / 2.5; // note this is for t = 2.5 hour
    		}
    		else
    		{
    			dP_dt = change / 3.5; // divide by 3.5 as this is the difference in time from 0 value
    		}
    	}
    	else if (minuteCount == 185)
    	{
    		pressureAvg[6] = getLastPressureSamplesAverage();
    		float change = (pressureAvg[6] - pressureAvg[0]) * CONVERSION_FACTOR;
    		if (firstRound) // first time initial 3 hour
    		{
    			dP_dt = change / 3; // note this is for t = 3 hour
    		}
    		else
    		{
    			dP_dt = change / 4; // divide by 4 as this is the difference in time from 0 value
    		}
    		pressureAvg[0] = pressureAvg[5]; // Equating the pressure at 0 to the pressure at 2 hour after 3 hours have past.
    		firstRound = false; // flag to let you know that this is on the past 3 hour mark. Initialized to 0 outside main loop.
    	}
    
    	int forecast = UNKNOWN;
    	if (minuteCount < 35 && firstRound) //if time is less than 35 min on the first 3 hour interval.
    	{
    		forecast = UNKNOWN;
    	}
    	else if (dP_dt < (-0.25))
    	{
    		forecast = THUNDERSTORM;
    	}
    	else if (dP_dt > 0.25)
    	{
    		forecast = UNSTABLE;
    	}
    	else if ((dP_dt > (-0.25)) && (dP_dt < (-0.05)))
    	{
    		forecast = CLOUDY;
    	}
    	else if ((dP_dt > 0.05) && (dP_dt < 0.25))
    	{
    		forecast = SUNNY;
    	}
    	else if ((dP_dt >(-0.05)) && (dP_dt < 0.05))
    	{
    		forecast = STABLE;
    	}
    	else
    	{
    		forecast = UNKNOWN;
    	}
    
    	return forecast;
    }
    

    Sensor node log:

    req node id
    send: 255-255-0-0 s=255,c=3,t=3,pt=0,l=0,st=ok:
    sensor started, id 255
    req node id
    send: 255-255-0-0 s=255,c=3,t=3,pt=0,l=0,st=ok:
    req node id
    send: 255-255-0-0 s=255,c=3,t=3,pt=0,l=0,st=ok:
    req node id
    send: 255-255-0-0 s=255,c=3,t=3,pt=0,l=0,st=ok:
    req node id
    send: 255-255-0-0 s=255,c=3,t=3,pt=0,l=0,st=ok:
    req node id
    send: 255-255-0-0 s=255,c=3,t=3,pt=0,l=0,st=ok:
    req node id
    send: 255-255-0-0 s=255,c=3,t=3,pt=0,l=0,st=ok:
    req node id
    send: 255-255-0-0 s=255,c=3,t=3,pt=0,l=0,st=ok:
    req node id
    send: 255-255-0-0 s=255,c=3,t=3,pt=0,l=0,st=ok:
    DHT Temperature: 23.00
    
    req node id
    send: 255-255-0-0 s=255,c=3,t=3,pt=0,l=0,st=ok:
    Humidity: 48.70
    
    BMP Temperature = 23.20 *C
    
    Pressure = 1016.88 hPa
    
    Forecast = unknown
    
    req node id
    send: 255-255-0-0 s=255,c=3,t=3,pt=0,l=0,st=ok:
    req node id
    send: 255-255-0-0 s=255,c=3,t=3,pt=0,l=0,st=ok:
    req node id
    send: 255-255-0-0 s=255,c=3,t=3,pt=0,l=0,st=ok:
    Soil moisture (0-1023) = 1023
    
    req node id
    send: 255-255-0-0 s=255,c=3,t=3,pt=0,l=0,st=ok:
    req node id
    send: 255-255-0-0 s=255,c=3,t=3,pt=0,l=0,st=ok:
    Humidity: 48.90
    
    BMP Temperature = 23.20 *C
    
    Pressure = 1016.83 hPa
    
    Forecast = unknown
    
    req node id
    send: 255-255-0-0 s=255,c=3,t=3,pt=0,l=0,st=ok:
    Soil moisture (0-1023) = 1023
    
    req node id
    send: 255-255-0-0 s=255,c=3,t=3,pt=0,l=0,st=ok:
    DHT Temperature: 23.10
    
    BMP Temperature = 23.30 *C
    
    Pressure = 1016.83 hPa
    
    Forecast = unknown
    
    req node id
    send: 255-255-0-0 s=255,c=3,t=3,pt=0,l=0,st=ok:
    Soil moisture (0-1023) = 1023
    
    req node id
    send: 255-255-0-0 s=255,c=3,t=3,pt=0,l=0,st=ok:
    Humidity: 49.20
    
    BMP Temperature = 23.30 *C
    
    Pressure = 1016.85 hPa
    
    Forecast = unknown
    
    req node id
    send: 255-255-0-0 s=255,c=3,t=3,pt=0,l=0,st=ok:
    Soil moisture (0-1023) = 1023
    

    Extract from OpenHAB log, specifically what it gets on serial:

    2015-08-01 15:07:31.604 [INFO ] [runtime.busevents             ] - Arduino_serial state updated to 0;0;3;0;14;Gateway startup complete.
    
    2015-08-01 15:09:08.070 [INFO ] [runtime.busevents             ] - Arduino_serial state updated to 0;0;3;0;9;read: 255-255-0 s=255,c=3,t=3,pt=0,l=0:
    255;255;3;0;3;
    
    2015-08-01 15:09:10.073 [INFO ] [runtime.busevents             ] - Arduino_serial state updated to 0;0;3;0;9;read: 255-255-0 s=255,c=3,t=3,pt=0,l=0:
    255;255;3;0;3;
    
    2015-08-01 15:09:12.082 [INFO ] [runtime.busevents             ] - Arduino_serial state updated to 0;0;3;0;9;read: 255-255-0 s=255,c=3,t=3,pt=0,l=0:
    255;255;3;0;3;
    
    2015-08-01 15:09:14.088 [INFO ] [runtime.busevents             ] - Arduino_serial state updated to 0;0;3;0;9;read: 255-255-0 s=255,c=3,t=3,pt=0,l=0:
    255;255;3;0;3;
    
    2015-08-01 15:09:16.099 [INFO ] [runtime.busevents             ] - Arduino_serial state updated to 0;0;3;0;9;read: 255-255-0 s=255,c=3,t=3,pt=0,l=0:
    255;255;3;0;3;
    
    2015-08-01 15:09:18.106 [INFO ] [runtime.busevents             ] - Arduino_serial state updated to 0;0;3;0;9;read: 255-255-0 s=255,c=3,t=3,pt=0,l=0:
    255;255;3;0;3;
    
    2015-08-01 15:09:20.114 [INFO ] [runtime.busevents             ] - Arduino_serial state updated to 0;0;3;0;9;read: 255-255-0 s=255,c=3,t=3,pt=0,l=0:
    255;255;3;0;3;
    
    2015-08-01 15:09:24.126 [INFO ] [runtime.busevents             ] - Arduino_serial state updated to 0;0;3;0;9;read: 255-255-0 s=255,c=3,t=3,pt=0,l=0:
    255;255;3;0;3;
    
    2015-08-01 15:09:26.139 [INFO ] [runtime.busevents             ] - Arduino_serial state updated to 0;0;3;0;9;read: 255-255-0 s=255,c=3,t=3,pt=0,l=0:
    255;255;3;0;3;
    
    2015-08-01 15:09:28.193 [INFO ] [runtime.busevents             ] - Arduino_serial state updated to 0;0;3;0;9;read: 255-255-0 s=255,c=3,t=3,pt=0,l=0:
    255;255;3;0;3;
    
    2015-08-01 15:09:30.200 [INFO ] [runtime.busevents             ] - Arduino_serial state updated to 0;0;3;0;9;read: 255-255-0 s=255,c=3,t=3,pt=0,l=0:
    255;255;3;0;3;
    
    2015-08-01 15:09:32.207 [INFO ] [runtime.busevents             ] - Arduino_serial state updated to 0;0;3;0;9;read: 255-255-0 s=255,c=3,t=3,pt=0,l=0:
    255;255;3;0;3;
    
    2015-08-01 15:09:34.217 [INFO ] [runtime.busevents             ] - Arduino_serial state updated to 0;0;3;0;9;read: 255-255-0 s=255,c=3,t=3,pt=0,l=0:
    255;255;3;0;3;
    
    2015-08-01 15:10:07.495 [INFO ] [runtime.busevents             ] - Arduino_serial state updated to 0;0;3;0;9;read: 255-255-0 s=255,c=3,t=3,pt=0,l=0:
    255;255;3;0;3;
    
    2015-08-01 15:10:09.551 [INFO ] [runtime.busevents             ] - Arduino_serial state updated to 0;0;3;0;9;read: 255-255-0 s=255,c=3,t=3,pt=0,l=0:
    255;255;3;0;3;
    
    2015-08-01 15:10:42.826 [INFO ] [runtime.busevents             ] - Arduino_serial state updated to 0;0;3;0;9;read: 255-255-0 s=255,c=3,t=3,pt=0,l=0:
    255;255;3;0;3;
    
    2015-08-01 15:10:44.885 [INFO ] [runtime.busevents             ] - Arduino_serial state updated to 0;0;3;0;9;read: 255-255-0 s=255,c=3,t=3,pt=0,l=0:
    255;255;3;0;3;
    
    2015-08-01 15:11:18.159 [INFO ] [runtime.busevents             ] - Arduino_serial state updated to 0;0;3;0;9;read: 255-255-0 s=255,c=3,t=3,pt=0,l=0:
    255;255;3;0;3;
    
    2015-08-01 15:11:20.215 [INFO ] [runtime.busevents             ] - Arduino_serial state updated to 0;0;3;0;9;read: 255-255-0 s=255,c=3,t=3,pt=0,l=0:
    255;255;3;0;3;
    
    
    


  • ok, I got where my fundamental mistake was: I was thinking that GW is managing node ID's...

    this helped me

    ok, headed to OpenHAB now 🙂


Log in to reply
 

Suggested Topics

18
Online

11.2k
Users

11.1k
Topics

112.5k
Posts