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
Diff: main.cpp
- Revision:
- 2:27714c8c9c0a
- Parent:
- 0:a29a0225f203
- Child:
- 3:f19f9c62c00b
diff -r 8e1fd64abe9d -r 27714c8c9c0a main.cpp --- 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(); }