From 1113695f4a2fc263c55462a5e70400dceaa3f90c Mon Sep 17 00:00:00 2001 From: Mark Cheverton Date: Mon, 20 Jan 2014 22:16:15 +0000 Subject: [PATCH] Example of using Stream storage for messages --- .../examples/mqtt_stream/mqtt_stream.ino | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 PubSubClient/examples/mqtt_stream/mqtt_stream.ino diff --git a/PubSubClient/examples/mqtt_stream/mqtt_stream.ino b/PubSubClient/examples/mqtt_stream/mqtt_stream.ino new file mode 100644 index 0000000..bf6df7c --- /dev/null +++ b/PubSubClient/examples/mqtt_stream/mqtt_stream.ino @@ -0,0 +1,53 @@ +/* + 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 + +// Update these with values suitable for your network. +byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED }; +byte server[] = { 172, 16, 0, 2 }; +byte ip[] = { 172, 16, 0, 100 }; + +SRAM sram(SRAM_CS_PIN, SRAM_1024); + +void callback(char* topic, byte* payload, unsigned int length) { + sram.seek(1); + + // do something with the message + myfunctionthattakesastreamargument(&sram); + + // Reset position for the next message to be stored + sram.seek(1); +} + +EthernetClient ethClient; +PubSubClient client(server, 1883, callback, ethClient); + +void setup() +{ + Ethernet.begin(mac, ip); + if (client.connect("arduinoClient")) { + client.publish("outTopic","hello world"); + client.subscribe("inTopic"); + } + + sram.begin(); + sram.seek(1); +} + +void loop() +{ + client.loop(); +} +