Description: mbed based IoT Gateway More details http://blog.thiseldo.co.uk/wp-filez/IoTGateway.pdf
Revision 2:27714c8c9c0a, committed 05 Apr 2012
- Comitter:
- Date:
- Thu Apr 05 08:03:07 2012 +0000
- Parent:
- 1:8e1fd64abe9d
- Child:
- 3:f19f9c62c00b
- Commit message:
- Updates to complete mqtt output
Changed in this revision
--- a/MQTTClient.lib Mon Apr 02 22:06:46 2012 +0000 +++ b/MQTTClient.lib Thu Apr 05 08:03:07 2012 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/SomeRandomBloke/code/MQTTClient/#734e384f72c3 +http://mbed.org/users/SomeRandomBloke/code/MQTTClient/#a0e39cea763a
--- a/Outputs/OutputMqtt.cpp Mon Apr 02 22:06:46 2012 +0000
+++ b/Outputs/OutputMqtt.cpp Thu Apr 05 08:03:07 2012 +0000
@@ -37,11 +37,11 @@
sendCount = 0;
}
-void callback(char* topic, char* payload); /*Callback function prototype*/
+static void callback(char* topic, char* payload); /*Callback function prototype*/
//MQTTClient mqtt(serverIpAddr, 1883, callback);
-void callback(char* topic, char* payload)
+static void callback(char* topic, char* payload)
{
printf("Topic: %s\r\n", topic);
printf("Payload: %s\r\n\r\n", payload);
@@ -54,13 +54,15 @@
this->password = password;
}
-void OutputMqtt::init( ) {
+bool OutputMqtt::init( ) {
// printf("MQTT server ip is %d.%d.%d.%d\n",serverIpAddr[0],serverIpAddr[1],serverIpAddr[2],serverIpAddr[3]);
- mqtt.init(serverIpAddr, port, callback);
+ mqtt.init(serverIpAddr, port, username, password, callback);
if(!mqtt.connect("IoTGateway")){
printf("\r\nConnect to server failed ..\r\n");
- // return -1;
+ mqtt.disconnect();
+ return false;
}
+ return true;
}
// These are used to send
@@ -71,6 +73,7 @@
// send here does nothing
int OutputMqtt::send( void ) {
+ mqtt.live();
return 0;
}
--- a/Outputs/OutputMqtt.h Mon Apr 02 22:06:46 2012 +0000
+++ b/Outputs/OutputMqtt.h Thu Apr 05 08:03:07 2012 +0000
@@ -46,7 +46,7 @@
/** Initialise output definition object
*/
- virtual void init();
+ virtual bool init();
/** Set the IP address of the host
*
--- a/Routing/IoTRouting.cpp Mon Apr 02 22:06:46 2012 +0000
+++ b/Routing/IoTRouting.cpp Thu Apr 05 08:03:07 2012 +0000
@@ -245,7 +245,7 @@
}
PayloadRouting* IoTRouting::getRouting( short nodeId, short sensorId ) {
- printf("Getting routing info for node %d, sensor %d size %d - ", nodeId, sensorId, (int)_routing.size());
+ printf("Getting routing info for node %d, sensor %d - ", nodeId, sensorId);
for ( short i=0; i<(short)_routing.size(); i++ ) {
PayloadRouting *pr = (PayloadRouting*)_routing.at(i);
--- a/main.cpp Mon Apr 02 22:06:46 2012 +0000
+++ b/main.cpp Thu Apr 05 08:03:07 2012 +0000
@@ -29,6 +29,9 @@
* Code is to be classed as beta. There is a lot of debug code still includes
* to dump heap sizes and other values. It is still a work in progress and
* should be treated as such.
+ *
+ * Further documentation available from
+ * http://blog.thiseldo.co.uk/wp-filez/IoTGateway.pdf
*
* Sample configuration file IOTSETUP.TXT
*
@@ -45,7 +48,9 @@
* time.timezone=GMT
* time.dst=yes
* mqtt.host=
- * mqtt.port=
+ * mqtt.port=1883
+ * mqtt.username=
+ * mqtt.password=
* @endcode
*
* Some values are not yet used
@@ -57,13 +62,14 @@
#include "SDFileSystem.h"
#include "EthernetNetIf.h"
#include "NTPClient.h"
+#include "dnsresolve.h"
#include "RF12B.h"
#include "IoTRouting.h"
using std::string;
-#define VERSION_INFO "IoT Gateway Basic - Version 0.4-BETA "
+#define VERSION_INFO "IoT Gateway Basic - Version 0.5-BETA "
DigitalOut heartbeatLED(LED1, "heartbeatLED");
DigitalOut led2(LED2, "led2");
@@ -72,13 +78,16 @@
DigitalOut linkLED(p30, "linkLED");
DigitalOut statusLED(p25, "statusLED");
-// Setup which filesystem to use, Local or uSD card
+// Put as much as we can into RAM bank AHBSRAM0 which is reserved for USB that we are not using
+
+// Setup which filesystem to use, Local or uSD card, both use same name
__attribute((section("AHBSRAM0"))) LocalFileSystem fs("iotfs");
//__attribute((section("AHBSRAM0"))) SDFileSystem sd(p5, p6, p7, p8, "iotfs");
__attribute((section("AHBSRAM0"))) EthernetNetIf *eth;
__attribute((section("AHBSRAM0"))) NTPClient ntp;
__attribute((section("AHBSRAM0"))) Ethernet ethernet;
+__attribute((section("AHBSRAM0"))) DNSResolver resolver;
// Configuration values loaded from file iotsetup.dat
__attribute((section("AHBSRAM0"))) IpAddr ipAddress(0, 0, 0, 0);
@@ -89,34 +98,37 @@
// Static buffers
#define MAX_LINE_LENGTH 128
-__attribute__((section("AHBSRAM0"))) static char lineBuf[MAX_LINE_LENGTH];
-__attribute__((section("AHBSRAM0"))) static OutputPachube outPachube;
-__attribute__((section("AHBSRAM0"))) static IoTRouting rtr;
+__attribute((section("AHBSRAM0"))) static char lineBuf[MAX_LINE_LENGTH];
+__attribute((section("AHBSRAM0"))) static OutputPachube outPachube;
+__attribute((section("AHBSRAM0"))) static IoTRouting rtr;
// Pachube config
-__attribute__((section("AHBSRAM0"))) static char pachubeApiKey[65];
+__attribute((section("AHBSRAM0"))) static char pachubeApiKey[65];
// MQTT config
-__attribute((section("AHBSRAM0"))) IpAddr mqttHostAddress( 0, 0, 0, 0);
+__attribute((section("AHBSRAM0"))) static IpAddr mqttHostAddress( 0, 0, 0, 0);
+__attribute((section("AHBSRAM0"))) short mqttPort;
+__attribute((section("AHBSRAM0"))) static char mqttUsername[MAX_LINE_LENGTH];
+__attribute((section("AHBSRAM0"))) static char mqttPassword[MAX_LINE_LENGTH];
+__attribute((section("AHBSRAM0"))) bool useMQTT = false;
+
//char mqttHostName[65];
// RFM12B config
__attribute((section("AHBSRAM0"))) static string rfm12bBands[4] = {"xxx", "433", "868", "915" };
-__attribute((section("AHBSRAM0"))) uint8_t rfm12bId;
-__attribute((section("AHBSRAM0"))) uint8_t rfm12bBand;
-__attribute((section("AHBSRAM0"))) uint8_t rfm12bGroup;
+__attribute((section("AHBSRAM0"))) static uint8_t rfm12bId;
+__attribute((section("AHBSRAM0"))) static uint8_t rfm12bBand;
+__attribute((section("AHBSRAM0"))) static uint8_t rfm12bGroup;
#define iotConfigFile "/iotfs/IOTSETUP.TXT"
-#define startUrlFile "/iotfs/START.URL"
+
__attribute((section("AHBSRAM0"))) RF12B rfm12b(p11, p12, p13, p14, p18);
-
__attribute((section("AHBSRAM0"))) Serial pc(USBTX, USBRX); // tx, rx
// Utility functions
-
/** convert string to IP address
*
* @param ipAddrInt IP address as single 32bit integer
@@ -179,19 +191,38 @@
if ( strcmp( nameStr, "ip.address" ) == 0 ) {
setIpAddress( &tmpAddress[0], valueStr );
ipAddress = IpAddr( tmpAddress[0],tmpAddress[1],tmpAddress[2],tmpAddress[3]);
- } else if ( strcmp( nameStr, "ip.netmask" ) == 0 ) {
+
+ } else if ( strcmp( nameStr, "ip.netmask" ) == 0 ) {
setIpAddress( &tmpAddress[0], valueStr );
netMask = IpAddr( tmpAddress[0],tmpAddress[1],tmpAddress[2],tmpAddress[3]);
- } else if ( strcmp( nameStr, "ip.gateway" ) == 0 ) {
+
+ } else if ( strcmp( nameStr, "ip.gateway" ) == 0 ) {
setIpAddress( &tmpAddress[0], valueStr );
gwAddress = IpAddr( tmpAddress[0],tmpAddress[1],tmpAddress[2],tmpAddress[3]);
- } else if ( strcmp( nameStr, "ip.dns" ) == 0 ) {
+
+ } else if ( strcmp( nameStr, "ip.dns" ) == 0 ) {
setIpAddress( &tmpAddress[0], valueStr );
dnsAddress = IpAddr( tmpAddress[0],tmpAddress[1],tmpAddress[2],tmpAddress[3]);
+
+ } else if ( strcmp( nameStr, "ip.mode" ) == 0 ) {
+ useDHCP = (strncmp( valueStr, "dhcp", 4) == 0 ? true : false);
+
} else if ( strcmp( nameStr, "pachube.key" ) == 0 ) {
strcpynull(pachubeApiKey, valueStr );
- } else if ( strcmp( nameStr, "ip.mode" ) == 0 ) {
- useDHCP = (strncmp( valueStr, "dhcp", 4) == 0 ? true : false);
+
+ } else if ( strcmp( nameStr, "mqtt.host" ) == 0 ) {
+ setIpAddress( &tmpAddress[0], valueStr );
+ mqttHostAddress = IpAddr( tmpAddress[0],tmpAddress[1],tmpAddress[2],tmpAddress[3]);
+
+ } else if ( strcmp( nameStr, "mqtt.port" ) == 0 ) {
+ mqttPort = atoi( valueStr );
+
+ } else if ( strcmp( nameStr, "mqtt.username" ) == 0 ) {
+ strcpynull(mqttUsername, valueStr );
+
+ } else if ( strcmp( nameStr, "mqtt.password" ) == 0 ) {
+ strcpynull(mqttPassword, valueStr );
+
} else if ( strcmp( nameStr, "rfm12b.band" ) == 0 ) {
if ( strncmp( valueStr, "433", 3 ) == 0 )
rfm12bBand = RF12_433MHZ;
@@ -199,6 +230,7 @@
rfm12bBand = RF12_868MHZ;
else if ( strncmp( valueStr, "915", 3 ) == 0 )
rfm12bBand = RF12_915MHZ;
+
} else if ( strcmp( nameStr, "rfm12b.id" ) == 0 ) {
rfm12bId = atoi( valueStr );
} else if ( strcmp( nameStr, "rfm12b.group" ) == 0 ) {
@@ -232,7 +264,6 @@
ctTime = time(NULL);
fprintf(fp, "# iotsetup created (UTC) %s\n", ctime(&ctTime));
- fprintf(fp, "pachube.key=%s\n", pachubeApiKey );
fprintf(fp, "ip.mode=%s\n", (useDHCP ? "dhcp" : "fixed") );
// Add msg to say net config being ignored
if ( useDHCP )
@@ -249,6 +280,14 @@
fprintf(fp, "time.timezone=GMT\n");
fprintf(fp, "time.dst=yes\n");
+
+ fprintf(fp, "pachube.key=%s\n", pachubeApiKey );
+
+ fprintf(fp, "mqtt.host=%hhu.%hhu.%hhu.%hhu\n",mqttHostAddress[0],mqttHostAddress[1],mqttHostAddress[2],mqttHostAddress[3]);
+ fprintf(fp, "mqtt.port=%d\n",mqttPort);
+ fprintf(fp, "mqtt.username=%s\n", mqttUsername );
+ fprintf(fp, "mqtt.password=%s\n", mqttPassword );
+
fclose(fp);
return true;
@@ -327,14 +366,23 @@
outPachube = OutputPachube();
outPachube.setApiKey( pachubeApiKey );
-// IpAddr serverIpAddr(192,168,1,77);
-// OutputMqtt outMqtt = OutputMqtt();
-// outMqtt.setHost( &serverIpAddr );
+// printf("MAIN: outPachube = %ld\n",(int)&outPachube);
+ rtr.addOutput( (OutputDef*)&outPachube , OUTPUT_TYPE_PACHUBE);
-// printf("MAIN: outPachube = %ld\n",(int)&outPachube);
- rtr.addOutput( (OutputDef*)&outPachube , 1);
-// rtr.addOutput( (OutputDef*)&outMqtt , 2);
+ //mqttHostAddress = IpAddr(192,168,1,77);
+ OutputMqtt outMqtt = OutputMqtt();
+// mqttHostAddress = resolver.resolveName("api.pachube.com");
+ // Only use MQTT is we have a host IP address
+ if( mqttHostAddress[0] > 0 && mqttHostAddress[3] > 0 ) {
+ outMqtt.initConnection( &mqttHostAddress, mqttPort, mqttUsername, mqttPassword );
+ //( &serverIpAddr );
+ useMQTT = outMqtt.init();
+
+ if( useMQTT )
+ rtr.addOutput( (OutputDef*)&outMqtt , OUTPUT_TYPE_MQTT);
+ }
+
rtr.initRouting();
printf("Setup OK\n");
@@ -392,6 +440,11 @@
tm.start();
}
+ // If using mqtt then send heartbeat
+ if(useMQTT) {
+ outMqtt.send(); // Used for heartbeat/keepalive packet
+ }
+
linkLED = !ethernet.link();
}

