Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Updated to v2.0 of Mysensors
- // https://forum.mysensors.org/post/51444
- // Enable debug prints
- #define MY_DEBUG
- #define MY_RADIO_NRF24
- #include <MySensors.h>
- #include <SPI.h>
- #define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))
- #define N_ELEMENTS(array) (sizeof(array)/sizeof((array)[0]))
- #define CHILD_ID_MOISTURE 0
- #define CHILD_ID_BATTERY 1
- #define SLEEP_TIME 10000 // Sleep time between reads (in milliseconds), was 10000
- #define THRESHOLD 1.1 // Only make a new reading with reverse polarity if the change is larger than 10%.
- #define STABILIZATION_TIME 1000 // Let the sensor stabilize before reading default BOD settings
- 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.
- // MySensor gw; //removed for v2.0
- MyMessage msg(CHILD_ID_MOISTURE, V_HUM);
- MyMessage voltage_msg(CHILD_ID_BATTERY, V_VOLTAGE);
- long oldvoltage = 0;
- byte direction = 0;
- int oldMoistureLevel = -1;
- float batteryPcnt;
- float batteryVolt;
- int LED = 5;
- void setup()
- {
- pinMode(LED, OUTPUT);
- digitalWrite(LED, HIGH);
- delay(200);
- digitalWrite(LED, LOW);
- delay(200);
- digitalWrite(LED, HIGH);
- delay(200);
- digitalWrite(LED, LOW);
- // gw.begin(); //Removed for v2.0
- for (int i = 0; i < N_ELEMENTS(SENSOR_ANALOG_PINS); i++) {
- pinMode(SENSOR_ANALOG_PINS[i], OUTPUT);
- digitalWrite(SENSOR_ANALOG_PINS[i], LOW);
- }
- }
- void presentation(){ //created for v2.0
- sendSketchInfo("Plant moisture w solar", "1.0");
- present(CHILD_ID_MOISTURE, S_HUM);
- delay(250);
- present(CHILD_ID_BATTERY, S_MULTIMETER);
- }
- void loop()
- {
- int moistureLevel = readMoisture();
- // Send rolling average of 2 samples to get rid of the "ripple" produced by different resistance in the internal pull-up resistors
- // See http://forum.mysensors.org/topic/2147/office-plant-monitoring/55 for more information
- if (oldMoistureLevel == -1) { // First reading, save current value as old
- oldMoistureLevel = moistureLevel;
- }
- if (moistureLevel > (oldMoistureLevel * THRESHOLD) || moistureLevel < (oldMoistureLevel / THRESHOLD)) {
- // The change was large, so it was probably not caused by the difference in internal pull-ups.
- // Measure again, this time with reversed polarity.
- moistureLevel = readMoisture();
- }
- send(msg.set((moistureLevel + oldMoistureLevel) / 2.0 / 10.23, 1));
- oldMoistureLevel = moistureLevel;
- int sensorValue = analogRead(A0);
- Serial.print("--Sensor value:");Serial.println(sensorValue);
- float voltage=sensorValue*(3.3/1023);
- Serial.print("--Voltage:");Serial.println(voltage);
- batteryPcnt = (sensorValue - 248) * 0.72;
- Serial.print("--Battery %:");Serial.println(batteryPcnt);
- batteryVolt = voltage;
- sendBatteryLevel(batteryPcnt);
- resend((voltage_msg.set(batteryVolt, 3)), 10);
- //send(voltage_msg.set(batteryVolt), 3);
- //flash led to indicate send
- digitalWrite(LED, HIGH);
- delay(200);
- digitalWrite(LED, LOW);
- sleep(SLEEP_TIME);
- }
- void resend(MyMessage &msg, int repeats)
- {
- int repeat = 1;
- int repeatdelay = 0;
- boolean sendOK = false;
- send(msg);
- /*
- while ((sendOK == false) and (repeat < repeats)) {
- if (send(msg)) {
- sendOK = true;
- } else {
- sendOK = false;
- Serial.print("Error ");
- Serial.println(repeat);
- repeatdelay += 500;
- } repeat++; delay(repeatdelay);
- }*/
- }
- int readMoisture() {
- pinMode(SENSOR_ANALOG_PINS[direction], INPUT_PULLUP); // Power on the sensor
- analogRead(SENSOR_ANALOG_PINS[direction]);// Read once to let the ADC capacitor start charging
- sleep(STABILIZATION_TIME);
- int moistureLevel = (1023 - analogRead(SENSOR_ANALOG_PINS[direction]));
- // Turn off the sensor to conserve battery and minimize corrosion
- pinMode(SENSOR_ANALOG_PINS[direction], OUTPUT);
- digitalWrite(SENSOR_ANALOG_PINS[direction], LOW);
- direction = (direction + 1) % 2; // Make direction alternate between 0 and 1 to reverse polarity which reduces corrosion
- return moistureLevel;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement