// Enable debug prints to serial monitor #define MY_DEBUG // Enable and select radio type attached #define MY_RADIO_RF24 //#define MY_RADIO_RFM69 // Enable repeater functionality for this node #define MY_REPEATER_FEATURE #include #include #include #include const int PINS[] = {4, 5, 6, 7, 8}; // I/O pins 4, 5, 6, 7, 8, A0, A1 for the relays #define NUMBER_OF_RELAYS 5 // Total number of attached relays #define RELAY_ON 0 // GPIO value to write to turn on attached relay #define RELAY_OFF 1 // GPIO value to write to turn off attached relay #define COMPARE_TEMP 0 // Send temperature only if changed? 1 = Yes 0 = No #define ONE_WIRE_BUS 2 // Pin where dallase sensor is connected #define MAX_ATTACHED_DS18B20 4 unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds) // Generally, you should use "unsigned long" for variables that hold time // The value will quickly become too large for an int to store unsigned long previousMillis = 0; // will store last time LED was updated 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. byte D[5][8] = { { 0x28, 0x4, 0x8A, 0x3B, 0x32, 0x19, 0x1, 0xF9 }, { 0x28, 0xFC, 0x94, 0x38, 0x32, 0x19, 0x1, 0x7C }, { 0x28, 0x7E, 0xEF, 0x39, 0x32, 0x19, 0x1, 0x6A }, { 0x28, 0x9F, 0x74, 0x37, 0x32, 0x19, 0x1, 0xA6 } }; float lastTemperature[MAX_ATTACHED_DS18B20]; int numSensors=0; bool receivedConfig = false; bool metric = true; // Initialize temperature message MyMessage msg(0,V_TEMP); void before() { for (int sensor=1; sensor<=NUMBER_OF_RELAYS; sensor++) { // Then set relay pins in output mode pinMode(PINS[sensor-1], OUTPUT); // Set relay to last known state (using eeprom storage) digitalWrite(PINS[sensor-1], loadState(sensor)?RELAY_ON:RELAY_OFF); } // Startup up the OneWire library sensors.begin(); } void setup() { // requestTemperatures() will not block current thread sensors.setWaitForConversion(false); while(!Serial); Serial.begin(9600); } void presentation() { // Send the sketch version information to the gateway and Controller sendSketchInfo("Relay and Temp", "1.0"); for (int sensor=1; sensor<=NUMBER_OF_RELAYS+MAX_ATTACHED_DS18B20; sensor++) { // Register all sensors to gw (they will be created as child devices) if (sensor<=NUMBER_OF_RELAYS){ present(sensor, S_BINARY); } else { present(sensor, S_TEMP); } } } void loop() { unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= SLEEP_TIME) { // save the last time you blinked the LED previousMillis = currentMillis; // 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(static_cast((getConfig().isMetric?sensors.getTempCByIndex(i):sensors.getTempFByIndex(i)) * 10.)) / 10.; float temperature = sensors.getTempC(D[i]); // Only send data if temperature has changed and no error if (COMPARE_TEMP == 1) { if (lastTemperature[i] != temperature && temperature != -127.00 && temperature != 85.00) { // Send in the new temperature send(msg.setSensor(i+NUMBER_OF_RELAYS+1).set(temperature,1)); // Save new temperatures for next compare lastTemperature[i]=temperature; } } else { if (temperature != -127.00 && temperature != 85.00) { // Send in the new temperature send(msg.setSensor(i+NUMBER_OF_RELAYS+1).set(temperature,1)); // Save new temperatures for next compare lastTemperature[i]=temperature; } } } } } void receive(const MyMessage &message) { // We only expect one type of message from controller. But we better check anyway. if (message.type==V_STATUS) { // Change relay state digitalWrite(PINS[message.sensor-1], message.getBool()?RELAY_ON:RELAY_OFF); // Store state in eeprom saveState(message.sensor, message.getBool()); // Write some debug info Serial.print("Incoming change for sensor:"); Serial.print(message.sensor); Serial.print(", New status: "); Serial.println(message.getBool()); } }