Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. #include <ArduinoJson.h>
  2. #include <DallasTemperature.h>
  3. #include <DHT.h>
  4.  
  5. //Let's say we have some DHT sensors
  6. #define DHTPIN 2
  7. #define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
  8. DHT dht(DHTPIN, DHTTYPE);
  9.  
  10. //And let's say we've got DS18B20
  11. #define ONE_WIRE_BUS 2
  12. OneWire oneWire(ONE_WIRE_BUS);
  13. DallasTemperature tempSensors(&oneWire);
  14.  
  15. //Structure that will hold readings of all the sensors
  16. typedef struct
  17. {
  18. float value =0 ;
  19.  
  20. char Label[20] = "";
  21. char Units[7] = "";
  22.  
  23. float upperLimit = 40;
  24. float lowerLimit = 0;
  25. } SensorReading;
  26.  
  27. //And make an array that will hold the sensor readings
  28. const byte NumberOfSensors = 3;
  29. SensorReading Readings[NumberOfSensors];
  30.  
  31. void setup() {
  32. // put your setup code here, to run once:
  33. Serial.begin(115200);
  34.  
  35. //Setup names and labels for sensors
  36. strcpy(Readings[0].Label, "Humidity");
  37. strcpy(Readings[0].Units, "%");
  38. strcpy(Readings[1].Label, "Air Temp");
  39. strcpy(Readings[1].Units, "Deg C");
  40. strcpy(Readings[2].Label, "Water Temp");
  41. strcpy(Readings[2].Units, "Deg C");
  42.  
  43. //Start DHT library
  44. dht.begin();
  45.  
  46. // Start up the DS18B20 library
  47. tempSensors.begin();
  48. }
  49.  
  50. void loop() {
  51. // Read humidity and store it in the data array
  52. Readings[0].value = dht.readHumidity();
  53. // Read temperature
  54. Readings[1].value = dht.readTemperature();
  55.  
  56. tempSensors.requestTemperatures();
  57. //just going to read the first sensor, change if you have more
  58. Readings[2].value = tempSensors.getTempCByIndex(0);
  59. SendData(Readings, NumberOfSensors);
  60. delay(1000);
  61. }
  62.  
  63. void SendData(SensorReading readings[], int sensors){
  64. //Small Json buffer, make larger if you have more sensors
  65. StaticJsonBuffer<300> jsonBuffer;
  66. //Create arrays that contain labels and data
  67. JsonObject& root = jsonBuffer.createObject();
  68. JsonArray& dataArray = root.createNestedArray("data");
  69. JsonArray& labelsArray = root.createNestedArray("labels");
  70. JsonArray& unitsArray = root.createNestedArray("units");
  71. JsonArray& upperLimArray = root.createNestedArray("upperLimits");
  72. JsonArray& lowerLimArray = root.createNestedArray("lowerLimits");
  73.  
  74. // fill in the time
  75. dataArray.add(millis());
  76. labelsArray.add("time");
  77. // these can be empty
  78. unitsArray.add("");
  79. upperLimArray.add("");
  80. lowerLimArray.add("");
  81.  
  82. //put data of all sensors into the arrays
  83. for(int i =0; i < sensors; i++)
  84. {
  85. dataArray.add(readings[i+1].value);
  86. labelsArray.add(readings[i+1].Label);
  87. unitsArray.add(readings[i+1].Units);
  88. upperLimArray.add(readings[i+1].upperLimit);
  89. lowerLimArray.add(readings[i+1].lowerLimit);
  90. }
  91. //Send Json over serial
  92. root.printTo(Serial);
  93. Serial.println();
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement