Advertisement
huczas

Untitled

Feb 2nd, 2017
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.05 KB | None | 0 0
  1. // Updated to v2.0 of Mysensors
  2. // https://forum.mysensors.org/post/51444
  3. // Enable debug prints
  4. #define MY_DEBUG
  5. #define MY_RADIO_NRF24
  6.  
  7. #include <MySensors.h>
  8. #include <SPI.h>
  9.  
  10. #define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))
  11. #define N_ELEMENTS(array) (sizeof(array)/sizeof((array)[0]))
  12.  
  13. #define CHILD_ID_MOISTURE 0
  14. #define CHILD_ID_BATTERY 1
  15. #define SLEEP_TIME 10000 // Sleep time between reads (in milliseconds), was 10000
  16. #define THRESHOLD 1.1 // Only make a new reading with reverse polarity if the change is larger than 10%.
  17. #define STABILIZATION_TIME 1000 // Let the sensor stabilize before reading default BOD settings
  18. const int SENSOR_ANALOG_PINS[] = {A4, A5}; // Sensor is connected to these two pins. Avoid A3 if using ATSHA204. A6 and A7 cannot be used because they don't have pullups.
  19.  
  20. // MySensor gw;   //removed for v2.0
  21. MyMessage msg(CHILD_ID_MOISTURE, V_HUM);
  22. MyMessage voltage_msg(CHILD_ID_BATTERY, V_VOLTAGE);
  23. long oldvoltage = 0;
  24. byte direction = 0;
  25. int oldMoistureLevel = -1;
  26. float batteryPcnt;
  27. float batteryVolt;
  28. int LED = 5;
  29.  
  30. void setup()
  31. {
  32.   pinMode(LED, OUTPUT);
  33.   digitalWrite(LED, HIGH);
  34.   delay(200);
  35.   digitalWrite(LED, LOW);
  36.   delay(200);
  37.   digitalWrite(LED, HIGH);
  38.   delay(200);
  39.   digitalWrite(LED, LOW);
  40.  
  41.   //  gw.begin(); //Removed for v2.0
  42.   for (int i = 0; i < N_ELEMENTS(SENSOR_ANALOG_PINS); i++) {
  43.     pinMode(SENSOR_ANALOG_PINS[i], OUTPUT);
  44.     digitalWrite(SENSOR_ANALOG_PINS[i], LOW);
  45.   }
  46. }
  47.  
  48. void presentation(){  //created for v2.0
  49.   sendSketchInfo("Plant moisture w solar", "1.0");
  50.   present(CHILD_ID_MOISTURE, S_HUM);
  51.   delay(250);
  52.   present(CHILD_ID_BATTERY, S_MULTIMETER);
  53. }
  54.  
  55.  
  56. void loop()
  57. {
  58.   int moistureLevel = readMoisture();
  59.  
  60.   // Send rolling average of 2 samples to get rid of the "ripple" produced by different resistance in the internal pull-up resistors
  61.   // See http://forum.mysensors.org/topic/2147/office-plant-monitoring/55 for more information
  62.   if (oldMoistureLevel == -1) { // First reading, save current value as old
  63.     oldMoistureLevel = moistureLevel;
  64.   }
  65.   if (moistureLevel > (oldMoistureLevel * THRESHOLD) || moistureLevel < (oldMoistureLevel / THRESHOLD)) {
  66.     // The change was large, so it was probably not caused by the difference in internal pull-ups.
  67.     // Measure again, this time with reversed polarity.
  68.     moistureLevel = readMoisture();
  69.   }
  70.   send(msg.set((moistureLevel + oldMoistureLevel) / 2.0 / 10.23, 1));
  71.   oldMoistureLevel = moistureLevel;
  72.  
  73.   int sensorValue = analogRead(A0);
  74.   Serial.print("--Sensor value:");Serial.println(sensorValue);
  75.   float voltage=sensorValue*(3.3/1023);
  76.   Serial.print("--Voltage:");Serial.println(voltage);
  77.   batteryPcnt = (sensorValue - 248) * 0.72;
  78.   Serial.print("--Battery %:");Serial.println(batteryPcnt);
  79.   batteryVolt = voltage;
  80.   sendBatteryLevel(batteryPcnt);
  81.   resend((voltage_msg.set(batteryVolt, 3)), 10);
  82.   //send(voltage_msg.set(batteryVolt), 3);
  83.  
  84.   //flash led to indicate send
  85.   digitalWrite(LED, HIGH);
  86.   delay(200);
  87.   digitalWrite(LED, LOW);
  88.  
  89.   sleep(SLEEP_TIME);
  90. }
  91.  
  92. void resend(MyMessage &msg, int repeats)
  93. {
  94.   int repeat = 1;
  95.   int repeatdelay = 0;
  96.   boolean sendOK = false;
  97.  
  98.   send(msg);
  99. /*
  100.   while ((sendOK == false) and (repeat < repeats)) {
  101.     if (send(msg)) {
  102.       sendOK = true;
  103.     } else {
  104.       sendOK = false;
  105.       Serial.print("Error ");
  106.       Serial.println(repeat);
  107.       repeatdelay += 500;
  108.     } repeat++; delay(repeatdelay);
  109.   }*/
  110. }
  111.  
  112.  
  113. int readMoisture() {
  114.   pinMode(SENSOR_ANALOG_PINS[direction], INPUT_PULLUP); // Power on the sensor
  115.   analogRead(SENSOR_ANALOG_PINS[direction]);// Read once to let the ADC capacitor start charging
  116.   sleep(STABILIZATION_TIME);
  117.   int moistureLevel = (1023 - analogRead(SENSOR_ANALOG_PINS[direction]));
  118.  
  119.   // Turn off the sensor to conserve battery and minimize corrosion
  120.   pinMode(SENSOR_ANALOG_PINS[direction], OUTPUT);
  121.   digitalWrite(SENSOR_ANALOG_PINS[direction], LOW);
  122.  
  123.   direction = (direction + 1) % 2; // Make direction alternate between 0 and 1 to reverse polarity which reduces corrosion
  124.   return moistureLevel;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement