Andrew Lindsay / Mbed 2 deprecated IoTGateway_Basic

Dependencies:   NetServices FatFileSystem csv_parser mbed MQTTClient RF12B DNSResolver SDFileSystem

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers OutputMqtt.cpp Source File

OutputMqtt.cpp

00001 /** IoT Gateway Output definition for MQTT
00002  *
00003  * @author Andrew Lindsay
00004  *
00005  * @section LICENSE
00006  *
00007  * Copyright (c) 2012 Andrew Lindsay (andrew [at] thiseldo [dot] co [dot] uk)
00008  *
00009  * Permission is hereby granted, free of charge, to any person obtaining a copy
00010  * of this software and associated documentation files (the "Software"), to deal
00011  * in the Software without restriction, including without limitation the rights
00012  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00013  * copies of the Software, and to permit persons to whom the Software is
00014  * furnished to do so, subject to the following conditions:
00015 
00016  * The above copyright notice and this permission notice shall be included in
00017  * all copies or substantial portions of the Software.
00018  * 
00019  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00020  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00021  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00022  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00023  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00024  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00025  * THE SOFTWARE.
00026  * 
00027  * @section DESCRIPTION
00028  * 
00029  * 
00030  */
00031  
00032 #include "mbed.h"
00033 #include "OutputMqtt.h"
00034 
00035 // Constructor
00036 OutputMqtt::OutputMqtt() {
00037     sendCount = 0;
00038 }
00039 
00040 static void callback(char* topic, char* payload); /*Callback function prototype*/
00041 //MQTTClient mqtt(serverIpAddr, 1883, callback);
00042 
00043 
00044 static void callback(char* topic, char* payload)
00045 {
00046     printf("Topic: %s\r\n", topic);
00047     printf("Payload: %s\r\n\r\n", payload);
00048 }
00049 
00050 void OutputMqtt::initConnection( IpAddr *serverIpAddr, short port, char *username, char *password ) {
00051     this->serverIpAddr = serverIpAddr;
00052     this->port = port;
00053     this->username = username;
00054     this->password = password;
00055 }
00056 
00057 bool OutputMqtt::init( ) {
00058 //    printf("MQTT server ip is %d.%d.%d.%d\n",serverIpAddr[0],serverIpAddr[1],serverIpAddr[2],serverIpAddr[3]);
00059     mqtt.init(serverIpAddr, port, username, password, callback);
00060     if(!mqtt.connect("IoTGateway")){
00061         printf("\r\nConnect to server failed ..\r\n");
00062         mqtt.disconnect();
00063         return false;
00064     }
00065     return true;
00066 }
00067 
00068 // These are used to send
00069 void OutputMqtt::addReading(char *topic, char *unused, char *reading ) {
00070     sendCount++;
00071     mqtt.publish( topic, reading );
00072 }
00073 
00074 
00075 // send here does nothing
00076 int OutputMqtt::send( void ) {
00077     mqtt.live();
00078     return 0;
00079 }
00080 
00081 
00082 int OutputMqtt::getSendCount( void ) {
00083     return sendCount;
00084 }