Skip to content

Instantly share code, notes, and snippets.

@bassdread
Created April 9, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bassdread/e2fba4c79c09a8456928 to your computer and use it in GitHub Desktop.
Save bassdread/e2fba4c79c09a8456928 to your computer and use it in GitHub Desktop.
#include <DHT.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_TSL2561_U.h>s
#include <OneWire.h>
#include <VirtualWire.h>
#include<stdlib.h>
char *message;
Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(TSL2561_ADDR_FLOAT, 12345);
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // for serial message complete
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
int DS18S20_Pin = 3;
int DS18S20_Pin_Probe = 4;
int LDR_Pin = A0;
int MOISTURE_Pin = A1;
int MOISTURE_Pin_DI = 5;
int light;
int pumpPin = 6;
float Res0=10.0;
OneWire ds_internal(DS18S20_Pin);
OneWire ds_external(DS18S20_Pin_Probe);
void setup() {
Serial.begin(9600);
dht.begin();
// setup 433
vw_set_tx_pin(12);
vw_setup(4000);// speed of data transfer Kbps
/* Display some basic information on this sensor */
displaySensorDetails();
/* Setup the sensor gain and integration time */
configureSensor();
/* We're ready to go! */
//Serial.println("");
pinMode(pumpPin, OUTPUT);
}
void loop(){
//digitalWrite(7, HIGH);
float h = dht.readHumidity();
//float db_temperature = getTemp();
float temperature = getTempInternal();
float temperature_external = getTempExternal();
//float h_temperature = dht.readTemperature();
//float temperature = h_temperature; //(db_temperature + h_temperature) / 2;
//float temperature = (db_temperature + h_temperature) / 2;
//int photocellReading0 = analogRead(LDR_Pin);
int moisture = analogRead(MOISTURE_Pin);
int moisture_digital = digitalRead(MOISTURE_Pin_DI);
//float Vout0=photocellReading0*0.0048828125; // calculate the voltage
//int light=500/(Res0*((5-Vout0)/Vout0));
/* Get a new sensor event */
sensors_event_t event;
tsl.getEvent(&event);
/* Display the results (light is measured in lux) */
if (event.light)
{
//Serial.print(event.light); Serial.println(" lux");
light = int(event.light);
}
else
{
/* If event.light = 0 lux the sensor is probably saturated
and no reliable data could be generated! */
Serial.println("Sensor overload");
}
if (Serial.available())
{
int speed = Serial.parseInt();
if (speed >= 0 && speed <= 255)
{
analogWrite(pumpPin, speed);
}
}
char tmp[40];
dtostrf(temperature,2,2,tmp);
String location = "g1,";
String temp = tmp;
location = location + temp;
location = location + ",";
dtostrf(h,1,1,tmp);
location = location + tmp;
location = location + ",";
dtostrf(light,1,1,tmp);
location = location + tmp;
location = location + ",";
dtostrf(temperature_external,2,2,tmp);
location = location + tmp;
//location = location + ",";
//dtostrf(moisture,1,1,tmp);
//location = location + tmp;
char charBuf[35];
location.toCharArray(charBuf, 35);
Serial.print("{\"temperature\":");
Serial.print(temperature);
Serial.print(", \"temperatureExternal\":");
Serial.print(temperature_external);
Serial.print(", \"humidity\":");
Serial.print(h);
Serial.print(", \"light\":");
Serial.print(light);
Serial.print(", \"moisture\":");
Serial.print(moisture);
Serial.println("}");
Serial.println(charBuf);
vw_send((uint8_t *)charBuf, strlen(charBuf));
vw_wait_tx(); // Wait until the whole message is gone
stringComplete = false;
//}
delay(1000); //just here to slow down the output for easier reading
}
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
stringComplete = true;
}
}
}
float getTempExternal(){
//returns the temperature from one DS18S20 in DEG Celsius
byte data[12];
byte addr[8];
if ( !ds_external.search(addr)) {
//no more sensors on chain, reset search
ds_external.reset_search();
return -1000;
}
if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return -1000;
}
if ( addr[0] != 0x10 && addr[0] != 0x28) {
Serial.print("Device is not recognized");
return -1000;
}
ds_external.reset();
ds_external.select(addr);
ds_external.write(0x44,1); // start conversion, with parasite power on at the end
byte present = ds_external.reset();
ds_external.select(addr);
ds_external.write(0xBE); // Read Scratchpad
for (int i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds_external.read();
}
ds_external.reset_search();
byte MSB = data[1];
byte LSB = data[0];
float tempRead = ((MSB << 8) | LSB); //using two's compliment
float TemperatureSum = tempRead / 16;
return TemperatureSum;
}
float getTempInternal(){
//returns the temperature from one DS18S20 in DEG Celsius
byte data[12];
byte addr[8];
if ( !ds_internal.search(addr)) {
//no more sensors on chain, reset search
ds_internal.reset_search();
return -1000;
}
if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return -1000;
}
if ( addr[0] != 0x10 && addr[0] != 0x28) {
Serial.print("Device is not recognized");
return -1000;
}
ds_internal.reset();
ds_internal.select(addr);
ds_internal.write(0x44,1); // start conversion, with parasite power on at the end
byte present = ds_internal.reset();
ds_internal.select(addr);
ds_internal.write(0xBE); // Read Scratchpad
for (int i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds_internal.read();
}
ds_internal.reset_search();
byte MSB = data[1];
byte LSB = data[0];
float tempRead = ((MSB << 8) | LSB); //using two's compliment
float TemperatureSum = tempRead / 16;
return TemperatureSum;
}
void displaySensorDetails(void)
{
sensor_t sensor;
tsl.getSensor(&sensor);
/*Serial.println("------------------------------------");
Serial.print ("Sensor: "); Serial.println(sensor.name);
Serial.print ("Driver Ver: "); Serial.println(sensor.version);
Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id);
Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" lux");
Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" lux");
Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" lux");
Serial.println("------------------------------------");
Serial.println("");
delay(500);
*/
}
/**************************************************************************/
/*
Configures the gain and integration time for the TSL2561
*/
/**************************************************************************/
void configureSensor(void)
{
/* You can also manually set the gain or enable auto-gain support */
// tsl.setGain(TSL2561_GAIN_1X); /* No gain ... use in bright light to avoid sensor saturation */
// tsl.setGain(TSL2561_GAIN_16X); /* 16x gain ... use in low light to boost sensitivity */
tsl.enableAutoGain(true); /* Auto-gain ... switches automatically between 1x and 16x */
/* Changing the integration time gives you better sensor resolution (402ms = 16-bit data) */
tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS); /* fast but low resolution */
// tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_101MS); /* medium resolution and speed */
// tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_402MS); /* 16-bit data but slowest conversions */
/* Update these values depending on what you've set above! */
/*Serial.println("------------------------------------");
Serial.print ("Gain: "); Serial.println("Auto");
Serial.print ("Timing: "); Serial.println("13 ms");
Serial.println("------------------------------------");*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment