Messenger library

I find find it really helpful :slight_smile:

Never mind about the 64 character limit. I found the offending code after all of about 30 seconds search. Sometimes using your own eyes is perfectly sufficient!

#define MESSENGERBUFFERSIZE 64

changed to

#define MESSENGERBUFFERSIZE 128

duh!

Hi, just for your information.

I've made a description in how to send serial data from and to Flash with the Messenger library. The file can be used with the basic_communication sketch in the Messenger library examples folder.

http://www.kasperkamperman.com/blog/arduino-flash-communication-as3-messenger/.

Im new to arduino and loving it!

Hey thanks for posting this library ! Im having a problem though ..

Using the example checkstring I can turn the led on and off fine :slight_smile: but if i type some random letters ect it will confuse it and will stop it working totally the next time i type "on" ect.

I need this to ignore anything other than "on" or "off" ect ?

// This example demonstrates Messenger's checkString method
// It turns on the LED attached to pin 13 if it receives "on"
// It turns it off if it receives "off"


#include <Messenger.h>


// Instantiate Messenger object with the message function and the default separator 
// (the space character)
Messenger message = Messenger(); 


// Define messenger function
void messageCompleted() {
  // This loop will echo each element of the message separately
  while ( message.available() ) {
   
   
     
   
   
    if ( message.checkString("on") ) {
      digitalWrite(13,HIGH);
    } else if ( message.checkString("off") ) {
      digitalWrite(13,LOW);
    }
  }
  
  
}

void setup() {
  // Initiate Serial Communication
  Serial.begin(115200); 
  message.attach(messageCompleted);
  
  pinMode(13,OUTPUT);
  
}

void loop() {
  
  // The following line is the most effective way of 
  // feeding the serial data to Messenger
  while ( Serial.available() ) message.process( Serial.read() );


}// This example demonstrates Messenger's checkString method
// It turns on the LED attached to pin 13 if it receives "on"
// It turns it off if it receives "off"


#include <Messenger.h>


// Instantiate Messenger object with the message function and the default separator 
// (the space character)
Messenger message = Messenger(); 


// Define messenger function
void messageCompleted() {
  // This loop will echo each element of the message separately
  while ( message.available() ) {
   
   
     
   
   
    if ( message.checkString("on") ) {
      digitalWrite(13,HIGH);
    } else if ( message.checkString("off") ) {
      digitalWrite(13,LOW);
    }
  }
  
  
}

void setup() {
  // Initiate Serial Communication
  Serial.begin(115200); 
  message.attach(messageCompleted);
  
  pinMode(13,OUTPUT);
  
}

void loop() {
  
  // The following line is the most effective way of 
  // feeding the serial data to Messenger
  while ( Serial.available() ) message.process( Serial.read() );


}

This library came in handy for me and thought that I would share a nifty function addition (checkSubString).

In this project's case the serial port is used to send data and to receive poll and configuration settings commands.

For example;

Poll Commands (e.g. send a poll command to Arduino to receive data from a client computer over serial):
M0!
where:

  • M0 is message type 0

M1!
where:

  • M1 is message type 1

Configuration Settings:
SET01nn
where:

  • nn set the sample interval in seconds (01-60)
    .
    .
    .
    SET08nn
    where:
  • nn = 96 sets the serial port's baud-rate to 9600
  • nn = 38 sets the serial port's baud-rate to 38400
Messenger.h (Public)
uint8_t checkSubString(char *subString, char *string, uint8_t length);

Messenger.cpp
// alpha implementation, may be a more efficient method
// instead of calling strncpy
uint8_t Messenger::checkSubString(char *subString, char *string, uint8_t length) {
      if (next()) {
            if ( strcasestr(subString,current) == 0 ) {
                  // return remaining chars after subString
                  int nChars = strlen(current) - strlen(subString);
                  strncpy(string, current + strlen(subString), nChars);
                  string[nChars] = '\0';
                        if ( strlen(string) > length ) return 0;
                  dumped = 1;
                  return 1;
            } else {
                  return 0;
            }
      } 
}

Here is a usage example - simply add the following snippet example to the checkString example.

char buffer[32]; // global buffer


    } else if ( message.checkString("M0!") ) {
       // send message 0 data
    } else if ( message.checkSubString("SET01", buffer, 2) ) {
      Serial.print("Sample Interval: ");
      Serial.println(buffer);
     //
     // store sample interval...
    } else if ( message.checkSubString("SET08", buffer, 2) ) {
      Serial.print("Baud-Rate: ");
      Serial.println(buffer);
     //
     // store serial port baud-rate..
    } else {
      return;
    }

Enjoy!

Eric

Hey luke123

Take a look at my post. Simply add an "else" statement to return if no matches are found.

Cheers..

Eric

Update to my previous post for checkSubString.

uint8_t Messenger::checkSubString(char *subString, char *string, uint8_t length) {
      if (next()) {
            //if ( strcasestr(subString,current) == 0 ) {
            if ( strstr(current, subString) != NULL ) {
                  // return remaining chars after subString
                  int nChars = strlen(current) - strlen(subString);
                  strncpy(string, current + strlen(subString), nChars);
                  string[nChars] = '\0';
                  if ( strlen(string) > length ) return 0;
                  dumped = 1;
                  return 1;
            } else {
                  return 0;
            }
      } 
}

Cheers,

Eric

I'm having some trouble with bad commands. the correct commands for my project work great, but if I enter a command wrong it seems to lock up and the pin 13 led comes on. my code is still pretty simple and I don't see how it could cause this, and I don't see how the library could do it directly either. could it be an overflow someplace?

here's my code
http://download.growcontrol.com/arduino/usbmessenger.pde

Hi tof,

I can see a lot of potential for this library.
Thanks for your efforts.

in the examples you may consider changing:
#include <Messenger.h>
to
#include "Messenger.h"

to enable people to simply place the .h file in the sketch's directory

is there a proper way to handle command errors or a proper way to reset or clear?

I think I fixed the problem. it took me forever to trace down the actual root, and it turns out pretty simple.

in the example basic_communication, I change line 21:
while ( message.available() ) {
to this:
if(messenger.available()){

this seems to fix my problems. no more lock ups caused by an endless loop. the incoming serial data is sent to the messenger class one character at a time anyway, so it can never have 2 separate commands in buffer at the same time. so no need to use a "while" loop.

many many many ......millions of thianks.
this library have help me to solve a big trouble.

Dear all,

I have added a readDouble function inside the Messenger library, for my own needs. For this, I have added the following line
double readDouble();
in Messenger.h, right after the int readInt();
Then I added the following function in Messenger.cpp
double Messenger::readDouble() {

if (next()) {
dumped = 1;
return atof(current);
}
return 0;
}

The same thing could be done for floats.
Best regards,

I have a question. can I include the library in the zip I'm posting for my project? I'd like to package the .h and .cpp files with my code. I'd like to do this to make it easier for people to use my project, and in case there are any changes to the library that might break my code, so I can fix it before releasing a new package. is this ok to do?

I would also like to thank the author for this library - I have been able to implement a much more sophisticated command handling protocol to my project with this library. I was using a switch case before and it was murder.

Thanks!

Great stuff... to make it lil better for my needs I created a fork on github:

Dunno if the author is still around but you create yours there I'll dump mine and fork. Thank you.

Hi everyone.

Can anyone point me at a url, or other source, showing Messenger being used in a Max patch? I am new to Max and can't figure out what kind of message it is expecting at its input. Thanks!

ST

I added this line to the Messenger.cpp file so that I could use the serial port monitor to enter commands:

case 13: // CR
** case ';': // to enable serial port monitor use**
buffer[bufferIndex]=0;

It also allows you to enter multiple commands separated by a ';' on the same line.

You must end each command and each line with a ';' when using the Arduino serial port monitor.

It is NOT necessary to append a ';' if you are sending a CR (ie using a standard terminal emulator or from a Processing sketch).

Adding this line implies that your Messenger commands can't have ';' in them...