Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. #include <ArduinoJson.h>
  2.  
  3. //Structure that will hold readings of all the sensors
  4. typedef struct
  5. {
  6. float value =0 ;
  7.  
  8. char Label[20] = "";
  9. char Units[7] = "";
  10.  
  11. float upperLimit = 40;
  12. float lowerLimit = 0;
  13. } SensorReading;
  14.  
  15. //And make an array that will hold the sensor readings
  16. const byte NumberOfSensors = 3;
  17. SensorReading Readings[NumberOfSensors];
  18.  
  19. void setup() {
  20. // put your setup code here, to run once:
  21. Serial.begin(115200);
  22.  
  23. //Setup names and labels for sensors
  24. strcpy(Readings[0].Label, "Humidity");
  25. strcpy(Readings[0].Units, "%");
  26. strcpy(Readings[1].Label, "Air Temp");
  27. strcpy(Readings[1].Units, "Deg C");
  28. strcpy(Readings[2].Label, "Water Temp");
  29. strcpy(Readings[2].Units, "Deg C");
  30.  
  31. }
  32.  
  33. void loop() {
  34. // Read humidity and store it in the data array
  35. Readings[0].value = random(15,25); // replace with actual sensor values
  36. // Read temperature
  37. Readings[1].value = random(20,22); // replace with actual sensor values
  38. Readings[2].value = random(15,17); // replace with actual sensor values
  39. SendData(Readings, NumberOfSensors);
  40.  
  41. delay(1000);
  42. }
  43.  
  44. void SendData(SensorReading readings[], int numberOfReadings){
  45. //Small Json buffer, make larger if you have more sensors
  46. StaticJsonBuffer<300> jsonBuffer;
  47. //Create arrays that contain labels and data
  48. JsonObject& root = jsonBuffer.createObject();
  49. JsonArray& dataArray = root.createNestedArray("data");
  50. JsonArray& labelsArray = root.createNestedArray("labels");
  51. JsonArray& unitsArray = root.createNestedArray("units");
  52. JsonArray& upperLimArray = root.createNestedArray("upperLimits");
  53. JsonArray& lowerLimArray = root.createNestedArray("lowerLimits");
  54.  
  55. // fill in the time
  56. dataArray.add(millis());
  57. labelsArray.add("time");
  58. // these can be empty
  59. unitsArray.add("");
  60. upperLimArray.add("");
  61. lowerLimArray.add("");
  62.  
  63. //put data of all sensors into the arrays
  64. for(int i =0; i < numberOfReadings; i++)
  65. {
  66. dataArray.add(readings[i].value);
  67. labelsArray.add(readings[i].Label);
  68. unitsArray.add(readings[i].Units);
  69. upperLimArray.add(readings[i].upperLimit);
  70. lowerLimArray.add(readings[i].lowerLimit);
  71. }
  72. //Send Json over serial
  73. root.printTo(Serial);
  74. Serial.println();
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement