Skip to content

Instantly share code, notes, and snippets.

@bassdread
Created January 13, 2014 20:29
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/8407496 to your computer and use it in GitHub Desktop.
Save bassdread/8407496 to your computer and use it in GitHub Desktop.
Code for output json from Arduino. The code also listens on serial for commands to turn the pump on and off.
#include <DHT.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP085.h>
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
int LDR_Pin = A0;
int MOISTURE_1_Pin = A1;
int MOISTURE_2_Pin = A2;
int pump = 10;
Adafruit_BMP085 bmp = Adafruit_BMP085(10085);
String inputString = "";
boolean stringComplete = false; // whether the string is complete
const int pin = A0; // Analog input pin
unsigned long duration;
float Res0=10.0;
int photocellReading0;
void setup(){
Serial.begin(9600);
pinMode(pump, OUTPUT);
/* Initialise the sensor */
if(!bmp.begin())
{
/* There was a problem detecting the BMP085 ... check your connections */
Serial.print("Ooops, no BMP085 detected ... Check your wiring or I2C ADDR!");
while(1);
}
dht.begin();
}
void loop(){
sensors_event_t event;
bmp.getEvent(&event);
if (event.pressure) {
int photocellReading0 = analogRead(LDR_Pin);
float Vout0=photocellReading0 * 0.0048828125; // calculate the voltage
int lux0 = 500 / (Res0*((5-Vout0)/Vout0));
Serial.print("{\"ldr\":");
Serial.print(lux0);
Serial.print(",");
/* Display atmospheric pressue in hPa */
Serial.print("\"pressure\":");
Serial.print(event.pressure);
Serial.print(",");
float temperature2;
bmp.getTemperature(&temperature2);
Serial.print("\"temp1\":");
Serial.print(temperature2);
float h = dht.readHumidity();
float t = dht.readTemperature();
Serial.print(", \"humidity\":");
Serial.print(h);
Serial.print(",");
Serial.print("\"temp2\":");
Serial.print(t);
int moisture_1 = analogRead(MOISTURE_1_Pin);
int moisture_2 = analogRead(MOISTURE_2_Pin);
Serial.print(",");
Serial.print("\"waterlevel\":");
Serial.print(moisture_1);
Serial.print(",");
Serial.print("\"moisture\":");
Serial.print(moisture_2);
Serial.println("}");
}
if (stringComplete) {
inputString.replace("\n","");
if (inputString == "on")
{
digitalWrite(pump, HIGH);
}
if (inputString == "off")
{
digitalWrite(pump, LOW);
}
// clear the string:
inputString = "";
stringComplete = false;
}
delay(3000); //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;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment