I'm trying to communicate with the Arduino through the serial port to fade the LED. There's the code running on my Arduino:
#include <aJSON.h>
aJsonStream serial_stream(&Serial);
const int localID = 1;
int intensity = 100; // fadeTo value(0% - 100%)
int currentIntensity = 0; // how bright the LED actually is (0-100%)
int time = 0; // milliseconds
int color[] = {0, 255, 0};
int pinRed = 11;
int pinGreen = 10;
int pinBlue = 9;
int pinPower = 8;
void fadeTo() {
if(intensity != currentIntensity) {
if(intensity > currentIntensity)
currentIntensity+=1;
else
currentIntensity-=1;
analogWrite(pinRed,255-currentIntensity);
analogWrite(pinGreen,255-currentIntensity);
analogWrite(pinBlue,255-currentIntensity);
}
}
aJsonObject *createMsg()
{
aJsonObject *orbData, *colorData;
orbData = aJson.createObject();
aJson.addNumberToObject(orbData, "id", (int)localID);
aJson.addNumberToObject(orbData, "currentIntensity", (int)currentIntensity);
//aJson.addNumberToObject(orbData, "time", (int)time);
colorData = aJson.createIntArray(color, 3);
aJson.addItemToObject(orbData, "color", colorData);
return orbData;
}
// echo {\"id\":1, \"intensity\":150, \"time\":10} >> /dev/ttyACM0 in terminal
// {"id":1, "intensity":150, "time":10} in Serial Monitor
void processMsg(aJsonObject *msg)
{
//Serial.println("Data succefully received.");
Serial.println("received message: ");
aJson.print(msg, &serial_stream);
aJsonObject *idData = aJson.getObjectItem(msg, "id");
if(idData != NULL)
{
Serial.println("ID data succefully received.");
if(localID == (idData->valueint)) {
aJsonObject *intensityData = aJson.getObjectItem(msg, "intensity");
if(intensityData != NULL) {
if(intensityData->type != aJson_Int) {
Serial.println("Invalid data type for Intensity");
return;
}
if(intensityData->valueint == currentIntensity) {
Serial.println("The received intensity value is already the current LED intensity");
return;
}
else {
Serial.println("Intensity data succefully received");
intensity = intensityData->valueint;
}
}
else {
Serial.println("No intensity value received.");
return;
}
aJsonObject *timeData = aJson.getObjectItem(msg, "time");
if(timeData != NULL) {
if(timeData->type != aJson_Int) {
Serial.println("Invalid data type for time");
return;
}
else {
Serial.println("Time data succefully received");
time = (timeData->valueint);
}
}
}
else {
Serial.println("The received ID is not mine.");
return;
}
}
else {
Serial.println("No ID data received.");
return;
}
Serial.println("received message: ");
aJson.print(msg, &serial_stream);
}
void setup()
{
Serial.begin(9600);
pinMode(pinRed,OUTPUT);
pinMode(pinGreen,OUTPUT);
pinMode(pinBlue,OUTPUT);
pinMode(pinPower,OUTPUT);
digitalWrite(pinPower,HIGH);
digitalWrite(pinRed,HIGH);
digitalWrite(pinGreen,HIGH);
digitalWrite(pinBlue,HIGH);
}
void loop()
{
aJsonObject *orbData = createMsg();
Serial.println("current status: ");
aJson.print(orbData, &serial_stream);
Serial.println();
aJson.deleteItem(orbData);
delay(100);
fadeTo();
}
void serialEvent()
{
if(serial_stream.available())
{
// first, skip any accidental whitespace like newlines
serial_stream.skip();
}
if(serial_stream.available())
{
aJsonObject *msg = aJson.parse(&serial_stream);
processMsg(msg);
aJson.deleteItem(msg);
}
}
The problem is, I can open my Serial Monitor in the Arduino IDE and send json data to the Arduino and I see live the changes through the output and the LED on the board. I can also send data through terminal to /dev/ttyACM0 while running Serial Monitor in the IDE and it works fine too. But as soon as I close the Serial Monitor and send data to the device through terminal, it breaks down. I can then open Serial Monitor and every time I get the ERROR from json parser --> ("No ID data received.").
Has anyone any idea why this is happening?
stty
program to discover how the serial monitor is setting the port. – Chris Stratton May 1 '14 at 23:04