Advertisement
seston

Moisture with relays v0.3

Apr 3rd, 2017
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.11 KB | None | 0 0
  1.  
  2. #define MY_RADIO_NRF24
  3. #define MY_GATEWAY_SERIAL
  4.  
  5.  
  6. #define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))
  7. #define MY_DEBUG
  8. #define CHILD_ID_BATTERY 0
  9. #define SLEEP_TIME 600000 // Sleep time between reads (in milliseconds)
  10. #define STABILIZATION_TIME 500 // Let the sensor stabilize 0.5 seconds before reading
  11. #define BATTERY_FULL 3000 // 3,000 millivolts when battery is full (assuming 2xAA)
  12. #define BATTERY_ZERO 1700 // 1,700 millivolts when battery is empty (reqires blown brownout detection fuse, use 2,800 otherwise)
  13. // This sketch assumes power from 2xAA on Vcc (not RAW). Remove the power led and the voltage regulator for better battery life.
  14. // Sensors shall be connected to GND and their analog pin. Throw away the middle chip, just use the pitchfork.
  15. // Number of analog pins for each Arduino model can be seen at https://www.arduino.cc/en/Products/Compare
  16. // A5 and A6 on most Arduinos cannot be used because they don't have internal pullups
  17. const int SENSORS[] = {A0, A1, A2, A3, A4, A5}; // Remove the pins that you don't want to use
  18. #define N_ELEMENTS(array) (sizeof(array)/sizeof((array)[0]))
  19. long oldvoltage = 0;
  20.  
  21. #define RELAY_1 3 // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
  22. #define NUMBER_OF_RELAYS 1 // Total number of attached relays
  23. #define RELAY_ON 1 // GPIO value to write to turn on attached relay
  24. #define RELAY_OFF 0 // GPIO value to write to turn off attached relay
  25.  
  26. #include <SPI.h>
  27. #include <MySensors.h>
  28.  
  29. MyMessage moisture_messages[N_ELEMENTS(SENSORS)];
  30. MyMessage voltage_msg(CHILD_ID_BATTERY, V_VOLTAGE);
  31.  
  32. void before()
  33. {
  34. for (int sensor = 1, pin = RELAY_1; sensor <= NUMBER_OF_RELAYS; sensor++, pin++) {
  35. // Then set relay pins in output mode
  36. pinMode(pin, OUTPUT);
  37.  
  38. }
  39. }
  40.  
  41.  
  42. void setup()
  43. {
  44. //gw.begin();
  45. sendSketchInfo("Plants moisture w bat", "1.2");
  46. present(CHILD_ID_BATTERY, S_CUSTOM);
  47. sendSketchInfo("Relay", "1.0");
  48.  
  49. for (int sensor = 0; sensor < N_ELEMENTS(SENSORS); sensor++) {
  50. moisture_messages[sensor].sensor = sensor + 1; // Battery uses child ID 0 so sensors start at 1
  51. moisture_messages[sensor].type = V_HUM;
  52. delay(250);
  53. present(sensor + 1, S_HUM);
  54. }
  55. for (int i = 0; i < N_ELEMENTS(SENSORS); i++) {
  56. pinMode(SENSORS[i], OUTPUT);
  57. digitalWrite(SENSORS[i], LOW);
  58. }
  59.  
  60. for (int sensor = 1, pin = RELAY_1; sensor <= NUMBER_OF_RELAYS; sensor++, pin++) {
  61.  
  62. }
  63. }
  64.  
  65. void receive(const MyMessage &message)
  66. {
  67. {
  68. // We only expect one type of message from controller. But we better check anyway.
  69. if (message.type == V_STATUS) {
  70. // Change relay state
  71. digitalWrite(message.sensor - 1 + RELAY_1, message.getBool() ? RELAY_ON : RELAY_OFF);
  72. // Store state in eeprom
  73. saveState(message.sensor, message.getBool());
  74. // Write some debug info
  75. Serial.print("Incoming change for sensor:");
  76. Serial.print(message.sensor);
  77. Serial.print(", New status: ");
  78. Serial.println(message.getBool());
  79. }
  80.  
  81. void loop()
  82.  
  83.  
  84. {
  85. for (int sensor = 0; sensor < N_ELEMENTS(SENSORS); sensor++) {
  86. pinMode(SENSORS[sensor], INPUT_PULLUP); // "Power on" the sensor and activate the internal pullup resistor
  87. analogRead(SENSORS[sensor]); // Read once to let the ADC capacitor start charging
  88. sleep(STABILIZATION_TIME);
  89. int moistureLevel = (1023 - analogRead(SENSORS[sensor])) / 10.23;
  90. }
  91.  
  92. // Turn off the sensor to conserve battery and minimize corrosion
  93. pinMode(SENSORS[sensor], OUTPUT);
  94. digitalWrite(SENSORS[sensor], LOW);
  95.  
  96. send(moisture_messages[sensor].set(moistureLevel));
  97. }
  98.  
  99. long voltage = readVcc();
  100. if (oldvoltage != voltage) { // Only send battery information if voltage has changed, to conserve battery.
  101. send(voltage_msg.set(voltage / 1000.0, 3)); // redVcc returns millivolts and set wants volts and how many decimals (3 in our case)
  102. sendBatteryLevel(round((voltage - BATTERY_ZERO) * 100.0 / (BATTERY_FULL - BATTERY_ZERO)));
  103. oldvoltage = voltage;
  104. }
  105. sleep(SLEEP_TIME);
  106. }
  107.  
  108. long readVcc()
  109.  
  110.  
  111. // From http://provideyourown.com/2012/secret-arduino-voltmeter-measure-battery-voltage/
  112. // Read 1.1V reference against AVcc
  113. // set the reference to Vcc and the measurement to the internal 1.1V reference
  114. #if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  115. ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  116. #elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
  117. ADMUX = _BV(MUX5) | _BV(MUX0);
  118. #elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
  119. ADMUX = _BV(MUX3) | _BV(MUX2);
  120. #else
  121. ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  122. #endif
  123.  
  124. delay(2); // Wait for Vref to settle
  125. ADCSRA |= _BV(ADSC); // Start conversion
  126. while (bit_is_set(ADCSRA, ADSC)); // measuring
  127.  
  128. uint8_t low = ADCL; // must read ADCL first - it then locks ADCH
  129. uint8_t high = ADCH; // unlocks both
  130.  
  131. long result = (high << 8) | low;
  132.  
  133. result = 1125300L / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000
  134. return result; // Vcc in millivolts
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement