I use node daily to send actions to Arduino via usb or via bt, and it works great in both cases. I think your problem comes from sending emails. You should send a buffer instead of the ascii value for the letter:
myPort.write(Buffer([myValueToBeSent]));
for this, I think, it will be better for you with some "logical" interface, with data headers, number of actions, such things. It is not required for you, but it will make your code more reliable and easier in the future in the future.
Here is an example of how I do it. Node first:
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;
Then you call them like you:
everyone.now.MyBatchOfActions = 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])); }
Thus, it is easy to get Serial.read () data on Arduino: (Note that you need to define the header and data below)
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 } } } } }
Hope I understand and hope this helps! Hooray!
ladislas
source share