Sorry in advance for any confusion, this is my first project involving arduino and raspberry Pi.
Im currently working on a project called weatherStation where I'm recieving multiple data from my arduino which sends the data in JSON format to the raspberry pi. The goal is to build an application that can start up on raspberry PI in kiosk mode and then get the data from the arduino. The problem we have is that when we reboot the PI, our whole string gets cut short somehow. When we start up the program again, it doesnt understand the JSON format because obviously, half the string is missing and the program fails.
Our goal is to recieve the data as such
{"windSpeed": "0.00", "windDir": "NE", "windFloat": "45.00", "waterAmount": "0.00", "temperatureValue" : "0.00", "pressureValue" : "0.00", "humidityValue" : "0.00", "gasValue" : "0.00"}
Sometimes when we start the program, this is what we get and then our program fails because it does not understand the JSON format. Sometimes it does work, but its like 2,5 out of 10 times
{"ue" : "0.00", "pressureValue" : "0.00", "humidityValue" : "0.00", "gasValue" : "0.00"}
, "waterAmount": "0.00", "temperatureValue" : "0.00", "pressureValue" : "0.00", "humidityValue" : "0.00", "gasValue" : "0.00"}
{"windSpeed": "0.00", "windDir": "NE", "windFloat": "45.00", "waterAmount": "0.00", "temperatureValue" : "0.00", "pressureValue" : "0.00", "humidityValue" : "0.00", "gasValue" : "0.00"}
{"windSpeed": "0.00", "windDir": "NE", "windFloat": "45.00", "waterAmount": "0.00", "temperatureValue" : "0.00", "pressureValue" : "0.00", "humidityValue" : "0.00", "gasValue" : "0.00"}
This is the code we have in the arduino
// Instantiere globale variabler
int windDirPin = A0;
int waterPin = 3;
int windSpePin = 9;
long unsigned int lastActivationWater;
long unsigned int lastActivationWind;
int debounceTime = 15;
double temperatureValue = 0;
double pressureValue = 0;
double humidityValue = 0;
double gasValue = 0;
float waterAmaunt;
int lastWaterValue = 1;
int lastWindSpeValue;
float windCounter;
double windSpeed;
long _previousMillis = 0;
long previousMillis = 0;
long interval = 1000;
void setup() {
// put your setup code here, to run once:
pinMode (windDirPin, INPUT);
pinMode (waterPin, INPUT);
pinMode (windSpePin, INPUT);
// Instantiere serial port med baud rate på 9600
Serial.begin(9600);
}
String windDirection() {
// Instantiere lokale variabler
int windDirValue = analogRead(windDirPin);
String windDir;
float windFloat;
if (windDirValue> 940) windDir = "E", windFloat = 90;
else if (windDirValue > 880) windDir = "SE", windFloat = 135;
else if (windDirValue > 820) windDir = "ESE", windFloat = 112.5;
else if (windDirValue > 780) windDir = "S", windFloat = 180;
else if (windDirValue > 700) windDir = "SSE", windFloat = 157.5;
else if (windDirValue > 630) windDir = "NE", windFloat = 45;
else if (windDirValue > 590) windDir = "ENE", windFloat = 67.5;
else if (windDirValue > 450) windDir = "SW", windFloat = 225;
else if (windDirValue > 400) windDir = "SSW", windFloat = 202.5;
else if (windDirValue > 280) windDir = "N", windFloat = 0;
else if (windDirValue > 240) windDir = "NNE", windFloat = 22.5;
else if (windDirValue > 180) windDir = "NW", windFloat = 315;
else if (windDirValue > 120) windDir = "NNW", windFloat = 337.5;
else if (windDirValue > 88) windDir = "W", windFloat = 270;
else if (windDirValue > 75) windDir = "WSW", windFloat = 247.5;
else windDir = "WNW", windFloat = 112.5;
return (String) windDir + "\", \"windFloat\": \"" + windFloat;
}
float waterCounter() {
// Instantiere lokale variabler
int waterValue = digitalRead(waterPin);
if((millis() - lastActivationWater) > debounceTime) //if the time between the last buttonChange is greater than the debounceTime
{
lastActivationWater = millis(); //update lastPress
if(waterValue == 0 && lastWaterValue == 1) //if button is pressed and was released last change
{
waterAmaunt += 0.2794;
lastWaterValue = 0; //record the lastButtonState
}
if(waterValue == 1 && lastWaterValue == 0) //if button is not pressed, and was pressed last change
{
lastWaterValue = 1; //record the lastButtonState
}
}
return (float) waterAmaunt;
}
double windSpeedCalc() {
// Instantiere lokale variabler
int windSpeValue = digitalRead(windSpePin);
unsigned long currentMillis = millis();
if((millis() - lastActivationWind) > debounceTime) // Vis tiden mellem siste aktivation er længere in debounceTime
{
lastActivationWind = millis(); //updatere lastActivation
if(windSpeValue == 1 && lastWindSpeValue == 1) //if button is pressed and was released last change
{
windCounter += 1;
lastWindSpeValue = 0; //record the lastButtonState
}
if(windSpeValue == 0 && lastWindSpeValue == 0) //if button is not pressed, and was pressed last change
{
lastWindSpeValue = 1; //record the lastButtonState
}
}
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
windSpeed = windCounter * 0.6666672;
windCounter = 0;
}
return (double) windSpeed;
}
void loop() {
// put your main code here, to run repeatedly:
double windSpeed = windSpeedCalc();
String win = String(windSpeed, 2);
String windDi = windDirection();
float water = waterCounter();
unsigned long _delay = millis();
if (_delay - _previousMillis > 500) {
_previousMillis = _delay;
String output = "{\"windSpeed\": \"" + win + "\", \"windDir\": \"" + windDi + "\", \"waterAmount\": \""
+ water + "\", \"temperatureValue\" : \"" + temperatureValue + "\", \"pressureValue\" : \"" + pressureValue +
"\", \"humidityValue\" : \"" + humidityValue + "\", \"gasValue\" : \"" + gasValue + "\"}";
Serial.println(output);
}
}
This is the error we have in Raspberry PI:
(node:29598) UnhandledPromiseRejectionWarning: SyntaxError: Unexpected token : in JSON at position 11
at JSON.parse (<anonymous>)
at ReadLineParser.<anonymous> (/home/pi/Desktop/test/app.js:54:24)
at ReadLineParser.emit (events.js:400:28)
at addChunk (internal/streams/readable.js:293:12)
at readableAddChunk (internal/streams/readable.js:263:11)
at ReadLineParser.Readable.push (internal/streams/readable.js:206:10)
at ReadLineParser.Transform.push (internal/streams/transform.js:166:32)
at ReadLineParser._transform (/home/pi/Desktop/test/node_modules/@serialport/parser-delimiter/lib/index.js:35:12)
at ReadLineParser.Transform._read (internal/streams/transform.js:205:10)
at ReadLineParser.Transform._write (internal/streams/transform.js:193:12)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:29598) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:29598) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
To be clear, the program does work! But only in 2-3 times out of 10.
Thanks in advance!
Andreas.