SHARE
TWEET

Dallas DS18b20 + relay Mysensors

a guest Jan 28th, 2020 102 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Enable debug prints to serial monitor
  2. #define MY_DEBUG
  3.  
  4. #define MY_GATEWAY_SERIAL
  5. // Enable repeater functionality for this node
  6. #define MY_REPEATER_FEATURE
  7.  
  8. #include <SPI.h>
  9. #include <MySensors.h>  
  10. #include <DallasTemperature.h>
  11. #include <OneWire.h>
  12.  
  13. const int PINS[] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
  14. #define NUMBER_OF_RELAYS 10 // Total number of attached relays
  15. #define RELAY_ON 1  // GPIO value to write to turn on attached relay
  16. #define RELAY_OFF 0 // GPIO value to write to turn off attached relay
  17.  
  18. #define COMPARE_TEMP 1 // Send temperature only if changed? 1 = Yes 0 = No
  19.  
  20. #define ONE_WIRE_BUS 2 // Pin where dallase sensor is connected
  21. #define MAX_ATTACHED_DS18B20 5
  22. unsigned long SLEEP_TIME = 10000; // Sleep time between reads (in milliseconds)
  23. // Generally, you should use "unsigned long" for variables that hold time
  24. // The value will quickly become too large for an int to store
  25. unsigned long previousMillis = 0;        // will store last time LED was updated
  26.  
  27. OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
  28. DallasTemperature sensors(&oneWire); // Pass the oneWire reference to Dallas Temperature.
  29.  
  30.  
  31. byte D[5][8] = {
  32. { 0x28, 0xF2, 0xAB, 0x0F, 0x07, 0x00, 0x00, 0xA1 },
  33. { 0x28, 0x23, 0x29, 0xDB, 0x05, 0x00, 0x00, 0x5A },
  34. { 0x28, 0xAB, 0x2A, 0xDB, 0x05, 0x00, 0x00, 0x5F },
  35. { 0x28, 0x49, 0xAF, 0x0F, 0x07, 0x00, 0x00, 0x41 },
  36. { 0x28, 0x1D, 0x2F, 0x10, 0x07, 0x00, 0x00, 0xDA }
  37. };
  38.  
  39. float lastTemperature[MAX_ATTACHED_DS18B20];
  40. int numSensors=0;
  41. bool receivedConfig = false;
  42. bool metric = true;
  43. // Initialize temperature message
  44. MyMessage msg(0,V_TEMP);
  45.  
  46. void before()
  47. {
  48.     for (int sensor=1; sensor<=NUMBER_OF_RELAYS; sensor++) {
  49.         // Then set relay pins in output mode
  50.         pinMode(PINS[sensor-1], OUTPUT);
  51.         // Set relay to last known state (using eeprom storage)
  52.         digitalWrite(PINS[sensor-1], loadState(sensor)?RELAY_ON:RELAY_OFF);    
  53.     }
  54.    
  55.     // Startup up the OneWire library
  56.     sensors.begin();
  57. }
  58.  
  59. void setup()  
  60. {
  61.   // requestTemperatures() will not block current thread
  62.   sensors.setWaitForConversion(false);
  63. }
  64.  
  65. void presentation()
  66. {
  67.     // Send the sketch version information to the gateway and Controller
  68.     sendSketchInfo("Relay and Temp", "1.0");
  69.  
  70.     for (int sensor=1; sensor<=NUMBER_OF_RELAYS+MAX_ATTACHED_DS18B20; sensor++) {
  71.         // Register all sensors to gw (they will be created as child devices)
  72.  
  73.         if (sensor<=NUMBER_OF_RELAYS){  
  74.           present(sensor, S_BINARY);      
  75.         }
  76.         else {
  77.           present(sensor, S_TEMP);          
  78.         }
  79.     }
  80. }
  81.  
  82. void loop()    
  83. {    
  84.   unsigned long currentMillis = millis();
  85.  
  86.   if (currentMillis - previousMillis >= SLEEP_TIME) {
  87.     // save the last time you blinked the LED
  88.     previousMillis = currentMillis;
  89.  
  90.     // Fetch temperatures from Dallas sensors
  91.     sensors.requestTemperatures();
  92.  
  93.     // query conversion time and sleep until conversion completed
  94.     int16_t conversionTime = sensors.millisToWaitForConversion(sensors.getResolution());
  95.     // sleep() call can be replaced by wait() call if node need to process incoming messages (or if node is repeater)
  96.     //sleep(conversionTime);
  97.     wait(conversionTime);
  98.  
  99.     // Read temperatures and send them to controller
  100.     for (int i=0; i<MAX_ATTACHED_DS18B20; i++) {
  101.  
  102.       // Fetch and round temperature to one decimal
  103.       // float temperature = static_cast<float>(static_cast<int>((getConfig().isMetric?sensors.getTempCByIndex(i):sensors.getTempFByIndex(i)) * 10.)) / 10.;
  104.       float temperature = sensors.getTempC(D[i]);
  105.      
  106.       // Only send data if temperature has changed and no error
  107.       if (COMPARE_TEMP == 1) {
  108.         if (lastTemperature[i] != temperature && temperature != -127.00 && temperature != 85.00) {
  109.             // Send in the new temperature
  110.             send(msg.setSensor(i+NUMBER_OF_RELAYS+1).set(temperature,1));
  111.             // Save new temperatures for next compare
  112.             lastTemperature[i]=temperature;
  113.         }
  114.       }
  115.       else {
  116.         if (temperature != -127.00 && temperature != 85.00) {
  117.             // Send in the new temperature
  118.             send(msg.setSensor(i+NUMBER_OF_RELAYS+1).set(temperature,1));
  119.             // Save new temperatures for next compare
  120.             lastTemperature[i]=temperature;
  121.         }
  122.       }
  123.     }
  124.   }
  125. }
  126.  
  127. void receive(const MyMessage &message)
  128. {
  129.     // We only expect one type of message from controller. But we better check anyway.
  130.     if (message.type==V_STATUS) {
  131.         // Change relay state
  132.         digitalWrite(PINS[message.sensor-1], message.getBool()?RELAY_ON:RELAY_OFF);
  133.         // Store state in eeprom
  134.         saveState(message.sensor, message.getBool());
  135.         // Write some debug info
  136.         Serial.print("Incoming change for sensor:");
  137.         Serial.print(message.sensor);
  138.         Serial.print(", New status: ");
  139.         Serial.println(message.getBool());
  140.     }
  141. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top