Last active
December 13, 2016 03:04
-
-
Save natan-morar/39c5fb661d9aba41d4e2aaf43ec172b6 to your computer and use it in GitHub Desktop.
arduino simple
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> | |
//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"); | |
} | |
void loop() { | |
// Read humidity and store it in the data array | |
Readings[0].value = random(15,25); // replace with actual sensor values | |
// Read temperature | |
Readings[1].value = random(20,22); // replace with actual sensor values | |
Readings[2].value = random(15,17); // replace with actual sensor values | |
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