MQTT Example
MQTT Example
h>
#include <PubSubClient.h>
#include <Ethernet.h>
/*
* LightSensorMqttDemo
*
* A simple m2m.io platform demo for Arduino.
*/
#define MQTT_SERVER "q.m2m.io"
// MAC Address of Arduino Ethernet Sheild (on sticker on shield)
byte MAC_ADDRESS[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x31, 0xB8 };
PubSubClient client;
// Pin 9 is the LED output pin
int ledPin = 9;
// Analog 0 is the input pin
int lightPinIn = 0;
// defines and variable for sensor/control mode
#define MODE_OFF
0 // not sensing light, LED off
#define MODE_ON
1 // not sensing light, LED on
#define MODE_SENSE 2 // sensing light, LED controlled by software
int senseMode = 0;
unsigned long time;
char message_buff[100];
void setup()
{
// initialize the digital pin as an output.
pinMode(ledPin, OUTPUT);
// init serial link for debugging
Serial.begin(9600);
if (Ethernet.begin(MAC_ADDRESS) == 0)
{
Serial.println("Failed to configure Ethernet using DHCP");
return;
}
client = PubSubClient(MQTT_SERVER, 1883, callback);
}
void loop()
{
if (!client.connected())
{
// clientID, username, MD5 encoded password
client.connect("arduino-mqtt", "john@m2m.io", "00000000000000000000000000000");
client.publish("io.m2m/arduino/lightsensor", "I'm alive!");
client.subscribe("io.m2m/arduino/lightsensor");
}
switch (senseMode) {
case MODE_OFF:
// light should be off
digitalWrite(ledPin, LOW);
break;
case MODE_ON:
// light should be on
digitalWrite(ledPin, HIGH);
break;
case MODE_SENSE:
// light is adaptive to light sensor
// read from light sensor (photocell)
int lightRead = analogRead(lightPinIn);
//
//
//
//
if
/*
Example of using a Stream object to store the message payload
Uses SRAM library: https://github.com/ennui2342/arduino-sram
but could use any Stream based class such as SD
- connects to an MQTT server
- publishes "hello world" to the topic "outTopic"
- subscribes to the topic "inTopic"
*/
#include
#include
#include
#include
<SPI.h>
<Ethernet.h>
<PubSubClient.h>
<SRAM.h>
/*
Basic MQTT example
This sketch demonstrates the basic
capabilities of the library.
It connects to an MQTT server then:
- publishes "hello world" to the topic
"outTopic"
- subscribes to the topic "inTopic", printing
out any messages
it receives. NB - it assumes the received
payloads are strings not binary
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
// Update these with values suitable for your network.
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
IPAddress ip(172, 16, 0, 100);
IPAddress server(172, 16, 0, 2);
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i=0;i<length;i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}
EthernetClient ethClient;
PubSubClient client(ethClient);
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT
connection...");
// Attempt to connect
if (client.connect("arduinoClient")) {
Serial.println("connected");
// Once connected, publish an
announcement...
client.publish("outTopic","hello world");
// ... and resubscribe
client.subscribe("inTopic");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup()
{
Serial.begin(57600);
client.setServer(server, 1883);
client.setCallback(callback);
Ethernet.begin(mac, ip);
// Allow the hardware to sort itself out
delay(1500);
}
void loop()
{
if (!client.connected()) {
reconnect();
}
client.loop();
}
/*
Publishing in the callback
- connects to an MQTT server
- subscribes to the topic "inTopic"
- when a message is received, republishes it to "outTopic"
This example shows how to publish messages within the
callback function. The callback function header needs to
be declared before the PubSubClient constructor and the
actual callback defined afterwards.
This ensures the client reference in the callback function
is valid.
*/
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
// Update these with values suitable for your network.
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
IPAddress ip(172, 16, 0, 100);
IPAddress server(172, 16, 0, 2);
// Callback function header
void callback(char* topic, byte* payload, unsigned int length);
EthernetClient ethClient;
PubSubClient client(server, 1883, callback, ethClient);
// Callback function
void callback(char* topic, byte* payload, unsigned int length) {
// In order to republish this payload, a copy must be made
// as the orignal payload buffer will be overwritten whilst
// constructing the PUBLISH packet.
// Allocate the correct amount of memory for the payload copy
byte* p = (byte*)malloc(length);
// Copy the payload to the new buffer
memcpy(p,payload,length);
client.publish("outTopic", p, length);
// Free the memory
free(p);
}
void setup()
{
Ethernet.begin(mac, ip);
if (client.connect("arduinoClient")) {
client.publish("outTopic","hello world");
client.subscribe("inTopic");
}
}
void loop()
{
client.loop();
}