diff --git a/greenhouse.ino b/greenhouse.ino index e5bcaa4..affd9d0 100644 --- a/greenhouse.ino +++ b/greenhouse.ino @@ -10,6 +10,7 @@ int sample_idx = 0; int voltage_reads[SAMPLES]; int moisture_reads[SAMPLES]; +bool radio_initialized = false; #define NODEBUG @@ -29,6 +30,7 @@ * voltage: 161 fully charged, 1.37V each cell * voltage: 162 fully charged + bit of sun. * voltage: 155 after running quite a bit + * voltage: 130 minimum below which radio no longer works * moisture: 880 dry / 600 wet */ @@ -69,6 +71,13 @@ HOME_ID, STATION_ID, 0, 0 }; +bool setupLoRa() { + LoRa.setPins(10, 9, 2); + LoRa.enableCrc(); + LoRa.onReceive(onReceive); + return LoRa.begin(868E6); +} + void setup() { #ifdef DEBUG Serial.begin(9600); @@ -86,17 +95,21 @@ // EEPROM.put(0, state); analogReference(DEFAULT); - LoRa.setPins(10, 9, 2); - LoRa.enableCrc(); - LoRa.begin(868E6); - LoRa.onReceive(onReceive); pumpOff(); + int voltage = 0; for (int i=0; i 130) { + radio_initialized = setupLoRa(); + if (radio_initialized) { + LoRa.receive(); + } + } } void pumpOn(int speed) { @@ -131,6 +144,10 @@ moisture = moisture + moisture_reads[i]; } state.voltage = voltage / SAMPLES; + if (state.voltage <= 135) { + LoRa.end(); + radio_initialized = false; + } state.moisture = moisture / SAMPLES; DPRINT("voltage = "); DPRINTLN(state.voltage); @@ -140,6 +157,8 @@ DPRINTLN(state.state); DPRINT("intervals = "); DPRINTLN(state.intervals); + DPRINT("radio = "); + DPRINTLN(radio_initialized); DFLUSH; switch(state.state) { case WAIT_FOR_DELAY_OR_DRY: @@ -190,11 +209,15 @@ } void sendState() { - LoRa.beginPacket(); - LoRa.write((byte *)&(my_header), sizeof(Header)); - LoRa.write((byte *)&(state), sizeof(State)); - LoRa.endPacket(); - LoRa.receive(); + if (radio_initialized) { + LoRa.beginPacket(); + LoRa.write((byte *)&(my_header), sizeof(Header)); + LoRa.write((byte *)&(state), sizeof(State)); + LoRa.endPacket(); + LoRa.receive(); + } else { + radio_initialized = setupLoRa(); + } } void onReceive(int packetSize) {