Last active
January 5, 2017 21:20
-
-
Save pofat/0cc9dee23d61467d8afeb3827a7202dd to your computer and use it in GitHub Desktop.
MQTT related example code of Arduino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SPI.h> | |
#include <Ethernet.h> | |
// Any MAC that won't conflice with any device in your network will do | |
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDe, 0x02 }; | |
EthernetClient client; | |
void setup() { | |
Serial.begin(9600); | |
// Wait until console is opened | |
while(!Serial); | |
if (Ethernet.begin(mac) == 0) { | |
Serial.println("Can not get IP address"); | |
for(;;) | |
; | |
} | |
Serial.print("IP address:"); | |
// How you retrieve local IP | |
Serial.println(Ethernet.localIP()); | |
} | |
void loop() { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Basic MQTT example | |
This sketch demonstrates the basic capabilities of the library. | |
It connects to an MQTT server then: | |
- publishes "hello world" to the topic "outTopic" | |
- subscribes to the topic "inTopic", printing out any messages | |
it receives. NB - it assumes the received payloads are strings not binary | |
It will reconnect to the server if the connection is lost using a blocking | |
reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to | |
achieve the same result without blocking the main loop. | |
*/ | |
#include <SPI.h> | |
#include <Ethernet.h> | |
#include <PubSubClient.h> | |
// Update these three with values suitable for your network. | |
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED }; | |
// IP of mqtt client (e.g. Arduino) | |
IPAddress ip(192, 168, 0, 111); | |
// IP of mqtt broker (e.g. Raspberry Pi) | |
IPAddress server(192, 168, 0, 100); | |
void callback(char* topic, byte* payload, unsigned int length) { | |
Serial.print("Message arrived ["); | |
Serial.print(topic); | |
Serial.print("] "); | |
for (int i=0;i<length;i++) { | |
Serial.print((char)payload[i]); | |
} | |
Serial.println(); | |
} | |
EthernetClient ethClient; | |
PubSubClient client(ethClient); | |
void reconnect() { | |
// Loop until we're reconnected | |
while (!client.connected()) { | |
Serial.print("Attempting MQTT connection..."); | |
// Attempt to connect | |
if (client.connect("arduinoClient")) { | |
Serial.println("connected"); | |
// Once connected, publish an announcement... | |
client.publish("outTopic","hello world"); | |
// ... and resubscribe | |
client.subscribe("inTopic"); | |
} else { | |
Serial.print("failed, rc="); | |
Serial.print(client.state()); | |
Serial.println(" try again in 5 seconds"); | |
// Wait 5 seconds before retrying | |
delay(5000); | |
} | |
} | |
} | |
void setup() | |
{ | |
Serial.begin(57600); | |
while(!Serial); | |
client.setServer(server, 1883); | |
client.setCallback(callback); | |
Ethernet.begin(mac, ip); | |
// Allow the hardware to sort itself out | |
delay(1500); | |
} | |
void loop() | |
{ | |
if (!client.connected()) { | |
reconnect(); | |
} | |
client.loop(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Basic MQTT example | |
This sketch demonstrates the basic capabilities of the library. | |
It connects to an MQTT server then: | |
- publishes "hello world" to the topic "outTopic" | |
- subscribes to the topic "inTopic", printing out any messages | |
it receives. NB - it assumes the received payloads are strings not binary | |
It will reconnect to the server if the connection is lost using a blocking | |
reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to | |
achieve the same result without blocking the main loop. | |
*/ | |
#include <SPI.h> | |
#include <Ethernet.h> | |
#include <PubSubClient.h> | |
#include <Wire.h> | |
#include "HTU21D.h" | |
HTU21D humidity; | |
// Update these with values suitable for your network. | |
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED }; | |
IPAddress ip(192, 168, 1, 109); | |
IPAddress server(192, 168, 1, 108); | |
int LED_PIN = 2; | |
void callback(char* topic, byte* payload, unsigned int length) { | |
char message_buff[100]; | |
Serial.println("Message arrived: topic: " + String(topic)); | |
Serial.println("Length: " + String(length,DEC)); | |
// create character buffer with ending null terminator (string) | |
for (int i = 0; i < length; i++) { | |
message_buff[i] = payload[i]; | |
} | |
message_buff[length] = '\0'; | |
String msgString = String(message_buff); | |
Serial.println("Payload: " + msgString); | |
if(msgString == "on") { | |
digitalWrite(LED_PIN, HIGH); | |
Serial.println("LED on"); | |
} else if(msgString == "off") { | |
digitalWrite(LED_PIN, LOW); | |
Serial.println("LED off"); | |
} else { | |
Serial.println("Do nothing"); | |
} | |
} | |
EthernetClient ethClient; | |
PubSubClient client(ethClient); | |
void reconnect() { | |
// Loop until we're reconnected | |
while (!client.connected()) { | |
Serial.print("Attempting MQTT connection..."); | |
// Attempt to connect | |
if (client.connect("arduinoClient")) { | |
Serial.println("connected"); | |
// Once connected, publish an announcement... | |
client.publish("outTopic","hello world"); | |
// ... and resubscribe | |
client.subscribe("inTopic"); | |
} else { | |
Serial.print("failed, rc="); | |
Serial.print(client.state()); | |
Serial.println(" try again in 5 seconds"); | |
// Wait 5 seconds before retrying | |
delay(5000); | |
} | |
} | |
} | |
void setup() | |
{ | |
Serial.begin(57600); | |
while(!Serial); | |
// start HTU21D | |
humidity.begin(); | |
// start mqtt client | |
client.setServer(server, 1883); | |
client.setCallback(callback); | |
Ethernet.begin(mac, ip); | |
// start LED | |
pinMode(2, OUTPUT); | |
// Allow the hardware to sort itself out | |
delay(1500); | |
} | |
void loop() | |
{ | |
if (!client.connected()) { | |
reconnect(); | |
} | |
client.loop(); | |
// read temperature | |
float temp = humidity.readTemperature(); | |
// read humidity | |
float humd = humidity.readHumidity(); | |
String result = "Temperature: " + String(temp, 1) + "; Humidity: " + String(humd, 1) + "%"; | |
// uncomment to try | |
// if(temp > 26) { | |
// client.publish("inTopic", "on"); | |
// } else { | |
// client.publish("inTopic", "off"); | |
// } | |
Serial.println(result); | |
client.publish("outTopic",string2char(result)); | |
delay(1000); | |
} | |
char* string2char(String command){ | |
if(command.length()!=0){ | |
char *p = const_cast<char*>(command.c_str()); | |
return p; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment