G2A Many GEOs
SHARE
TWEET

Untitled

a guest Jan 27th, 2020 109 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}; // I/O pins 4, 5, 6, 7, 8, A0, A1 for the relays
  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 16
  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[16][8] = {
  32.   {0x28, 0xFF, 0x67, 0x60, 0x60, 0x17, 0x04, 0xDE}, //101
  33.   {0x28, 0xFF, 0xCB, 0xCA, 0x60, 0x17, 0x04, 0x7E}, //102
  34.   {0x28, 0xFF, 0xFF, 0xA1, 0x60, 0x17, 0x04, 0xE1}, //103
  35.   {0x28, 0xFF, 0x03, 0xBA, 0x60, 0x17, 0x04, 0xCA}, //104
  36.   {0x28, 0xFF, 0x14, 0xA2, 0x60, 0x17, 0x04, 0xC3}, //105
  37.   {0x28, 0xFF, 0x3A, 0x46, 0x60, 0x17, 0x04, 0xDE}, //106
  38.   {0x28, 0xFF, 0xCF, 0xD9, 0x60, 0x17, 0x04, 0xD1}, //107
  39.   {0x28, 0xFF, 0xC0, 0xBE, 0x60, 0x17, 0x04, 0xA8}, //108
  40.   {0x28, 0xFF, 0x80, 0xDC, 0x60, 0x17, 0x04, 0xD6}, //109
  41.   {0x28, 0xFF, 0x77, 0xBA, 0x60, 0x17, 0x04, 0xB8}, //110
  42.   {0x28, 0xFF, 0x17, 0x5A, 0x60, 0x17, 0x04, 0xE0}, //111
  43.   {0x28, 0xFF, 0x06, 0xD0, 0x60, 0x17, 0x04, 0x93}, //112
  44.   {0x28, 0xFF, 0x0E, 0xAF, 0x60, 0x17, 0x04, 0x9F}, //113
  45.   {0x28, 0xFF, 0xFA, 0x49, 0x60, 0x17, 0x04, 0x66}, //114
  46.   {0x28, 0xFF, 0xFA, 0xC0, 0x33, 0x18, 0x01, 0x94}, //115
  47.   {0x28, 0xFF, 0x41, 0x89, 0x60, 0x17, 0x04, 0x60} //116
  48.  
  49. };
  50.  
  51.  
  52. float lastTemperature[MAX_ATTACHED_DS18B20];
  53. int numSensors=0;
  54. bool receivedConfig = false;
  55. bool metric = true;
  56. // Initialize temperature message
  57. MyMessage msg(0,V_TEMP);
  58.  
  59. void before()
  60. {
  61.     for (int sensor=1; sensor<=NUMBER_OF_RELAYS; sensor++) {
  62.         // Then set relay pins in output mode
  63.         pinMode(PINS[sensor-1], OUTPUT);
  64.         // Set relay to last known state (using eeprom storage)
  65.         digitalWrite(PINS[sensor-1], loadState(sensor)?RELAY_ON:RELAY_OFF);    
  66.     }
  67.    
  68.     // Startup up the OneWire library
  69.     sensors.begin();
  70. }
  71.  
  72. void setup()  
  73. {
  74.   // requestTemperatures() will not block current thread
  75.   sensors.setWaitForConversion(false);
  76. }
  77.  
  78. void presentation()
  79. {
  80.     // Send the sketch version information to the gateway and Controller
  81.     sendSketchInfo("Relay and Temp", "1.0");
  82.  
  83.     for (int sensor=1; sensor<=NUMBER_OF_RELAYS+MAX_ATTACHED_DS18B20; sensor++) {
  84.         // Register all sensors to gw (they will be created as child devices)
  85.  
  86.         if (sensor<=NUMBER_OF_RELAYS){  
  87.           present(sensor, S_BINARY);      
  88.         }
  89.         else {
  90.           present(sensor, S_TEMP);          
  91.         }
  92.     }
  93. }
  94.  
  95. void loop()    
  96. {    
  97.   unsigned long currentMillis = millis();
  98.  
  99.   if (currentMillis - previousMillis >= SLEEP_TIME) {
  100.     // save the last time you blinked the LED
  101.     previousMillis = currentMillis;
  102.  
  103.     // Fetch temperatures from Dallas sensors
  104.     sensors.requestTemperatures();
  105.  
  106.     // query conversion time and sleep until conversion completed
  107.     int16_t conversionTime = sensors.millisToWaitForConversion(sensors.getResolution());
  108.     // sleep() call can be replaced by wait() call if node need to process incoming messages (or if node is repeater)
  109.     sleep(conversionTime);
  110.  
  111.     // Read temperatures and send them to controller
  112.     for (int i=0; i<MAX_ATTACHED_DS18B20; i++) {
  113.  
  114.       // Fetch and round temperature to one decimal
  115.       // float temperature = static_cast<float>(static_cast<int>((getConfig().isMetric?sensors.getTempCByIndex(i):sensors.getTempFByIndex(i)) * 10.)) / 10.;
  116.       float temperature = sensors.getTempC(D[i]);
  117.      
  118.       // Only send data if temperature has changed and no error
  119.       if (COMPARE_TEMP == 1) {
  120.         if (lastTemperature[i] != temperature && temperature != -127.00 && temperature != 85.00) {
  121.             // Send in the new temperature
  122.             send(msg.setSensor(i+NUMBER_OF_RELAYS+1).set(temperature,1));
  123.             // Save new temperatures for next compare
  124.             lastTemperature[i]=temperature;
  125.         }
  126.       }
  127.       else {
  128.         if (temperature != -127.00 && temperature != 85.00) {
  129.             // Send in the new temperature
  130.             send(msg.setSensor(i+NUMBER_OF_RELAYS+1).set(temperature,1));
  131.             // Save new temperatures for next compare
  132.             lastTemperature[i]=temperature;
  133.         }
  134.       }
  135.     }
  136.   }
  137. }
  138.  
  139. void receive(const MyMessage &message)
  140. {
  141.     // We only expect one type of message from controller. But we better check anyway.
  142.     if (message.type==V_STATUS) {
  143.         // Change relay state
  144.         digitalWrite(PINS[message.sensor-1], message.getBool()?RELAY_ON:RELAY_OFF);
  145.         // Store state in eeprom
  146.         saveState(message.sensor, message.getBool());
  147.         // Write some debug info
  148.         Serial.print("Incoming change for sensor:");
  149.         Serial.print(message.sensor);
  150.         Serial.print(", New status: ");
  151.         Serial.println(message.getBool());
  152.     }
  153. }
RAW Paste Data
Ledger Nano X - The secure hardware wallet
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