Mebm  1.0.0
 All Classes Files Functions Variables Macros Pages
mebm_tut_2.ino

Tutorial number 2: Receiving messages using Mebm.

Again we are going to imagine our Arduino in a kitchen. This time, we imagine it being hooked up to the cooker somehow, with the ability to turn it on and off by calling turnOvenOn() and turnOvenOff(). We also have some mechanism for changing the temperature of the oven by calling setOvenTemperature().

In this example we see that the data part of the message is entirely arbitrary, so long as it fits into the 48 bytes allocated. For setting the oven temperature we expect an integer encoded as a string which we extract using atoi().

Messages to control the oven can come from anywhere on the network - for example a smartphone app which connects to the house web server.

#include <SPI.h>
#include <Ethernet.h>
#include <Mebm.h>
#include <stdlib.h>
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x59, 0x20 };
IPAddress thisNode(192, 168, 1, 101);
int ovenTemperature = 200;
void setup() {
Serial.begin(9600);
Ethernet.begin(mac, thisNode);
Mebm.begin( "kitchen", 3 );
Mebm.addResponder("*", "turnOn", turnOvenOn);
Mebm.addResponder("*", "turnOff", turnOvenOff);
Mebm.addResponder("*", "setOvenTemp", setOvenTemperature);
Serial.println("setup() done");
}
void loop() {
delay(100);
Mebm.listen();
}
void turnOvenOn(MebmClass &node, const t_mebmMessage *message)
{
Serial.print("Click! The oven is now on at ");
Serial.print(ovenTemperature);
Serial.println(" C");
}
void turnOvenOff(MebmClass &node, const t_mebmMessage *message)
{
Serial.println("Click! The oven is now off");
}
void setOvenTemperature(MebmClass &node, const t_mebmMessage *message)
{
ovenTemperature = atoi(message->msgData);
Serial.print("The oven temperature is now ");
Serial.print(ovenTemperature);
Serial.println(" C");
}