1

I'm trying to control Arduino from NodeJS.

I have already tried Duino, I get that the device is ready, and the debugger shows that the commands were sent succssesfuly to the Arduino, but nothing happens.

I have also tried Johnny-Five, It shows that the device is connected (on COM8), But the on ready event is never fired.

Help please! Thanks..

1 Answer 1

3

I might be able to help you be you've got to be a little more specific about what you really want to do?

Do you want to read data? Do you want to remote control it?

EDIT: I also use Node to control an Arduino but I'm not using Duino nor Johnny-Five because the don't fit in my project.

Instead, I've made my own communication protocole between my computer and my robot.

On the Arduino, the code is simple. It checks if serial is available and if so, reads and stores the buffer. Using a switch or if/else I then choose the action I want my robot to execute (move forward, move backward, blink led, etc.)

The communication is made by sending bytes and not human readable actions. So the first thing you have to do is imagine a small interface between the two. Bytes are useful because on the Arduino side, you won't need any conversion and they work great with switch, whereas it's not the case with strings.

On the Arduino Side, you'll have something like that: (note that you need to declare DATA_HEADER somewhere)

void readCommands(){
    while(Serial.available() > 0){

        // Read first byte of stream.
        uint8_t numberOfActions;
        uint8_t recievedByte = Serial.read();

        // If first byte is equal to dataHeader, lets do
        if(recievedByte == DATA_HEADER){
            delay(10);

            // Get the number of actions to execute
            numberOfActions = Serial.read();

            delay(10);

            // Execute each actions
            for (uint8_t i = 0 ; i < numberOfActions ; i++){

                // Get action type
                actionType = Serial.read();

                if(actionType == 0x01){
                    // do you first action
                }
                else if(actionType == 0x02{
                    // do your second action
                }
                else if(actionType == 0x03){
                    // do your third action
                }
            }
        }
    }
}

On the node side, you'll have something like that: (check the serialport github for more informations)

var dataHeader = 0x0f, //beginning of the data stream, very useful if you intend to send a batch of actions
myFirstAction = 0x01,
mySecondAction = 0x02,
myThirdAction = 0x03;

sendCmdToArduino = function() {
    sp.write(Buffer([dataHeader]));

    sp.write(Buffer([0x03])); // this is the number of actions for the Arduino code

    sp.write(Buffer([myFirstAction]));
    sp.write(Buffer([mySecondAction]));
    sp.write(Buffer([myThirdAction]));
}

Hope it helps!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.