mbed based IoT Gateway More details http://blog.thiseldo.co.uk/wp-filez/IoTGateway.pdf

Dependencies:   NetServices FatFileSystem csv_parser mbed MQTTClient RF12B DNSResolver SDFileSystem

Committer:
SomeRandomBloke
Date:
Wed May 09 20:29:30 2012 +0000
Revision:
5:0dbc27a7af55
Parent:
2:27714c8c9c0a
Reduced debug output

Who changed what in which revision?

UserRevisionLine numberNew contents of line
SomeRandomBloke 0:a29a0225f203 1 /** IoT Gateway Output definition for MQTT
SomeRandomBloke 0:a29a0225f203 2 *
SomeRandomBloke 0:a29a0225f203 3 * @author Andrew Lindsay
SomeRandomBloke 0:a29a0225f203 4 *
SomeRandomBloke 0:a29a0225f203 5 * @section LICENSE
SomeRandomBloke 0:a29a0225f203 6 *
SomeRandomBloke 0:a29a0225f203 7 * Copyright (c) 2012 Andrew Lindsay (andrew [at] thiseldo [dot] co [dot] uk)
SomeRandomBloke 0:a29a0225f203 8 *
SomeRandomBloke 0:a29a0225f203 9 * Permission is hereby granted, free of charge, to any person obtaining a copy
SomeRandomBloke 0:a29a0225f203 10 * of this software and associated documentation files (the "Software"), to deal
SomeRandomBloke 0:a29a0225f203 11 * in the Software without restriction, including without limitation the rights
SomeRandomBloke 0:a29a0225f203 12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
SomeRandomBloke 0:a29a0225f203 13 * copies of the Software, and to permit persons to whom the Software is
SomeRandomBloke 0:a29a0225f203 14 * furnished to do so, subject to the following conditions:
SomeRandomBloke 0:a29a0225f203 15
SomeRandomBloke 0:a29a0225f203 16 * The above copyright notice and this permission notice shall be included in
SomeRandomBloke 0:a29a0225f203 17 * all copies or substantial portions of the Software.
SomeRandomBloke 0:a29a0225f203 18 *
SomeRandomBloke 0:a29a0225f203 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
SomeRandomBloke 0:a29a0225f203 20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
SomeRandomBloke 0:a29a0225f203 21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
SomeRandomBloke 0:a29a0225f203 22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
SomeRandomBloke 0:a29a0225f203 23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
SomeRandomBloke 0:a29a0225f203 24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
SomeRandomBloke 0:a29a0225f203 25 * THE SOFTWARE.
SomeRandomBloke 0:a29a0225f203 26 *
SomeRandomBloke 0:a29a0225f203 27 * @section DESCRIPTION
SomeRandomBloke 0:a29a0225f203 28 *
SomeRandomBloke 0:a29a0225f203 29 *
SomeRandomBloke 0:a29a0225f203 30 */
SomeRandomBloke 0:a29a0225f203 31
SomeRandomBloke 0:a29a0225f203 32 #include "mbed.h"
SomeRandomBloke 0:a29a0225f203 33 #include "OutputMqtt.h"
SomeRandomBloke 0:a29a0225f203 34
SomeRandomBloke 0:a29a0225f203 35 // Constructor
SomeRandomBloke 0:a29a0225f203 36 OutputMqtt::OutputMqtt() {
SomeRandomBloke 0:a29a0225f203 37 sendCount = 0;
SomeRandomBloke 0:a29a0225f203 38 }
SomeRandomBloke 0:a29a0225f203 39
SomeRandomBloke 2:27714c8c9c0a 40 static void callback(char* topic, char* payload); /*Callback function prototype*/
SomeRandomBloke 0:a29a0225f203 41 //MQTTClient mqtt(serverIpAddr, 1883, callback);
SomeRandomBloke 0:a29a0225f203 42
SomeRandomBloke 0:a29a0225f203 43
SomeRandomBloke 2:27714c8c9c0a 44 static void callback(char* topic, char* payload)
SomeRandomBloke 0:a29a0225f203 45 {
SomeRandomBloke 0:a29a0225f203 46 printf("Topic: %s\r\n", topic);
SomeRandomBloke 0:a29a0225f203 47 printf("Payload: %s\r\n\r\n", payload);
SomeRandomBloke 0:a29a0225f203 48 }
SomeRandomBloke 0:a29a0225f203 49
SomeRandomBloke 0:a29a0225f203 50 void OutputMqtt::initConnection( IpAddr *serverIpAddr, short port, char *username, char *password ) {
SomeRandomBloke 0:a29a0225f203 51 this->serverIpAddr = serverIpAddr;
SomeRandomBloke 0:a29a0225f203 52 this->port = port;
SomeRandomBloke 0:a29a0225f203 53 this->username = username;
SomeRandomBloke 0:a29a0225f203 54 this->password = password;
SomeRandomBloke 0:a29a0225f203 55 }
SomeRandomBloke 0:a29a0225f203 56
SomeRandomBloke 2:27714c8c9c0a 57 bool OutputMqtt::init( ) {
SomeRandomBloke 0:a29a0225f203 58 // printf("MQTT server ip is %d.%d.%d.%d\n",serverIpAddr[0],serverIpAddr[1],serverIpAddr[2],serverIpAddr[3]);
SomeRandomBloke 2:27714c8c9c0a 59 mqtt.init(serverIpAddr, port, username, password, callback);
SomeRandomBloke 0:a29a0225f203 60 if(!mqtt.connect("IoTGateway")){
SomeRandomBloke 0:a29a0225f203 61 printf("\r\nConnect to server failed ..\r\n");
SomeRandomBloke 2:27714c8c9c0a 62 mqtt.disconnect();
SomeRandomBloke 2:27714c8c9c0a 63 return false;
SomeRandomBloke 0:a29a0225f203 64 }
SomeRandomBloke 2:27714c8c9c0a 65 return true;
SomeRandomBloke 0:a29a0225f203 66 }
SomeRandomBloke 0:a29a0225f203 67
SomeRandomBloke 0:a29a0225f203 68 // These are used to send
SomeRandomBloke 0:a29a0225f203 69 void OutputMqtt::addReading(char *topic, char *unused, char *reading ) {
SomeRandomBloke 5:0dbc27a7af55 70 sendCount++;
SomeRandomBloke 0:a29a0225f203 71 mqtt.publish( topic, reading );
SomeRandomBloke 0:a29a0225f203 72 }
SomeRandomBloke 0:a29a0225f203 73
SomeRandomBloke 0:a29a0225f203 74
SomeRandomBloke 0:a29a0225f203 75 // send here does nothing
SomeRandomBloke 0:a29a0225f203 76 int OutputMqtt::send( void ) {
SomeRandomBloke 2:27714c8c9c0a 77 mqtt.live();
SomeRandomBloke 0:a29a0225f203 78 return 0;
SomeRandomBloke 0:a29a0225f203 79 }
SomeRandomBloke 0:a29a0225f203 80
SomeRandomBloke 5:0dbc27a7af55 81
SomeRandomBloke 5:0dbc27a7af55 82 int OutputMqtt::getSendCount( void ) {
SomeRandomBloke 5:0dbc27a7af55 83 return sendCount;
SomeRandomBloke 5:0dbc27a7af55 84 }