1 | /*
|
---|
2 | Example of using a Stream object to store the message payload
|
---|
3 |
|
---|
4 | Uses SRAM library: https://github.com/ennui2342/arduino-sram
|
---|
5 | but could use any Stream based class such as SD
|
---|
6 |
|
---|
7 | - connects to an MQTT server
|
---|
8 | - publishes "hello world" to the topic "outTopic"
|
---|
9 | - subscribes to the topic "inTopic"
|
---|
10 | */
|
---|
11 |
|
---|
12 | #include <SPI.h>
|
---|
13 | #include <Ethernet.h>
|
---|
14 | #include <PubSubClient.h>
|
---|
15 | #include <SRAM.h>
|
---|
16 |
|
---|
17 | // Update these with values suitable for your network.
|
---|
18 | byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
|
---|
19 | IPAddress ip(172, 16, 0, 100);
|
---|
20 | IPAddress server(172, 16, 0, 2);
|
---|
21 |
|
---|
22 | SRAM sram(4, SRAM_1024);
|
---|
23 |
|
---|
24 | void callback(char* topic, byte* payload, unsigned int length) {
|
---|
25 | sram.seek(1);
|
---|
26 |
|
---|
27 | // do something with the message
|
---|
28 | for(uint8_t i=0; i<length; i++) {
|
---|
29 | Serial.write(sram.read());
|
---|
30 | }
|
---|
31 | Serial.println();
|
---|
32 |
|
---|
33 | // Reset position for the next message to be stored
|
---|
34 | sram.seek(1);
|
---|
35 | }
|
---|
36 |
|
---|
37 | EthernetClient ethClient;
|
---|
38 | PubSubClient client(server, 1883, callback, ethClient, sram);
|
---|
39 |
|
---|
40 | void setup()
|
---|
41 | {
|
---|
42 | Ethernet.begin(mac, ip);
|
---|
43 | if (client.connect("arduinoClient")) {
|
---|
44 | client.publish("outTopic","hello world");
|
---|
45 | client.subscribe("inTopic");
|
---|
46 | }
|
---|
47 |
|
---|
48 | sram.begin();
|
---|
49 | sram.seek(1);
|
---|
50 |
|
---|
51 | Serial.begin(9600);
|
---|
52 | }
|
---|
53 |
|
---|
54 | void loop()
|
---|
55 | {
|
---|
56 | client.loop();
|
---|
57 | }
|
---|