IoT (Internet of Things) recent become more and more popular. Although HTTP is the standard for Web pages, but between machines (Machine-to-Machine,M2M)requires a different mode of mass communication: request/answer before (Request/Response) models are no longer suitable, instead of publish/subscribe (Publish/Subscribe) mode. That’s the lightweight and extensible MQTT (Message Queuing TelemetryTransport).
This recipe will show you how to make the simplest example to run MQTT on arduino (Ethernet requirred)
Here are something you need :
Arduino or seeeduino
Arduino Ethernet
MQTT services
Arduino Pubsubclient
Reference examples of official:
#include <SPI.h> #include <Ethernet.h> #include <PubSubClient.h> byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED }; byte server[] = { byte server[] = { 192, 168, 168, 78 }; // MQTT服务地址 byte ip[] = { 192, 168, 168, 250 }; // 设备IP void callback(char* topic, byte* payload, unsigned int length) { // handle message arrived } EthernetClient ethClient; PubSubClient client(server, 1883, callback, ethClient); void setup() { Ethernet.begin(mac, ip); if (client.connect("arduinoClient")) { client.publish( client.publish("outTopic","hello world"); client.subscribe( client.subscribe("inTopic"); } } } } void loop() { client.loop(); }
trouble shooting
Service error like these:
{ [Error: read ECONNRESET] code: 'ECONNRESET', errno: 'ECONNRESET', syscall: 'read' } { [Error: read ECONNRESET] code: 'ECONNRESET', errno: 'ECONNRESET', syscall: 'read' }
Because it is strictly restricted as incoming JSON form
Then I try this
client.publish("outTopic","{'hello': 'true'}"); http://www.seeed.cc/MQTT-Arduino-p-1756.html
Leave a Reply
You must be logged in to post a comment.