Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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+1].value);
- labelsArray.add(readings[i+1].Label);
- unitsArray.add(readings[i+1].Units);
- upperLimArray.add(readings[i+1].upperLimit);
- lowerLimArray.add(readings[i+1].lowerLimit);
- }
- //Send Json over serial
- root.printTo(Serial);
- Serial.println();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement