#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME680.h>
#include <BH1750.h>
#include "RTClib.h"
#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
#include <SPI.h>
#include <DueTimer.h>
//BME680 Setup
Adafruit_BME680 bme;
//BH1750 Setup
BH1750 lightMeter(0x23);
RTC_DS1307 rtc;
//SDI-12 Setup
#define DIRO 7
String command;
int sensor_address = 0;
String deviceIdentification = "allccccccccmmmmmmvvvxxx";
bool startMeasurement = false;
double STemperature = 0;
double SPressure = 0;
double SHumidity = 0;
double SGas = 0;
float SLight = 0;
double temperature = 0;
double pressure = 0;
double humidity = 0;
double gas = 0;
float light = 0;
String formatNumber(int number) {
if (number < 10) {
return "0"+String(number);
}
return String(number);
}
void setup() {
//Arduino IDE Serial Monitor
Wire.begin();
Serial.begin(9600);
if(!rtc.begin()) {
Serial.println("failed rtc");
}
// ================ BME680 ================
if (!bme.begin(0x76)) {
Serial.println("Could not find a valid BME680 sensor, check wiring!");
while (1);
}
// Set the temperature, pressure and humidity oversampling
bme.setTemperatureOversampling(BME680_OS_8X);
bme.setPressureOversampling(BME680_OS_8X);
bme.setHumidityOversampling(BME680_OS_2X);
// ================ BH1750 ================
lightMeter.begin();
// ================ SDI-12 ================
Serial1.begin(1200, SERIAL_7E1); //SDI-12 UART, configures serial port for 7 data bits, even parity, and 1 stop bit
pinMode(DIRO, OUTPUT); //DIRO Pin
Serial.println("Sensors Ready");
//HIGH to Receive from SDI-12
digitalWrite(DIRO, HIGH);
}
void loop() {
int byte;
//Receive SDI-12 over UART and then print to Serial Monitor
if(Serial1.available()) {
byte = Serial1.read(); //Reads incoming communication in bytes
//Serial.println(byte);
if (byte == 33) { //If byte is command terminator (!)
SDI12Receive(command);
command = ""; //reset command string
} else {
if (byte != 0) { //do not add start bit (0)
command += char(byte); //append byte to command string
}
}
}
}
void SDI12Receive(String input) {
DateTime now = rtc.now();
String timeText = formatNumber(now.hour()) + ":" + formatNumber(now.minute()) + ":" + formatNumber(now.second());
Serial.println(timeText);
if (chartAtInt(input, 0) == sensor_address) {
if (chartAt(input, 1) == "A" && input.length() == 3 && chartAtInt(input, 2) >= 0 && chartAtInt(input, 2) <= 9) {
sensor_address = chartAtInt(input, 2);
SDI12Send(String(sensor_address));
}
if (chartAt(input, 1) == "M" && input.length() == 2) {
startMeasurement = true;
bme.performReading();
STemperature = bme.temperature;
SPressure = bme.pressure / 100.0;
SHumidity = bme.humidity;
SGas = bme.gas_resistance / 1000.0;
SLight = lightMeter.readLightLevel();
SDI12Send(String(sensor_address)+"000"+"5");
}
if (chartAt(input, 1) == "D" && input.length() == 3) {
if (startMeasurement) {
if (chartAtInt(input, 2) == 1) {
SDI12Send(String(sensor_address)+(STemperature >= 0 ? "+" : "-")+String(STemperature)+"+"+String(SPressure)+"+"+String(SHumidity)+"+"+String(SGas));
}
if (chartAtInt(input, 2) == 2) {
SDI12Send(String(sensor_address)+"+"+String(SLight));
}
}
}
if (chartAt(input, 1) == "R" && chartAt(input, 2) == "0" && input.length() == 3) {
bme.performReading();
temperature = bme.temperature;
pressure = bme.pressure / 100.0;
humidity = bme.humidity;
gas = bme.gas_resistance / 1000.0;
light = lightMeter.readLightLevel();
SDI12Send(String(sensor_address)+(temperature >= 0 ? "+" : "-")+String(temperature)+"+"+String(pressure)+"+"+String(humidity)+"+"+String(gas)+"+"+String(light));
}
}
if (String(input.charAt(0)) == "?") {
SDI12Send(String(sensor_address));
}
}
void SDI12Send(String message) {
digitalWrite(DIRO, LOW);
delay(100);
Serial1.print(message + String("\r\n"));
Serial1.flush();
Serial1.end();
Serial1.begin(1200, SERIAL_7E1);
digitalWrite(DIRO, HIGH);
}
String chartAt(String input, int at) {
return String(input.charAt(at));
}
int chartAtInt(String input, int at) {
int a;
a = input.charAt(at) - '0';
return a;
}