Last active
December 3, 2016 05:15
-
-
Save natan-morar/8cd54bd971dd65cd11ba5273ca48385d to your computer and use it in GitHub Desktop.
interface with temp humidity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ArduinoJson.h> | |
#include <DallasTemperature.h> | |
#include <DHT.h> | |
//Let's say we have some DHT sensors | |
#define DHTPIN 2 | |
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321 | |
DHT dht(DHTPIN, DHTTYPE); | |
//And let's say we've got DS18B20 | |
#define ONE_WIRE_BUS 2 | |
OneWire oneWire(ONE_WIRE_BUS); | |
DallasTemperature tempSensors(&oneWire); | |
//Structure that will hold readings of all the sensors | |
typedef struct | |
{ | |
float value =0 ; | |
char Label[20] = ""; | |
char Units[7] = ""; | |
float upperLimit = 40; | |
float lowerLimit = 0; | |
} SensorReading; | |
//And make an array that will hold the sensor readings | |
const byte NumberOfSensors = 3; | |
SensorReading Readings[NumberOfSensors]; | |
void setup() { | |
// put your setup code here, to run once: | |
Serial.begin(115200); | |
//Setup names and labels for sensors | |
strcpy(Readings[0].Label, "Humidity"); | |
strcpy(Readings[0].Units, "%"); | |
strcpy(Readings[1].Label, "Air Temp"); | |
strcpy(Readings[1].Units, "Deg C"); | |
strcpy(Readings[2].Label, "Water Temp"); | |
strcpy(Readings[2].Units, "Deg C"); | |
//Start DHT library | |
dht.begin(); | |
// Start up the DS18B20 library | |
tempSensors.begin(); | |
} | |
void loop() { | |
// Read humidity and store it in the data array | |
Readings[0].value = dht.readHumidity(); | |
// Read temperature | |
Readings[1].value = dht.readTemperature(); | |
tempSensors.requestTemperatures(); | |
//just going to read the first sensor, change if you have more | |
Readings[2].value = tempSensors.getTempCByIndex(0); | |
SendData(Readings, NumberOfSensors); | |
delay(1000); | |
} | |
void SendData(SensorReading readings[], int sensors){ | |
//Small Json buffer, make larger if you have more sensors | |
StaticJsonBuffer<300> jsonBuffer; | |
//Create arrays that contain labels and data | |
JsonObject& root = jsonBuffer.createObject(); | |
JsonArray& dataArray = root.createNestedArray("data"); | |
JsonArray& labelsArray = root.createNestedArray("labels"); | |
JsonArray& unitsArray = root.createNestedArray("units"); | |
JsonArray& upperLimArray = root.createNestedArray("upperLimits"); | |
JsonArray& lowerLimArray = root.createNestedArray("lowerLimits"); | |
// fill in the time | |
dataArray.add(millis()); | |
labelsArray.add("time"); | |
// these can be empty | |
unitsArray.add(""); | |
upperLimArray.add(""); | |
lowerLimArray.add(""); | |
//put data of all sensors into the arrays | |
for(int i =0; i < sensors; i++) | |
{ | |
dataArray.add(readings[i].value); | |
labelsArray.add(readings[i].Label); | |
unitsArray.add(readings[i].Units); | |
upperLimArray.add(readings[i].upperLimit); | |
lowerLimArray.add(readings[i].lowerLimit); | |
} | |
//Send Json over serial | |
root.printTo(Serial); | |
Serial.println(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment