2

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?

  • My first thought would be that the port speed and settings change back to defaults when you close the IDEs Serial monitor? – TimboTinkerer May 1 '14 at 17:16
  • This really should go back to stackoverflow, as the problem is with not using the desktop serial configuration APIs. Basically, you need to set a variety of serial port settings in a compatible way - when you (somewhat dangerously) "share" the port you inherit those set by the serial monitor, until it sets them back on exit. You may be able to use a command line stty program to discover how the serial monitor is setting the port. – Chris Stratton May 1 '14 at 23:04
  • How are you opening the serial port when you communicate "through terminal" ? It's application dependant whether the serial port settings are restored to their original state when the port is closed. From your result it seems like the Arduino IDE is restoring them (which is best practise). – Kingsley Jun 30 '14 at 22:14
1

I had a similar problem trying to use the commands such as :

echo 1 > /dev/ttyACM0

and found that I needed privileges to perform serial communication in this way like:

sudo echo 1 > /dev/ttyACM0

You could also try setting the baud rate you are communicating at :

sudo echo 1 > /dev/ttyACM0 9600

GOOD LUCK!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy