Basics of outputting AJSON over serial.

Hello,

All I really want to do here is output two values. The temperature and the current pressure. Later, I'll detect movement. The pir is connected and working but just to keep things simple, temperature and pressure would do fine at the moment.

From what I've gathered, AJSON is the best way of outputting via serial when you want to use a Python script to captures these values. I tried just printing to serial and reading the results but this was messy and not accurate.

What I would love would be to use JSon in Python to create a dictionary. One Key called Temperature and the other called Pressure. Later, as I said before, if I wanted one called PIR moved, this shouldn't be too difficult. I could then use these values in any way I wanted in Python.
The problem is, how do I get these values over accurately?

The Arduino code that I have written and partially taken from the AJSON example is shown below. The values that it is printing for temperature and pressure are wildly off.

Temperature should be the first number and pressure should be the second.

#include <Wire.h>
#include <Adafruit_BMP085.h>
#include <LiquidCrystal.h>
#include <passive_timer.h>
#define RED_LED_PIN 9
#define GREEN_LED_PIN 8
#include <aJSON.h>

PassiveTimer green_led_timer;				// Tracks the green Led time.
PassiveTimer red_led_timer;				// Tracks the red Led time.
PassiveTimer temperature_timer;				// tracks the temperature test time
boolean green_led_is_on;				// Used for green LED timer logic. 
boolean red_led_is_on;					// Used for red LED timer logic. 
boolean room_is_cold;					// Indicates if temperature levels have reached cold. . 
unsigned long last_print = 0;
aJsonStream serial_stream(&Serial);			// AJSON

Adafruit_BMP085 bmp;
 LiquidCrystal lcd(12, 11, 5, 4, 3, 6);

void red_led_setup (){
  red_led_is_on = false;
  pinMode(RED_LED_PIN, OUTPUT);
  digitalWrite(RED_LED_PIN, LOW);
}

void green_led_setup (){
  green_led_is_on = true;
  pinMode(GREEN_LED_PIN, OUTPUT);
  digitalWrite(GREEN_LED_PIN, HIGH);
}

void lcd_setup () {
   lcd.begin(16, 2);
   lcd.print("Initializing Enviro-Sense");
}

void setup() {
  lcd_setup();
  Serial.begin(9600);
  if (!bmp.begin()) {
	Serial.println("Could not find a valid BMP085 sensor, check wiring!");
	lcd.print("Could not find a valid BMP085 sensor, check wiring!");
	while (1) {}
  }
  red_led_setup();
  green_led_setup();
  lcd.setCursor(0, 0);
  lcd.print("Temp: ");
  lcd.setCursor(0, 1);
  lcd.print("Pres: ");
}
aJsonObject *createMessage()
{
  aJsonObject *environment = aJson.createObject();
  float sensetemp = bmp.readTemperature();
  float sensepress = bmp.readPressure();
  int Values[2];
Values[1] = sensetemp;
Values[2] = sensepress;

  aJsonObject *EnvTest = aJson.createIntArray(Values, 2);
  aJson.addItemToObject(environment, "SensorValues", EnvTest);

  return environment;
}

/* Process message like: { "pwm": { "8": 0, "9": 128 } } */
void processMessage(aJsonObject *environment)
{
  aJsonObject *pwm = aJson.getObjectItem(environment, "pwm");
  if (!pwm) {
    Serial.println("no pwm data");
    return;
  }
}

void loop() {
  if (millis() - last_print > 1000) {
    /* One second elapsed, send message. */
    lcd.setCursor(0, 1);
    lcd.setCursor(6, 0);
    lcd.print(bmp.readTemperature());
    lcd.setCursor(6, 1);
    lcd.print(bmp.readPressure());

    aJsonObject *environment = createMessage();
    aJson.print(environment, &serial_stream);
    Serial.println(); /* Add newline. */
    aJson.deleteItem(environment);
    last_print = millis();
  }
  if (serial_stream.available()) {
    /* First, skip any accidental whitespace like newlines. */
    serial_stream.skip();
  }

  if (serial_stream.available()) {
    /* Something real on input, let's take a look. */
    aJsonObject *environment = aJson.parse(&serial_stream);
    processMessage(environment);
    aJson.deleteItem(environment);
  }

}

An example of the output is here:

{"SensorValues":[13825,19]}
{"SensorValues":[13825,19]}
{"SensorValues":[13825,19]}
{"SensorValues":[13825,19]}
{"SensorValues":[13825,19]}
{"SensorValues":[13825,19]}

Temperature is currently 19.77C and pressure is 97957 but this changes frequently by minor amounts.

I thought this might be printing the numbers in the wrong order. I also thought that it was because the array was an integer.

When I try to substitute CreateIntArray with IntFloatArray I encount an error. Even though CreateFloatArray is one of the methods and functions.

If I am going about this in the wrong way please let me know.

As I've said before, I'm far from being a developer. I am still in the early stages of learning. I love the sound of JSON but I have only scratched the surface.

From what I've gathered, AJSON is the best way of outputting via serial when you want to use a Python script to captures these values.

No idea where you got that impression.

  int Values[2];
Values[1] = sensetemp;
Values[2] = sensepress;

Wrong. The index values start at 0.

If I were you I'd just print the data as comma separated decimal values, with each data set separated by a newline.

Peter, Thanks but if I send out CSV, How can I be absolutely sure that what I'm getting back is accurate?

Paul, Thanks. That was a stupid thing for me to do.
Paul, IF I don't use AJSON, what do you recommend?

JSON is just a way of expressing data.

Its also not guaranteed... For sure you get a closing brace signifying the end of data / pseudo validity of data. However transmission errors can easily cause an '}' character ( or any other ) to be read by the receiving end. Its only with strong validation that you can ensure what you are receiving is correct.

Maybe you could send your data ( csv, json ) multiple times, say 3, if all three do not match dump it all. Going more complex, you could employ networking strategies by using a known layout, or 'packet'. Even incorporating a data integrity check like CRC16/32 or MD5.

As you are using python I'd assume you can do almost anything ( not used python ). If it contains a JSON interpreter, then maybe it'll make the python side easier, however you will still not have a guarantee of integrity.

Nothing you've said so far makes me think you need to do any special validation of the received data, but if there is a requirement then an easy approach is to generate a hashcode of the data you're sending and include that value in the data so that the recipient can validate it. You can do that whatever encoding scheme you're using. CSV is just a simpler easier encoding scheme than JSON - there are no other particular advantages or disadvantages between them in this application.