Added examples directory and mqtt_basic example
This commit is contained in:
parent
ef75340e6e
commit
ef565d8d98
37
examples/mqtt_basic/mqtt_basic.pde
Normal file
37
examples/mqtt_basic/mqtt_basic.pde
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
/*
|
||||||
|
Basic MQTT example
|
||||||
|
|
||||||
|
- connects to an MQTT server
|
||||||
|
- publishes "hello world" to the topic "outTopic"
|
||||||
|
- subscribes to the topic "inTopic"
|
||||||
|
*/
|
||||||
|
|
||||||
|
#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 };
|
||||||
|
byte server[] = { 172, 16, 0, 2 };
|
||||||
|
byte ip[] = { 172, 16, 0, 100 };
|
||||||
|
|
||||||
|
void callback(char* topic, byte* payload,int length) {
|
||||||
|
// handle message arrived
|
||||||
|
}
|
||||||
|
|
||||||
|
PubSubClient client(server, 1883, callback);
|
||||||
|
|
||||||
|
void setup()
|
||||||
|
{
|
||||||
|
Ethernet.begin(mac, ip);
|
||||||
|
if (client.connect("arduinoClient")) {
|
||||||
|
client.publish("outTopic","hello world");
|
||||||
|
client.subscribe("inTopic");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop()
|
||||||
|
{
|
||||||
|
client.loop();
|
||||||
|
}
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user