Categories
Connected Farm

MySensors.org LoRa IOT for Farming, Part 3 Temperature and Humidity Sensors

I’ve setup a number different types of temperature and temperature/humidity sensors. The types that I’ve used are Ds18B20 temperature sensors and DHT11 temperature/humidity sensors.

Ds18B20

The Ds18B20 temperature sensor, comes in 2 packages, a TO-92 and a waterproof probe. The TO-92 is useful for making room temperature sensors but the probe is better suited to most of our agri applications such as milk tank monitors and other liquid temperatures. It is connected as per the instructions on https://www.mysensors.org/build/temp, multiple devices can be connected to the one data pin as they are digital devices with unique 64 bit serial numbers and use the 1-wire bus. More detailed information on the sensor is available here: https://components101.com/sensors/ds18b20-temperature-sensor.

#define MY_DEBUG

#define MY_RADIO_RFM95
//#define MY_DEBUG_VERBOSE_RFM95
#define MY_RFM95_FREQUENCY (RFM95_868MHZ)
#define MY_TRANSPORT_STATE_TIMEOUT_MS  (3*1000ul)
#define RFM95_RETRY_TIMEOUT_MS  (3000ul) 
#define MY_RFM95_MAX_POWER_LEVEL_DBM (1) //1mW = 0dBm 10mW = 10dBm 25mW = 14dBm 100mW = 20dBm
//#define MY_RFM95_MODEM_CONFIGRUATION RFM95_BW125CR45SF1288
#define MY_RFM95_MODEM_CONFIGRUATION  RFM95_BW125CR48SF4096



#define MY_NODE_ID 45

//#define MY_RADIO_RF24
//#define MY_RF24_CHANNEL  91


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

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

#define ONE_WIRE_BUS 3 // Pin where dallase sensor is connected 
#define MAX_ATTACHED_DS18B20 16
unsigned long SLEEP_TIME = 56000; // 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 = true;
// 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 = static_cast<float>(static_cast<int>((getControllerConfig().isMetric?sensors.getTempCByIndex(i):sensors.getTempFByIndex(i)) * 10.)) / 10.;

    // Only send data if temperature has changed and no error
    #if COMPARE_TEMP == 1
    if (lastTemperature[i] != temperature && temperature != -127.00 && temperature != 85.00) {
    #else
    if (temperature != -127.00 && temperature != 85.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);
}Code language: PHP (php)

DHT11/22

I’ve been experimenting with the DHT11 temperature and humidity sensor in my polytunnel. The DHT11 is the wrong choice for this though and I should be using a DHT22, the main difference between the 2 is their range but it should be simple to replace one with the other at a later date. There is more info on the difference between the 2 here https://learn.adafruit.com/dht?view=all .

In the graph DHT11 can’t handle negative temperatures and shows them as positive
#define MY_DEBUG

#define MY_RADIO_RFM95
//#define MY_DEBUG_VERBOSE_RFM95
#define MY_RFM95_FREQUENCY (RFM95_868MHZ)
#define MY_TRANSPORT_STATE_TIMEOUT_MS  (3*1000ul)
#define RFM95_RETRY_TIMEOUT_MS  (3000ul) 
#define MY_RFM95_MAX_POWER_LEVEL_DBM (1) //1mW = 0dBm 10mW = 10dBm 25mW = 14dBm 100mW = 20dBm
//#define MY_RFM95_MODEM_CONFIGRUATION RFM95_BW125CR45SF1288
#define MY_RFM95_MODEM_CONFIGRUATION  RFM95_BW125CR48SF4096

//Enable Signing <Make Sure you Change Password>
//#define MY_SIGNING_SIMPLE_PASSWD "mysecretpassword"

//Enable Encryption This uses less memory, and hides the actual data. <Make Sure you Change Password>
//#define MY_ENCRYPTION_SIMPLE_PASSWD "mysecretpassword"


#define MY_NODE_ID 45

//#define MY_RADIO_RF24
//#define MY_RF24_CHANNEL  91


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

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

#define ONE_WIRE_BUS 3 // Pin where dallase sensor is connected 
#define MAX_ATTACHED_DS18B20 16
unsigned long SLEEP_TIME = 56000; // 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 = true;
// 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 = static_cast<float>(static_cast<int>((getControllerConfig().isMetric?sensors.getTempCByIndex(i):sensors.getTempFByIndex(i)) * 10.)) / 10.;

    // Only send data if temperature has changed and no error
    #if COMPARE_TEMP == 1
    if (lastTemperature[i] != temperature && temperature != -127.00 && temperature != 85.00) {
    #else
    if (temperature != -127.00 && temperature != 85.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);
}Code language: PHP (php)

Facebook Comments