I've been trying to get this to work for so long it's ridiculous. I have an Arduino Uno hooked up to my Pi via Serial. At first I tried USB, but then hardwire serial. I'm using a simple voltage divider with 220? on the Arduino side and 1K? on the other going to ground. It gives about 3.1v DC that the Pi accepts fine. ~~I don't want to have to say anything for a third time, so please read both Recieved Data is Scrambled and Not Correct · Issue #297 · serialport/node-serialport · GitHub and Serial data scrambled (solved) - Raspberry Pi Forums
Example of data:
{"data":{"temp":68.00000,"hum":0.00000,"press":35.65534,"wd":0.00000,"ws":0.00000,"light":0.00000},"status":1}
{"data":{"temp":67.66250,"hum":0.00000,"press":35.65207,"wd":0.00000,"ws":0.00000,"light":0.00000},"status":1}
{"data":{"temp":67.88750,"hum":0.00000,"press":35.65749,"wd":0.00000,"ws":0.00000,"light":0.00000},"status":1}
{"data":{"temp":68.00000,"hum":0.00000,"press":35.65800,"wd":0.00000,"ws":0.00000,"light":0.00000},"status":1}
{"data":{"temp":67.88750,"hum":0.00000,"press":35.65688,"wd":0.00000,"ws":0.00000,"light":0.00000},"status":1}
{"data":{"temp":68.00000,"hum":0.00000,"press":35.65836,"wd":0.00000,"ws":0.00000,"light":0.00000},"status":1}
{"data":{"temp":67.88750,"hum":0.00000,"press":35.65681,"wd":0.00000,"":0.00000},"":1}
It screws up the last one and then prints blank lines. When I tried this on my Mega R3, it would get a little farther and really scramble and shorten the last line.
I've come to the conslusion that it's the Arduino. I think it has to do with my I2C Pressure Sensor (SparkFun Altitude/Pressure Sensor Breakout - MPL3115A2 - SEN-11084 - SparkFun Electronics). I think that it cannot provide the information fast enough, even though it says it has the data. I tried it at 9600 baud, and it still didn't work. However, that's as low as I'm willing to go, as this is a real-time application that requires data as the sensor sees it.
My sketch:
#include <Wire.h>
#include <Math.h>
#include <aJSON.h>
#include "MPL3115A2.h"
aJsonStream serial_stream(&Serial);
//Create an instance of the object
MPL3115A2 myPressure;
int oldTemperature;
float oldPressure;
float oldHumidity;
int oldCompareBaroin;
float oldWindDirection;
float oldWindSpeed;
float oldAmbientLight;
void setup() {
Wire.begin(); // Join i2c bus
Serial.begin(115200); // Start serial for output
myPressure.begin(); // Get sensor online
//Configure the sensor
//myPressure.setModeAltimeter(); // Measure altitude above sea level in meters
myPressure.setModeBarometer(); // Measure pressure in Pascals from 20 to 110 kPa
myPressure.setOversampleRate(7); // Set Oversample to the recommended 128
myPressure.enableEventFlags(); // Enable all three pressure and temp event flags
}
void loop() {
float temperature = myPressure.readTempF();
float humidity = 0;
float pressure = myPressure.readPressure();
float windDirection = 0;
float windSpeed = 0;
float ambientLight = 0;
//References:
//Definition of "altimeter setting": http://www.crh.noaa.gov/bou/awebphp/definitions_pressure.php
//Altimeter setting: http://www.srh.noaa.gov/epz/?n=wxcalc_altimetersetting
//Altimeter setting: http://www.srh.noaa.gov/images/epz/wxcalc/altimeterSetting.pdf
//Verified against Boulder, CO readings: http://www.crh.noaa.gov/bou/include/webpres.php?product=webpres.txt
//const int station_elevation_ft = 5374; //Must be obtained with a GPS unit
//float station_elevation_m = station_elevation_ft * 0.3048; //I'm going to hard code this
const int station_elevation_m = 1638; //Accurate for the roof on my house
//1 pascal = 0.01 millibars
pressure /= 100; //pressure is now in millibars
float part1 = pressure - 0.3; //Part 1 of formula
const float part2 = 8.42288 / 100000.0;
float part3 = pow((pressure - 0.3), 0.190284);
float part4 = (float)station_elevation_m / part3;
float part5 = (1.0 + (part2 * part4));
float part6 = pow(part5, (1.0/0.190284));
float altimeter_setting_pressure_mb = part1 * part6; //Output is now in adjusted millibars
float baroin = altimeter_setting_pressure_mb * 0.02953;
float compareTemperatureFloat = temperature * 1000;
int compareTemperature = round(compareTemperatureFloat);
float compareBaroinFloat = baroin * 100;
int compareBaroin = round(compareBaroinFloat);
if (compareTemperature != oldTemperature) {
sendSerial(temperature, humidity, baroin, windDirection, windSpeed, ambientLight);
}
// else if (humidity != oldHumidity) {
// sendSerial(temperature, humidity, baroin, windDirection, windSpeed, ambientLight);
// }
else if (compareBaroin != oldCompareBaroin) {
sendSerial(temperature, humidity, baroin, windDirection, windSpeed, ambientLight);
}
// else if (windDirection != oldWindDirection) {
// sendSerial(temperature, humidity, baroin, windDirection, windSpeed, ambientLight);
// }
// else if (windSpeed != oldWindSpeed) {
// sendSerial(temperature, humidity, baroin, windDirection, windSpeed, ambientLight);
// }
// else if (ambientLight != oldAmbientLight) {
// sendSerial(temperature, humidity, baroin, windDirection, windSpeed, ambientLight);
// }
oldTemperature = compareTemperature;
oldHumidity = humidity;
oldCompareBaroin = compareBaroin;
oldWindDirection = windDirection;
oldWindSpeed = windSpeed;
oldAmbientLight = ambientLight;
}
void sendSerial(float temp, float hum, float press, float wd, float ws, float light) {
aJsonObject *root,*data;
root = aJson.createObject();
aJson.addItemToObject(root, "data", data = aJson.createObject());
aJson.addNumberToObject(data, "temp", temp);
aJson.addNumberToObject(data, "hum", hum);
aJson.addNumberToObject(data, "press", press);
aJson.addNumberToObject (data, "wd", wd);
aJson.addNumberToObject(data, "ws", ws);
aJson.addNumberToObject(data, "light", light);
aJson.addItemToObject(root, "status", aJson.createItem(1));
// Serial.print("\"data\": { \"temp\": ");
// Serial.print(temp, 4);
// Serial.print(", \"hum\": ");
// Serial.print(hum);
// Serial.print(", \"press\": ");
// Serial.print(press,4);
// Serial.print(", \"wd\": ");
// Serial.print(wd);
// Serial.print(", \"ws\": ");
// Serial.print(ws);
// Serial.print(", \"light\": ");
// Serial.print(light);
// Serial.print("}");
aJson.print(root, &serial_stream);
Serial.print("\n");
}
You'll see that I'm using a library to help with the JSON. I tried this to fix the problem of scrambled data, but it didn't help much. My previous attempt is commented out below.
The libraries:
- MPL3115A2_Pressure - https://github.com/sparkfun/MPL3115A2_Breakout/raw/master/library/MPL3115A2_Pressure.zip
- aJSON - GitHub - interactive-matter/aJson: aJson is an Arduino library to enable JSON processing with Arduino. It easily enables you to decode, create, manipulate and encode JSON directly from and to data structures.
T