diff --git a/greenhouse.ino b/greenhouse.ino new file mode 100644 index 0000000..e5bcaa4 --- /dev/null +++ b/greenhouse.ino @@ -0,0 +1,226 @@ +#include +#include +#include +#include + +int motor_in_1 = 5; +int motor_in_2 = 6; +int motor_sleep_pin = 4; +#define SAMPLES 5 +int sample_idx = 0; +int voltage_reads[SAMPLES]; +int moisture_reads[SAMPLES]; + +#define NODEBUG + +#ifdef DEBUG +#define DPRINTLN(str) Serial.println(str) +#define DPRINT(str) Serial.print(str) +#define DPRINTHEX(b) Serial.print(b, HEX) +#define DFLUSH Serial.flush() +#else +#define DPRINTLN(str) +#define DPRINT(str) +#define DPRINTHEX(b) +#define DFLUSH +#endif + +/* + * voltage: 161 fully charged, 1.37V each cell + * voltage: 162 fully charged + bit of sun. + * voltage: 155 after running quite a bit + * moisture: 880 dry / 600 wet + */ + +#define WAIT_FOR_DELAY_OR_DRY 0 +#define WATER_TILL_WET_MAX_TIME 1 +#define WAIT_FOR_MAX_TIME 2 +#define DRY 690 +#define WET 660 +#define DELAY 8 // hours +#define MAX_TIME 20 // mins +#define PUMP_SPEED 128 +#define STATION_ID 2 +#define HOME_ID 1 +#define INTERVAL 10 // seconds + +struct Header { + uint8_t to; + uint8_t from; + uint8_t seq; + uint8_t flags; +}; + +struct State { + unsigned int voltage, moisture, intervals; + unsigned int dry, wet; + byte state, delay_hours, max_time_mins, pumpSpeed; + bool pumpRunning; + byte errors; +}; + +struct Payload { + Header header; + State state; +}; + +State state; +Header my_header = { + HOME_ID, STATION_ID, 0, 0 +}; + +void setup() { +#ifdef DEBUG + Serial.begin(9600); +#endif + state.state = WAIT_FOR_DELAY_OR_DRY; + state.intervals = 0; + state.pumpRunning = false; + state.pumpSpeed = PUMP_SPEED; + state.dry = DRY; + state.wet = WET; + state.delay_hours = DELAY; + state.max_time_mins = MAX_TIME; + state.errors = 0; +// DPRINTLN("Setting one time state to EEPROM"); +// EEPROM.put(0, state); + + analogReference(DEFAULT); + LoRa.setPins(10, 9, 2); + LoRa.enableCrc(); + LoRa.begin(868E6); + LoRa.onReceive(onReceive); + pumpOff(); + for (int i=0; i