/* * Extended from Derek OKeeffe's cloud-rain-monitor for an indi driver. * Arduino firmware for a basic weather station for an astronomical observatory. * Sends sensor readings from sensors: MLX90614 IR, TSL2591 and RG11 * Sends firmata messages as json strings every minute. * if rain sensor transitions from dry to wet message will be sent every second for the first minute. * * * Use: * Adafruit's Adafruit_MLX90614 library and wiring, see: * https://learn.adafruit.com/using-melexis-mlx90614-non-contact-sensors/wiring-and-test * * Adafruit TSL2591 High Dynamic Range Digital Light sensor * Install the TSL2591 library and the Unified Sensor library using the Arduino IDE library manager * * PARTS: * 1 x Arduino nano * 1 x RG-11 Rain sensor * 1 x MLX90614 IR temp sensor * 1 x TSL2591 Lux sensor * * Wiring: * Rain sensor Use mode 1 "Is raining", 12VDC connection. Relay is attached to pin 2 and acts as a * simple on off switch. Connected with a pull-up resistor. * MLX90614 * Wired to A4 and A5 with 2 pull up resistors, see: * https://learn.adafruit.com/using-melexis-mlx90614-non-contact-sensors/wiring-and-test) * * TSL2591 * Wired in parallel with the MLX90614 using a different I2C address * * Sample json message * * {"rain":false,"sky":20.93,"ambient":21.93, "lux":250} * */ #include #include #include #include #include "Adafruit_TSL2591.h" #include #define RAIN_SENSOR_PIN 2 #define DRY_DELAY 60*1000 // 1 minute #define RAIN_DELAY 1000 // 1 second #define RAIN_COUNT 60 Adafruit_MLX90614 mlx = Adafruit_MLX90614(); Adafruit_TSL2591 tsl = Adafruit_TSL2591(2591); long raining = 0; /** * Startup firmata and the mlx */ void setup() { Firmata.setFirmwareVersion(FIRMATA_FIRMWARE_MAJOR_VERSION, FIRMATA_FIRMWARE_MINOR_VERSION); Firmata.begin(9600); pinMode(RAIN_SENSOR_PIN, INPUT); mlx.begin(); tsl.begin(); tsl.setGain(TSL2591_GAIN_HIGH); // LOW | MED | HIGH | MAX tsl.setTiming(TSL2591_INTEGRATIONTIME_500MS); // 100 | 200 | 300 | 400 | 500 | 600 } /** * Once per minute take readings from sensors, build a json string and send over firmata. * If RG11 detects rain, send message once per second or so for first minute. */ void loop() { char charBuf[128]; String message = "{\"rain\":"; uint16_t luminosity = tsl.getLuminosity(TSL2591_VISIBLE); if(digitalRead(RAIN_SENSOR_PIN)==HIGH) { raining = 0; message += "false,"; } else { raining++; message += "true,"; } message += "\"sky\":"; message += mlx.readObjectTempC(); message += ",\"ambient\":"; message += mlx.readAmbientTempC(); message += ",\"lux\":"; message += luminosity; message += "}"; message.toCharArray(charBuf, 128); Firmata.sendString(charBuf); // Delay between messages for (int cnt=0; cnt < 60; cnt++) { delay(1000); if(digitalRead(RAIN_SENSOR_PIN) == LOW) // Is raining { if (raining < 60) break; } } }