Collecting Data

If you don’t know how to program an Arduino, you can learn about that here.

After you’ve put together a program that generates data, you’ll need a way to send that data to our system. Add our Packet library to your sketch and initialize it like in the example code below.


#include <Packet.h>

Packet p;

void setup() {
p.initialize("Example Project"); // Identify your project here
}

void loop() {
// Collect data and eventually send it with Packet here
  // p.start_packet();
  // p.add_int_field("Name", value);
  // p.end_packet();
  delay(1000); // Wait before sending another
}

Use start_packet() to begin a message. Use add_int_field(name,value), add_double_field(name,value,decimalPlaces), and/or add_text_field(name, value) to add data to the packet. Complete and send the packet with end_packet().

Remember to wait before sending new data.

Here’s an example program for collecting soil moisture data:

#include <Packet.h>

Packet p;

int sensorPin = A0;
int sensorValue = 0;
String soilStatus = "";

void loop() {
  // put your main code here, to run repeatedly:
  sensorValue = analogRead(sensorPin);
  if (sensorValue < 300){
    soilStatus = "Dry";
  } else if (sensorValue < 700){
    soilStatus = "Moist";
  } else {
    soilStatus = "Waterlogged";
  }
  p.start_packet();
  p.add_int_field("Resistivity", sensorValue);
  p.add_text_field("Status", soilStatus.c_str()); // Unsupported but won't cause errors
  p.end_packet(); // Finish and send packet
  delay(1000);
}

You can confirm your program is working by checking it’s serial output (Tools->Serial Monitor in the Arduino IDE). The output is JSON formatted and should look similar to this:

Example serial output

Make sure that you only use the Packet library to communicate over serial. If you’re using an Arduino that supports more than one serial interface (i.e. Arduino Mega), feel free to use the extras for whatever you wish. Packet uses Serial, so you’ll have to use Serial1…3 as described here: MultiSerialMega.