Mebm  1.0.0
 All Classes Files Functions Variables Macros Pages
Public Member Functions | Static Public Member Functions | List of all members
MebmClass Class Reference

A node for sending and receiving MEBM protocol messages. More...

#include <Mebm.h>

Public Member Functions

 MebmClass ()
 Create a new MebmClass object.
 
 ~MebmClass ()
 Destructor for a MebmClass object.
 
void begin (const char *name, int numResponders=0, bool defaultResponder=false)
 Configure the MebmClass object.
 
int sendToIP (const IPAddress &toIP, const char *messageType, const char *data)
 Send a MEBM message to a specific IP address.
 
int sendToIP (const IPAddress &toIP, const char *messageType, int data)
 
int sendToIP (const IPAddress &toIP, const char *messageType, float data)
 
void addResponder (const char *msgFrom, const char *messageType, void(*callBack)(MebmClass &, const t_mebmMessage *))
 Add a resonder for incoming messages.
 
int listen ()
 Check for incoming messages.
 
const char * id ()
 Get the Node ID string for this node.
 

Static Public Member Functions

static void _mebmReplyWithID (MebmClass &node, const t_mebmMessage *message)
 This is a built-in responder callback which sends a Mebm's ID back to the sender of a message (a reply to WhoDat messages)
 
static void ip2str (const IPAddress &ip, char *str)
 Convert an IPAddress object to a string IP address.
 
static IPAddress str2ip (const char *str)
 Convert a string IP address to an IPAddress object.
 

Detailed Description

A node for sending and receiving MEBM protocol messages.

A MebmClass object has an ID, can send and receive messages in the super-simple MEBM protocol. A MEBM protocol message has three parts:

  1. FromNode ID (nodes are expected to be honest - there no spoofing protection)
  2. MessageType (arbitrary string with maximum length MEBM_MSGTYP_MAX_SZ)
  3. MessageData (arbitrary string with maximum length MEBM_MSGDAT_MAX_SZ)

Messages are sent to listening nodes, which may be other arduinos, or python scripts like the central control node script.

A MebmClass object can have a server associated with it, which is where it sends outbound messages to by default. Typically this will be the central control node.

A MebmClass object can also listen for and react to incoming messages from other nodes. To do this, responders are added. A responder is a FromNode pattern (* or a fixed name), a message type pattern (* or a fixed string), and a callback function which will be triggered when an incoming message's FromNode and MessageType match the specified pattern.

In the style of the Arduino Ethernet and Serial libraries, we also create an instance of MebmClass called Mebm which you can access from within your code.

Examples:
mebm_tut_2.ino, and mebm_tut_3.ino.

Member Function Documentation

void MebmClass::_mebmReplyWithID ( MebmClass node,
const t_mebmMessage message 
)
static

This is a built-in responder callback which sends a Mebm's ID back to the sender of a message (a reply to WhoDat messages)

Parameters
nodea reference to the node which is doing the responding. Necessary for this static function.
messagea pointer to the incoming WhoDat message which prompted the response. The IP address is expected to be string encoded in the payoad of the incoming message.
void MebmClass::addResponder ( const char *  msgFrom,
const char *  messageType,
void(*)(MebmClass &, const t_mebmMessage *)  callBack 
)

Add a resonder for incoming messages.

Responders are callback function which are called when an incoming message matches the source node and/or the message type for the responder.

Parameters
msgFromOnly call callBack when the source node matches msgFromID in the incoming message (use * to match any source node).
messageTypeOnly call callBack when the messageType matches the message type in the inoming message (use * to match any message type).
callBackThe function to be called when a matching message is received, The data part of the message will be used as the parameter to the callBack (it should accept a char* argument). For example: node.addResponder("*", "*", anyMessage); will add a responder which is triggered for any incoming message (matches all source nodes and message types) and calls anyMessage()

Another example: node.addResponder("*", "SwitchLights", lightSwitch); will add a responder which works when and node (*) sends a "SwitchLights" message type, calling lightSwitch() in this case.

Examples:
mebm_tut_2.ino, and mebm_tut_3.ino.
void MebmClass::begin ( const char *  name,
int  numResponders = 0,
bool  defaultResponder = false 
)

Configure the MebmClass object.

Parameters
nameuse this name in outgoing messages to identify this MebmClass as the source of the message
numRespondersallocate enough memory to have up to this many responders
defaultResponderif true, add a responder for WhoDat messages (kindof like ping), note that this will take up one of the slots from the numResponders parameter, so if numResponders is 0 having defaultResponder=true will do nothing.
Examples:
mebm_tut_1.ino, mebm_tut_2.ino, and mebm_tut_3.ino.
void MebmClass::ip2str ( const IPAddress &  ip,
char *  str 
)
static

Convert an IPAddress object to a string IP address.

Given an Arduino Ethernet library IPAddress object, convert it to a string of the form "aaa.bbb.ccc.ddd"

Parameters
ipthe IP address to convert to a string
stra char* with at least 16 characters allocated which will be written to
int MebmClass::listen ( )

Check for incoming messages.

This should be called frequently from the loop() function to catch incoming messages Returns -1 if there was no incoming message to process, else how many handlers matched the message.

Examples:
mebm_tut_2.ino, and mebm_tut_3.ino.
int MebmClass::sendToIP ( const IPAddress &  toIP,
const char *  messageType,
const char *  data 
)

Send a MEBM message to a specific IP address.

Parameters
toIPthe IP address where this message should be sent.
messageTypeset the message type in the Mebm protocol message. Note that only MEBM_MSGTYP_MAX_SZ characters are used - anything else is ignored
datathe data payload to send. Note that only MEBM_MSGDAT_MAX_SZ characters are used - anything else is ignored
Returns
0 on successful send, non-0 otherwise.
Examples:
mebm_tut_1.ino, and mebm_tut_3.ino.
IPAddress MebmClass::str2ip ( const char *  str)
static

Convert a string IP address to an IPAddress object.

Given a string of the form "aaa.bbb.ccc.ddd" representing an IP address, convert it into An Arduino Ethernet Library IPAddress object.

Parameters
stra pointer to a string containing a representation of an IP address.
Returns
an IPAddress object containing the address represented by str.

The documentation for this class was generated from the following files: