Created
January 14, 2016 21:55
-
-
Save bittailor/f97cbc529cc246d3bffe to your computer and use it in GitHub Desktop.
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 <IPStack.h> | |
#include <Countdown.h> | |
#include <MQTTClient.h> | |
#include "GPRSClient.h" | |
SoftwareSerial sSoftwareSerial(7,8); | |
GPRSClient sGprsClient(sSoftwareSerial,9); | |
IPStack sIpStack(sGprsClient); | |
MQTT::Client<IPStack, Countdown, 50, 1> sMqttClient = MQTT::Client<IPStack, Countdown, 50, 1>(sIpStack); | |
int sArrivedcount = 0; | |
MQTT::Message sMessage; | |
void messageArrived(MQTT::MessageData& md) | |
{ | |
MQTT::Message& message = md.message; | |
Serial.print(F("Message ")); | |
Serial.print(++sArrivedcount); | |
Serial.print(F(" arrived: qos ")); | |
Serial.print(message.qos); | |
Serial.print(F(", retained ")); | |
Serial.print(message.retained); | |
Serial.print(F(", dup ")); | |
Serial.print(message.dup); | |
Serial.print(F(", packetid ")); | |
Serial.println(message.id); | |
Serial.print(F("Payload ")); | |
Serial.println((char*)message.payload); | |
} | |
void connect() | |
{ | |
char hostname[] = "iot.eclipse.org"; | |
int port = 1883; | |
Serial.print(F("Connecting to ")); | |
Serial.print(hostname); | |
Serial.print(F(":")); | |
Serial.println(port); | |
int rc = sIpStack.connect(hostname, port); | |
if (rc != 1) | |
{ | |
Serial.print(F("rc from TCP connect is ")); | |
Serial.println(rc); | |
} | |
Serial.println(F("MQTT connecting")); | |
MQTTPacket_connectData data = MQTTPacket_connectData_initializer; | |
data.MQTTVersion = 3; | |
data.clientID.cstring = (char*)"bt-solar"; | |
data.willFlag = true; | |
data.will.topicName.cstring = (char*)"bt/solar/status"; | |
data.will.message.cstring = (char*)"offline"; | |
data.will.retained = true; | |
data.will.qos = MQTT::QOS1; | |
rc = sMqttClient.connect(data); | |
if (rc != 0) | |
{ | |
Serial.print(F("rc from MQTT connect is ")); | |
Serial.println(rc); | |
} | |
Serial.println(F("MQTT connected")); | |
char buf[20]; | |
strcpy(buf, "online"); | |
sMessage.qos = MQTT::QOS1; | |
sMessage.retained = true; | |
sMessage.dup = false; | |
sMessage.payload = (void*)buf; | |
sMessage.payloadlen = strlen(buf)+1; | |
sMqttClient.publish("bt/solar/status", sMessage); | |
rc = sMqttClient.subscribe("bt/solar/command", MQTT::QOS2, messageArrived); | |
if (rc != 0) | |
{ | |
Serial.print(F("rc from MQTT subscribe is ")); | |
Serial.println(rc); | |
} | |
Serial.println(F("MQTT subscribed")); | |
} | |
void setup() | |
{ | |
Serial.begin(115200); | |
Serial.println(F("** MQTT PAHO over GPRS example **")); | |
Serial.println(F("send any character to start ...")); | |
while(!Serial.available()){} | |
Serial.println(F(" ... starting")); | |
sSoftwareSerial.begin(9600); | |
sGprsClient.begin("1210","gprs.swisscom.ch"); | |
connect(); | |
} | |
void loop() | |
{ | |
if (!sMqttClient.isConnected()) { | |
connect(); | |
} | |
char buf[30]; | |
unsigned long uptime = millis(); | |
ultoa(uptime,buf,10); | |
sMessage.qos = MQTT::QOS0; | |
sMessage.retained = false; | |
sMessage.dup = false; | |
sMessage.payload = (void*)buf; | |
sMessage.payloadlen = strlen(buf)+1; | |
Serial.print(F("publish "));Serial.println(buf); | |
int rc = sMqttClient.publish("bt/solar/panel/A/current", sMessage); | |
sMqttClient.yield(10000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment