source: rtos_arduino/trunk/arduino_lib/libraries/pubsubclient-2.6/examples/mqtt_stream/mqtt_stream.ino@ 209

Last change on this file since 209 was 209, checked in by ertl-honda, 8 years ago

BlueMix用のフィアルを追加

File size: 1.2 KB
Line 
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.
18byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
19IPAddress ip(172, 16, 0, 100);
20IPAddress server(172, 16, 0, 2);
21
22SRAM sram(4, SRAM_1024);
23
24void 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
37EthernetClient ethClient;
38PubSubClient client(server, 1883, callback, ethClient, sram);
39
40void 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
54void loop()
55{
56 client.loop();
57}
Note: See TracBrowser for help on using the repository browser.