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
Revision 3:f19f9c62c00b, committed 2012-04-20
- Comitter:
- SomeRandomBloke
- Date:
- Fri Apr 20 19:32:34 2012 +0000
- Parent:
- 2:27714c8c9c0a
- Child:
- 4:d460406ac780
- Commit message:
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Thu Apr 05 08:03:07 2012 +0000 +++ b/main.cpp Fri Apr 20 19:32:34 2012 +0000 @@ -29,8 +29,8 @@ * 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 + * + * Further documentation available from * http://blog.thiseldo.co.uk/wp-filez/IoTGateway.pdf * * Sample configuration file IOTSETUP.TXT @@ -45,6 +45,7 @@ * rfm12b.band=868 * rfm12b.id=30 * rfm12b.group=212 + * time.host=0.uk.pool.ntp.org * time.timezone=GMT * time.dst=yes * mqtt.host= @@ -59,6 +60,7 @@ #include "mbed.h" +#include <ctype.h> #include "SDFileSystem.h" #include "EthernetNetIf.h" #include "NTPClient.h" @@ -66,10 +68,9 @@ #include "RF12B.h" #include "IoTRouting.h" - using std::string; -#define VERSION_INFO "IoT Gateway Basic - Version 0.5-BETA " +#define VERSION_INFO "IoT Gateway Basic - Version 0.6-BETA " DigitalOut heartbeatLED(LED1, "heartbeatLED"); DigitalOut led2(LED2, "led2"); @@ -103,17 +104,20 @@ __attribute((section("AHBSRAM0"))) static IoTRouting rtr; // Pachube config -__attribute((section("AHBSRAM0"))) static char pachubeApiKey[65]; +__attribute((section("AHBSRAM0"))) char pachubeApiKey[65]; // MQTT config -__attribute((section("AHBSRAM0"))) static IpAddr mqttHostAddress( 0, 0, 0, 0); +__attribute((section("AHBSRAM0"))) 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"))) char mqttUsername[MAX_LINE_LENGTH]; +__attribute((section("AHBSRAM0"))) char mqttPassword[MAX_LINE_LENGTH]; __attribute((section("AHBSRAM0"))) bool useMQTT = false; //char mqttHostName[65]; +// Time server config +__attribute((section("AHBSRAM0"))) char ntpHost[MAX_LINE_LENGTH] = "0.uk.pool.ntp.org"; + // RFM12B config __attribute((section("AHBSRAM0"))) static string rfm12bBands[4] = {"xxx", "433", "868", "915" }; @@ -123,7 +127,6 @@ #define iotConfigFile "/iotfs/IOTSETUP.TXT" - __attribute((section("AHBSRAM0"))) RF12B rfm12b(p11, p12, p13, p14, p18); __attribute((section("AHBSRAM0"))) Serial pc(USBTX, USBRX); // tx, rx @@ -164,6 +167,53 @@ *to = '\0'; } +/** Trim a string in buffer, remove leading and trailing spaces + * + */ +char *trim(char *str) { + size_t len = 0; + char *frontp = str - 1; + char *endp = NULL; + + if ( str == NULL ) + return NULL; + + if ( str[0] == '\0' ) + return str; + + len = strlen(str); + endp = str + len; + + /* Move the front and back pointers to address + * the first non-whitespace characters from + * each end. + */ + while ( isspace(*(++frontp)) ); + while ( isspace(*(--endp)) && endp != frontp ); + +// while( *(++frontp) == ' ' ); +// while( (*(--endp) == ' ') && endp != frontp ); + + if ( str + len - 1 != endp ) + *(endp + 1) = '\0'; + else if ( frontp != str && endp == frontp ) + *str = '\0'; + + /* Shift the string so that it starts at str so + * that if it's dynamically allocated, we can + * still free it on the returned pointer. Note + * the reuse of endp to mean the front of the + * string buffer now. + */ + endp = str; + if ( frontp != str ) { + while ( *frontp ) *endp++ = *frontp++; + *endp = '\0'; + } + + + return str; +} /** Read IoT gateway configuration file * File is of format name=value @@ -179,7 +229,10 @@ // read file while (!feof( fp )) { fgets(lineBuf,MAX_LINE_LENGTH,fp); - //printf("%s",lineBuf); + printf("[%s] ",lineBuf); + trim( lineBuf ); + printf("[%s]\n", lineBuf); + // Need to read each entry and update config char *nameStr; char *valueStr; @@ -191,25 +244,28 @@ 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 ) { setIpAddress( &tmpAddress[0], valueStr ); netMask = IpAddr( tmpAddress[0],tmpAddress[1],tmpAddress[2],tmpAddress[3]); - + } 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 ) { 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, "time.host" ) == 0 ) { + strcpynull(ntpHost, valueStr ); + } else if ( strcmp( nameStr, "pachube.key" ) == 0 ) { strcpynull(pachubeApiKey, valueStr ); - + } else if ( strcmp( nameStr, "mqtt.host" ) == 0 ) { setIpAddress( &tmpAddress[0], valueStr ); mqttHostAddress = IpAddr( tmpAddress[0],tmpAddress[1],tmpAddress[2],tmpAddress[3]); @@ -219,10 +275,10 @@ } 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; @@ -230,7 +286,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 ) { @@ -356,33 +412,34 @@ ipAddress = eth->getIp(); } - // Get Current time - Host server(IpAddr(), 123, "0.uk.pool.ntp.org"); + Host server(IpAddr(), 123, ntpHost); // "0.uk.pool.ntp.org"); ntp.setTime(server); time_t ctTime = time(NULL); printf("\nTime is now (UTC): %s\n", ctime(&ctTime)); - outPachube = OutputPachube(); - outPachube.setApiKey( pachubeApiKey ); -// printf("MAIN: outPachube = %ld\n",(int)&outPachube); - rtr.addOutput( (OutputDef*)&outPachube , OUTPUT_TYPE_PACHUBE); - + if ( strlen(pachubeApiKey) > 0 ) { + // Only add Pachube output if a key has been defined + outPachube = OutputPachube(); + outPachube.setApiKey( pachubeApiKey ); +// printf("MAIN: outPachube = %ld\n",(int)&outPachube); + rtr.addOutput( (OutputDef*)&outPachube , OUTPUT_TYPE_PACHUBE); + } //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 ) { + if ( mqttHostAddress[0] > 0 && mqttHostAddress[3] > 0 ) { outMqtt.initConnection( &mqttHostAddress, mqttPort, mqttUsername, mqttPassword ); //( &serverIpAddr ); useMQTT = outMqtt.init(); - if( useMQTT ) + if ( useMQTT ) rtr.addOutput( (OutputDef*)&outMqtt , OUTPUT_TYPE_MQTT); } - + rtr.initRouting(); printf("Setup OK\n"); @@ -441,10 +498,10 @@ } // If using mqtt then send heartbeat - if(useMQTT) { + if (useMQTT) { outMqtt.send(); // Used for heartbeat/keepalive packet } - + linkLED = !ethernet.link(); }