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.