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
main.cpp@0:a29a0225f203, 2012-04-02 (annotated)
- Committer:
- SomeRandomBloke
- Date:
- Mon Apr 02 22:05:20 2012 +0000
- Revision:
- 0:a29a0225f203
- Child:
- 2:27714c8c9c0a
Initial version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
SomeRandomBloke | 0:a29a0225f203 | 1 | /** IoT Gateway main code |
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 | * Code is to be classed as beta. There is a lot of debug code still includes |
SomeRandomBloke | 0:a29a0225f203 | 30 | * to dump heap sizes and other values. It is still a work in progress and |
SomeRandomBloke | 0:a29a0225f203 | 31 | * should be treated as such. |
SomeRandomBloke | 0:a29a0225f203 | 32 | * |
SomeRandomBloke | 0:a29a0225f203 | 33 | * Sample configuration file IOTSETUP.TXT |
SomeRandomBloke | 0:a29a0225f203 | 34 | * |
SomeRandomBloke | 0:a29a0225f203 | 35 | * @code |
SomeRandomBloke | 0:a29a0225f203 | 36 | * pachube.key=***your pachube api key*** |
SomeRandomBloke | 0:a29a0225f203 | 37 | * ip.mode=fixed |
SomeRandomBloke | 0:a29a0225f203 | 38 | * ip.address=192.168.1.99 |
SomeRandomBloke | 0:a29a0225f203 | 39 | * ip.netmask=255.255.255.0 |
SomeRandomBloke | 0:a29a0225f203 | 40 | * ip.gateway=192.168.1.1 |
SomeRandomBloke | 0:a29a0225f203 | 41 | * ip.dns=192.168.1.1 |
SomeRandomBloke | 0:a29a0225f203 | 42 | * rfm12b.band=868 |
SomeRandomBloke | 0:a29a0225f203 | 43 | * rfm12b.id=30 |
SomeRandomBloke | 0:a29a0225f203 | 44 | * rfm12b.group=212 |
SomeRandomBloke | 0:a29a0225f203 | 45 | * time.timezone=GMT |
SomeRandomBloke | 0:a29a0225f203 | 46 | * time.dst=yes |
SomeRandomBloke | 0:a29a0225f203 | 47 | * mqtt.host= |
SomeRandomBloke | 0:a29a0225f203 | 48 | * mqtt.port= |
SomeRandomBloke | 0:a29a0225f203 | 49 | * @endcode |
SomeRandomBloke | 0:a29a0225f203 | 50 | * |
SomeRandomBloke | 0:a29a0225f203 | 51 | * Some values are not yet used |
SomeRandomBloke | 0:a29a0225f203 | 52 | * |
SomeRandomBloke | 0:a29a0225f203 | 53 | */ |
SomeRandomBloke | 0:a29a0225f203 | 54 | |
SomeRandomBloke | 0:a29a0225f203 | 55 | |
SomeRandomBloke | 0:a29a0225f203 | 56 | #include "mbed.h" |
SomeRandomBloke | 0:a29a0225f203 | 57 | #include "SDFileSystem.h" |
SomeRandomBloke | 0:a29a0225f203 | 58 | #include "EthernetNetIf.h" |
SomeRandomBloke | 0:a29a0225f203 | 59 | #include "NTPClient.h" |
SomeRandomBloke | 0:a29a0225f203 | 60 | #include "RF12B.h" |
SomeRandomBloke | 0:a29a0225f203 | 61 | #include "IoTRouting.h" |
SomeRandomBloke | 0:a29a0225f203 | 62 | |
SomeRandomBloke | 0:a29a0225f203 | 63 | |
SomeRandomBloke | 0:a29a0225f203 | 64 | using std::string; |
SomeRandomBloke | 0:a29a0225f203 | 65 | |
SomeRandomBloke | 0:a29a0225f203 | 66 | #define VERSION_INFO "IoT Gateway Basic - Version 0.4-BETA " |
SomeRandomBloke | 0:a29a0225f203 | 67 | |
SomeRandomBloke | 0:a29a0225f203 | 68 | DigitalOut heartbeatLED(LED1, "heartbeatLED"); |
SomeRandomBloke | 0:a29a0225f203 | 69 | DigitalOut led2(LED2, "led2"); |
SomeRandomBloke | 0:a29a0225f203 | 70 | DigitalOut led3(LED3, "led3"); |
SomeRandomBloke | 0:a29a0225f203 | 71 | //DigitalOut led4(LED4, "led4"); |
SomeRandomBloke | 0:a29a0225f203 | 72 | DigitalOut linkLED(p30, "linkLED"); |
SomeRandomBloke | 0:a29a0225f203 | 73 | DigitalOut statusLED(p25, "statusLED"); |
SomeRandomBloke | 0:a29a0225f203 | 74 | |
SomeRandomBloke | 0:a29a0225f203 | 75 | // Setup which filesystem to use, Local or uSD card |
SomeRandomBloke | 0:a29a0225f203 | 76 | __attribute((section("AHBSRAM0"))) LocalFileSystem fs("iotfs"); |
SomeRandomBloke | 0:a29a0225f203 | 77 | //__attribute((section("AHBSRAM0"))) SDFileSystem sd(p5, p6, p7, p8, "iotfs"); |
SomeRandomBloke | 0:a29a0225f203 | 78 | |
SomeRandomBloke | 0:a29a0225f203 | 79 | __attribute((section("AHBSRAM0"))) EthernetNetIf *eth; |
SomeRandomBloke | 0:a29a0225f203 | 80 | __attribute((section("AHBSRAM0"))) NTPClient ntp; |
SomeRandomBloke | 0:a29a0225f203 | 81 | __attribute((section("AHBSRAM0"))) Ethernet ethernet; |
SomeRandomBloke | 0:a29a0225f203 | 82 | |
SomeRandomBloke | 0:a29a0225f203 | 83 | // Configuration values loaded from file iotsetup.dat |
SomeRandomBloke | 0:a29a0225f203 | 84 | __attribute((section("AHBSRAM0"))) IpAddr ipAddress(0, 0, 0, 0); |
SomeRandomBloke | 0:a29a0225f203 | 85 | __attribute((section("AHBSRAM0"))) IpAddr netMask(255, 255, 255, 0); |
SomeRandomBloke | 0:a29a0225f203 | 86 | __attribute((section("AHBSRAM0"))) IpAddr gwAddress(0, 0, 0, 0); |
SomeRandomBloke | 0:a29a0225f203 | 87 | __attribute((section("AHBSRAM0"))) IpAddr dnsAddress(0, 0, 0, 0); |
SomeRandomBloke | 0:a29a0225f203 | 88 | __attribute((section("AHBSRAM0"))) bool useDHCP = false; |
SomeRandomBloke | 0:a29a0225f203 | 89 | |
SomeRandomBloke | 0:a29a0225f203 | 90 | // Static buffers |
SomeRandomBloke | 0:a29a0225f203 | 91 | #define MAX_LINE_LENGTH 128 |
SomeRandomBloke | 0:a29a0225f203 | 92 | __attribute__((section("AHBSRAM0"))) static char lineBuf[MAX_LINE_LENGTH]; |
SomeRandomBloke | 0:a29a0225f203 | 93 | __attribute__((section("AHBSRAM0"))) static OutputPachube outPachube; |
SomeRandomBloke | 0:a29a0225f203 | 94 | __attribute__((section("AHBSRAM0"))) static IoTRouting rtr; |
SomeRandomBloke | 0:a29a0225f203 | 95 | |
SomeRandomBloke | 0:a29a0225f203 | 96 | // Pachube config |
SomeRandomBloke | 0:a29a0225f203 | 97 | __attribute__((section("AHBSRAM0"))) static char pachubeApiKey[65]; |
SomeRandomBloke | 0:a29a0225f203 | 98 | |
SomeRandomBloke | 0:a29a0225f203 | 99 | // MQTT config |
SomeRandomBloke | 0:a29a0225f203 | 100 | __attribute((section("AHBSRAM0"))) IpAddr mqttHostAddress( 0, 0, 0, 0); |
SomeRandomBloke | 0:a29a0225f203 | 101 | //char mqttHostName[65]; |
SomeRandomBloke | 0:a29a0225f203 | 102 | |
SomeRandomBloke | 0:a29a0225f203 | 103 | |
SomeRandomBloke | 0:a29a0225f203 | 104 | // RFM12B config |
SomeRandomBloke | 0:a29a0225f203 | 105 | __attribute((section("AHBSRAM0"))) static string rfm12bBands[4] = {"xxx", "433", "868", "915" }; |
SomeRandomBloke | 0:a29a0225f203 | 106 | __attribute((section("AHBSRAM0"))) uint8_t rfm12bId; |
SomeRandomBloke | 0:a29a0225f203 | 107 | __attribute((section("AHBSRAM0"))) uint8_t rfm12bBand; |
SomeRandomBloke | 0:a29a0225f203 | 108 | __attribute((section("AHBSRAM0"))) uint8_t rfm12bGroup; |
SomeRandomBloke | 0:a29a0225f203 | 109 | |
SomeRandomBloke | 0:a29a0225f203 | 110 | #define iotConfigFile "/iotfs/IOTSETUP.TXT" |
SomeRandomBloke | 0:a29a0225f203 | 111 | #define startUrlFile "/iotfs/START.URL" |
SomeRandomBloke | 0:a29a0225f203 | 112 | |
SomeRandomBloke | 0:a29a0225f203 | 113 | __attribute((section("AHBSRAM0"))) RF12B rfm12b(p11, p12, p13, p14, p18); |
SomeRandomBloke | 0:a29a0225f203 | 114 | |
SomeRandomBloke | 0:a29a0225f203 | 115 | __attribute((section("AHBSRAM0"))) Serial pc(USBTX, USBRX); // tx, rx |
SomeRandomBloke | 0:a29a0225f203 | 116 | |
SomeRandomBloke | 0:a29a0225f203 | 117 | // Utility functions |
SomeRandomBloke | 0:a29a0225f203 | 118 | |
SomeRandomBloke | 0:a29a0225f203 | 119 | |
SomeRandomBloke | 0:a29a0225f203 | 120 | /** convert string to IP address |
SomeRandomBloke | 0:a29a0225f203 | 121 | * |
SomeRandomBloke | 0:a29a0225f203 | 122 | * @param ipAddrInt IP address as single 32bit integer |
SomeRandomBloke | 0:a29a0225f203 | 123 | * @param value The IP address as a string |
SomeRandomBloke | 0:a29a0225f203 | 124 | */ |
SomeRandomBloke | 0:a29a0225f203 | 125 | void setIpAddress( int *ipAddrInt, char *value ) { |
SomeRandomBloke | 0:a29a0225f203 | 126 | sscanf(value, "%d.%d.%d.%d", &ipAddrInt[0],&ipAddrInt[1],&ipAddrInt[2],&ipAddrInt[3]); |
SomeRandomBloke | 0:a29a0225f203 | 127 | } |
SomeRandomBloke | 0:a29a0225f203 | 128 | |
SomeRandomBloke | 0:a29a0225f203 | 129 | /** Get the value from a name=value pair |
SomeRandomBloke | 0:a29a0225f203 | 130 | * |
SomeRandomBloke | 0:a29a0225f203 | 131 | * @param buf The name/value pair character buffer |
SomeRandomBloke | 0:a29a0225f203 | 132 | * @returns pointer to value |
SomeRandomBloke | 0:a29a0225f203 | 133 | */ |
SomeRandomBloke | 0:a29a0225f203 | 134 | char *getConfigValue( char *buf ) { |
SomeRandomBloke | 0:a29a0225f203 | 135 | char *ptr = strchr(buf, '='); |
SomeRandomBloke | 0:a29a0225f203 | 136 | if ( ptr == NULL ) |
SomeRandomBloke | 0:a29a0225f203 | 137 | return NULL; |
SomeRandomBloke | 0:a29a0225f203 | 138 | ptr++; |
SomeRandomBloke | 0:a29a0225f203 | 139 | return ptr; |
SomeRandomBloke | 0:a29a0225f203 | 140 | } |
SomeRandomBloke | 0:a29a0225f203 | 141 | |
SomeRandomBloke | 0:a29a0225f203 | 142 | /** Copy zero or CR/LF terminated string to new zero terminated string |
SomeRandomBloke | 0:a29a0225f203 | 143 | * Used when reading config values from files to remove CR/LF endings |
SomeRandomBloke | 0:a29a0225f203 | 144 | * |
SomeRandomBloke | 0:a29a0225f203 | 145 | * @param to Char pointer to destination |
SomeRandomBloke | 0:a29a0225f203 | 146 | * @param from Char pointer to source |
SomeRandomBloke | 0:a29a0225f203 | 147 | */ |
SomeRandomBloke | 0:a29a0225f203 | 148 | void strcpynull(char *to, char *from ) { |
SomeRandomBloke | 0:a29a0225f203 | 149 | while ( *from != '\n' && *from != '\r' && *from != '\0' ) { |
SomeRandomBloke | 0:a29a0225f203 | 150 | *to++ = *from++; |
SomeRandomBloke | 0:a29a0225f203 | 151 | } |
SomeRandomBloke | 0:a29a0225f203 | 152 | *to = '\0'; |
SomeRandomBloke | 0:a29a0225f203 | 153 | } |
SomeRandomBloke | 0:a29a0225f203 | 154 | |
SomeRandomBloke | 0:a29a0225f203 | 155 | |
SomeRandomBloke | 0:a29a0225f203 | 156 | /** Read IoT gateway configuration file |
SomeRandomBloke | 0:a29a0225f203 | 157 | * File is of format name=value |
SomeRandomBloke | 0:a29a0225f203 | 158 | * |
SomeRandomBloke | 0:a29a0225f203 | 159 | */ |
SomeRandomBloke | 0:a29a0225f203 | 160 | bool readConfig() { |
SomeRandomBloke | 0:a29a0225f203 | 161 | |
SomeRandomBloke | 0:a29a0225f203 | 162 | FILE *fp = fopen(iotConfigFile, "r"); |
SomeRandomBloke | 0:a29a0225f203 | 163 | if (fp == NULL) { |
SomeRandomBloke | 0:a29a0225f203 | 164 | printf("Could not open file %s for read\n", iotConfigFile); |
SomeRandomBloke | 0:a29a0225f203 | 165 | return false; |
SomeRandomBloke | 0:a29a0225f203 | 166 | } |
SomeRandomBloke | 0:a29a0225f203 | 167 | // read file |
SomeRandomBloke | 0:a29a0225f203 | 168 | while (!feof( fp )) { |
SomeRandomBloke | 0:a29a0225f203 | 169 | fgets(lineBuf,MAX_LINE_LENGTH,fp); |
SomeRandomBloke | 0:a29a0225f203 | 170 | //printf("%s",lineBuf); |
SomeRandomBloke | 0:a29a0225f203 | 171 | // Need to read each entry and update config |
SomeRandomBloke | 0:a29a0225f203 | 172 | char *nameStr; |
SomeRandomBloke | 0:a29a0225f203 | 173 | char *valueStr; |
SomeRandomBloke | 0:a29a0225f203 | 174 | nameStr = lineBuf; |
SomeRandomBloke | 0:a29a0225f203 | 175 | valueStr = strchr( lineBuf, '=' ); |
SomeRandomBloke | 0:a29a0225f203 | 176 | if ( valueStr != NULL ) { |
SomeRandomBloke | 0:a29a0225f203 | 177 | *valueStr++ = '\0'; |
SomeRandomBloke | 0:a29a0225f203 | 178 | int tmpAddress[4] = {0,0,0,0}; |
SomeRandomBloke | 0:a29a0225f203 | 179 | if ( strcmp( nameStr, "ip.address" ) == 0 ) { |
SomeRandomBloke | 0:a29a0225f203 | 180 | setIpAddress( &tmpAddress[0], valueStr ); |
SomeRandomBloke | 0:a29a0225f203 | 181 | ipAddress = IpAddr( tmpAddress[0],tmpAddress[1],tmpAddress[2],tmpAddress[3]); |
SomeRandomBloke | 0:a29a0225f203 | 182 | } else if ( strcmp( nameStr, "ip.netmask" ) == 0 ) { |
SomeRandomBloke | 0:a29a0225f203 | 183 | setIpAddress( &tmpAddress[0], valueStr ); |
SomeRandomBloke | 0:a29a0225f203 | 184 | netMask = IpAddr( tmpAddress[0],tmpAddress[1],tmpAddress[2],tmpAddress[3]); |
SomeRandomBloke | 0:a29a0225f203 | 185 | } else if ( strcmp( nameStr, "ip.gateway" ) == 0 ) { |
SomeRandomBloke | 0:a29a0225f203 | 186 | setIpAddress( &tmpAddress[0], valueStr ); |
SomeRandomBloke | 0:a29a0225f203 | 187 | gwAddress = IpAddr( tmpAddress[0],tmpAddress[1],tmpAddress[2],tmpAddress[3]); |
SomeRandomBloke | 0:a29a0225f203 | 188 | } else if ( strcmp( nameStr, "ip.dns" ) == 0 ) { |
SomeRandomBloke | 0:a29a0225f203 | 189 | setIpAddress( &tmpAddress[0], valueStr ); |
SomeRandomBloke | 0:a29a0225f203 | 190 | dnsAddress = IpAddr( tmpAddress[0],tmpAddress[1],tmpAddress[2],tmpAddress[3]); |
SomeRandomBloke | 0:a29a0225f203 | 191 | } else if ( strcmp( nameStr, "pachube.key" ) == 0 ) { |
SomeRandomBloke | 0:a29a0225f203 | 192 | strcpynull(pachubeApiKey, valueStr ); |
SomeRandomBloke | 0:a29a0225f203 | 193 | } else if ( strcmp( nameStr, "ip.mode" ) == 0 ) { |
SomeRandomBloke | 0:a29a0225f203 | 194 | useDHCP = (strncmp( valueStr, "dhcp", 4) == 0 ? true : false); |
SomeRandomBloke | 0:a29a0225f203 | 195 | } else if ( strcmp( nameStr, "rfm12b.band" ) == 0 ) { |
SomeRandomBloke | 0:a29a0225f203 | 196 | if ( strncmp( valueStr, "433", 3 ) == 0 ) |
SomeRandomBloke | 0:a29a0225f203 | 197 | rfm12bBand = RF12_433MHZ; |
SomeRandomBloke | 0:a29a0225f203 | 198 | else if ( strncmp( valueStr, "868", 3 ) == 0 ) |
SomeRandomBloke | 0:a29a0225f203 | 199 | rfm12bBand = RF12_868MHZ; |
SomeRandomBloke | 0:a29a0225f203 | 200 | else if ( strncmp( valueStr, "915", 3 ) == 0 ) |
SomeRandomBloke | 0:a29a0225f203 | 201 | rfm12bBand = RF12_915MHZ; |
SomeRandomBloke | 0:a29a0225f203 | 202 | } else if ( strcmp( nameStr, "rfm12b.id" ) == 0 ) { |
SomeRandomBloke | 0:a29a0225f203 | 203 | rfm12bId = atoi( valueStr ); |
SomeRandomBloke | 0:a29a0225f203 | 204 | } else if ( strcmp( nameStr, "rfm12b.group" ) == 0 ) { |
SomeRandomBloke | 0:a29a0225f203 | 205 | rfm12bGroup = atoi( valueStr ); |
SomeRandomBloke | 0:a29a0225f203 | 206 | } |
SomeRandomBloke | 0:a29a0225f203 | 207 | } |
SomeRandomBloke | 0:a29a0225f203 | 208 | } |
SomeRandomBloke | 0:a29a0225f203 | 209 | fclose(fp); |
SomeRandomBloke | 0:a29a0225f203 | 210 | |
SomeRandomBloke | 0:a29a0225f203 | 211 | return true; |
SomeRandomBloke | 0:a29a0225f203 | 212 | } |
SomeRandomBloke | 0:a29a0225f203 | 213 | |
SomeRandomBloke | 0:a29a0225f203 | 214 | /** Write the current config to file |
SomeRandomBloke | 0:a29a0225f203 | 215 | */ |
SomeRandomBloke | 0:a29a0225f203 | 216 | bool writeConfig() { |
SomeRandomBloke | 0:a29a0225f203 | 217 | |
SomeRandomBloke | 0:a29a0225f203 | 218 | // Doesnt work? |
SomeRandomBloke | 0:a29a0225f203 | 219 | // Rename original file |
SomeRandomBloke | 0:a29a0225f203 | 220 | // if( rename ( iotConfigFile.c_str(), iotConfigFileBak.c_str() ) != 0 ) { |
SomeRandomBloke | 0:a29a0225f203 | 221 | // printf("Could not rename file %s\n", iotConfigFile); |
SomeRandomBloke | 0:a29a0225f203 | 222 | // return false; |
SomeRandomBloke | 0:a29a0225f203 | 223 | // } |
SomeRandomBloke | 0:a29a0225f203 | 224 | |
SomeRandomBloke | 0:a29a0225f203 | 225 | FILE *fp = fopen(iotConfigFile, "w"); |
SomeRandomBloke | 0:a29a0225f203 | 226 | if (fp == NULL) { |
SomeRandomBloke | 0:a29a0225f203 | 227 | printf("Could not open file %s for write\n", iotConfigFile); |
SomeRandomBloke | 0:a29a0225f203 | 228 | return false; |
SomeRandomBloke | 0:a29a0225f203 | 229 | } |
SomeRandomBloke | 0:a29a0225f203 | 230 | |
SomeRandomBloke | 0:a29a0225f203 | 231 | time_t ctTime; |
SomeRandomBloke | 0:a29a0225f203 | 232 | ctTime = time(NULL); |
SomeRandomBloke | 0:a29a0225f203 | 233 | |
SomeRandomBloke | 0:a29a0225f203 | 234 | fprintf(fp, "# iotsetup created (UTC) %s\n", ctime(&ctTime)); |
SomeRandomBloke | 0:a29a0225f203 | 235 | fprintf(fp, "pachube.key=%s\n", pachubeApiKey ); |
SomeRandomBloke | 0:a29a0225f203 | 236 | fprintf(fp, "ip.mode=%s\n", (useDHCP ? "dhcp" : "fixed") ); |
SomeRandomBloke | 0:a29a0225f203 | 237 | // Add msg to say net config being ignored |
SomeRandomBloke | 0:a29a0225f203 | 238 | if ( useDHCP ) |
SomeRandomBloke | 0:a29a0225f203 | 239 | fprintf(fp, "# Following ip.* parameters not used in DHCP mode\n"); |
SomeRandomBloke | 0:a29a0225f203 | 240 | |
SomeRandomBloke | 0:a29a0225f203 | 241 | fprintf(fp, "ip.address=%hhu.%hhu.%hhu.%hhu\n", ipAddress[0], ipAddress[1],ipAddress[2],ipAddress[3]); |
SomeRandomBloke | 0:a29a0225f203 | 242 | fprintf(fp, "ip.netmask=%hhu.%hhu.%hhu.%hhu\n",netMask[0], netMask[1], netMask[2], netMask[3]); |
SomeRandomBloke | 0:a29a0225f203 | 243 | fprintf(fp, "ip.gateway=%hhu.%hhu.%hhu.%hhu\n",gwAddress[0],gwAddress[1],gwAddress[2],gwAddress[3]); |
SomeRandomBloke | 0:a29a0225f203 | 244 | fprintf(fp, "ip.dns=%hhu.%hhu.%hhu.%hhu\n",dnsAddress[0],dnsAddress[1],dnsAddress[2],dnsAddress[3]); |
SomeRandomBloke | 0:a29a0225f203 | 245 | |
SomeRandomBloke | 0:a29a0225f203 | 246 | fprintf(fp, "rfm12b.band=%s\n",rfm12bBands[rfm12bBand].c_str()); |
SomeRandomBloke | 0:a29a0225f203 | 247 | fprintf(fp, "rfm12b.id=%d\n",rfm12bId); |
SomeRandomBloke | 0:a29a0225f203 | 248 | fprintf(fp, "rfm12b.group=%d\n",rfm12bGroup); |
SomeRandomBloke | 0:a29a0225f203 | 249 | |
SomeRandomBloke | 0:a29a0225f203 | 250 | fprintf(fp, "time.timezone=GMT\n"); |
SomeRandomBloke | 0:a29a0225f203 | 251 | fprintf(fp, "time.dst=yes\n"); |
SomeRandomBloke | 0:a29a0225f203 | 252 | fclose(fp); |
SomeRandomBloke | 0:a29a0225f203 | 253 | |
SomeRandomBloke | 0:a29a0225f203 | 254 | return true; |
SomeRandomBloke | 0:a29a0225f203 | 255 | } |
SomeRandomBloke | 0:a29a0225f203 | 256 | |
SomeRandomBloke | 0:a29a0225f203 | 257 | |
SomeRandomBloke | 0:a29a0225f203 | 258 | // These external symbols are maintained by the linker to indicate the |
SomeRandomBloke | 0:a29a0225f203 | 259 | // location of various regions in the device's memory. They will be used by |
SomeRandomBloke | 0:a29a0225f203 | 260 | // DisplayRAMBanks() to dump the size of each RAM bank to stdout. |
SomeRandomBloke | 0:a29a0225f203 | 261 | extern unsigned int Image$$RW_IRAM1$$Base; |
SomeRandomBloke | 0:a29a0225f203 | 262 | extern unsigned int Image$$RW_IRAM1$$ZI$$Limit; |
SomeRandomBloke | 0:a29a0225f203 | 263 | extern unsigned int Image$$RW_IRAM2$$Base; |
SomeRandomBloke | 0:a29a0225f203 | 264 | extern unsigned int Image$$RW_IRAM2$$ZI$$Limit; |
SomeRandomBloke | 0:a29a0225f203 | 265 | extern unsigned int Image$$RW_IRAM3$$Base; |
SomeRandomBloke | 0:a29a0225f203 | 266 | extern unsigned int Image$$RW_IRAM3$$ZI$$Limit; |
SomeRandomBloke | 0:a29a0225f203 | 267 | |
SomeRandomBloke | 0:a29a0225f203 | 268 | // Displays the size of static allocations for each RAM bank as indicated by |
SomeRandomBloke | 0:a29a0225f203 | 269 | // ARM linker to stdout. |
SomeRandomBloke | 0:a29a0225f203 | 270 | static void DisplayRAMBanks(void) { |
SomeRandomBloke | 0:a29a0225f203 | 271 | printf("Static RAM bank allocations\r\n"); |
SomeRandomBloke | 0:a29a0225f203 | 272 | printf(" Main RAM = %u\r\n", (unsigned int)&Image$$RW_IRAM1$$ZI$$Limit - |
SomeRandomBloke | 0:a29a0225f203 | 273 | (unsigned int)&Image$$RW_IRAM1$$Base); |
SomeRandomBloke | 0:a29a0225f203 | 274 | printf(" RAM0 = %u\r\n", (unsigned int)&Image$$RW_IRAM2$$ZI$$Limit - |
SomeRandomBloke | 0:a29a0225f203 | 275 | (unsigned int)&Image$$RW_IRAM2$$Base); |
SomeRandomBloke | 0:a29a0225f203 | 276 | printf(" RAM1 = %u\r\n", (unsigned int)&Image$$RW_IRAM3$$ZI$$Limit - |
SomeRandomBloke | 0:a29a0225f203 | 277 | (unsigned int)&Image$$RW_IRAM3$$Base); |
SomeRandomBloke | 0:a29a0225f203 | 278 | } |
SomeRandomBloke | 0:a29a0225f203 | 279 | |
SomeRandomBloke | 0:a29a0225f203 | 280 | /** Main function, where all the magic starts |
SomeRandomBloke | 0:a29a0225f203 | 281 | */ |
SomeRandomBloke | 0:a29a0225f203 | 282 | int main() { |
SomeRandomBloke | 0:a29a0225f203 | 283 | linkLED = 1; |
SomeRandomBloke | 0:a29a0225f203 | 284 | statusLED = 0; |
SomeRandomBloke | 0:a29a0225f203 | 285 | pc.baud(115200); |
SomeRandomBloke | 0:a29a0225f203 | 286 | |
SomeRandomBloke | 0:a29a0225f203 | 287 | printf(VERSION_INFO); |
SomeRandomBloke | 0:a29a0225f203 | 288 | printf("\n"); |
SomeRandomBloke | 0:a29a0225f203 | 289 | |
SomeRandomBloke | 0:a29a0225f203 | 290 | DisplayRAMBanks(); |
SomeRandomBloke | 0:a29a0225f203 | 291 | |
SomeRandomBloke | 0:a29a0225f203 | 292 | printf("Setting up...\n"); |
SomeRandomBloke | 0:a29a0225f203 | 293 | printf("\nHEAP STATS\n"); |
SomeRandomBloke | 0:a29a0225f203 | 294 | __heapstats((__heapprt)fprintf,stderr); |
SomeRandomBloke | 0:a29a0225f203 | 295 | |
SomeRandomBloke | 0:a29a0225f203 | 296 | if ( !readConfig() ) { |
SomeRandomBloke | 0:a29a0225f203 | 297 | error("Setup failed"); |
SomeRandomBloke | 0:a29a0225f203 | 298 | } |
SomeRandomBloke | 0:a29a0225f203 | 299 | |
SomeRandomBloke | 0:a29a0225f203 | 300 | if (useDHCP) { |
SomeRandomBloke | 0:a29a0225f203 | 301 | printf("Using DHCP\n"); |
SomeRandomBloke | 0:a29a0225f203 | 302 | eth = new EthernetNetIf( "IoTGateway" ); |
SomeRandomBloke | 0:a29a0225f203 | 303 | } else { |
SomeRandomBloke | 0:a29a0225f203 | 304 | // printf("Using Fixed addressing\n"); |
SomeRandomBloke | 0:a29a0225f203 | 305 | eth = new EthernetNetIf( ipAddress, netMask, gwAddress, dnsAddress ); |
SomeRandomBloke | 0:a29a0225f203 | 306 | } |
SomeRandomBloke | 0:a29a0225f203 | 307 | EthernetErr ethErr = eth->setup(); |
SomeRandomBloke | 0:a29a0225f203 | 308 | if (ethErr) { |
SomeRandomBloke | 0:a29a0225f203 | 309 | printf("Error %d in setup.\n", ethErr); |
SomeRandomBloke | 0:a29a0225f203 | 310 | return -1; |
SomeRandomBloke | 0:a29a0225f203 | 311 | } |
SomeRandomBloke | 0:a29a0225f203 | 312 | |
SomeRandomBloke | 0:a29a0225f203 | 313 | linkLED = !ethernet.link(); |
SomeRandomBloke | 0:a29a0225f203 | 314 | |
SomeRandomBloke | 0:a29a0225f203 | 315 | if (useDHCP) { |
SomeRandomBloke | 0:a29a0225f203 | 316 | // We are using dhcp so get IP Address |
SomeRandomBloke | 0:a29a0225f203 | 317 | ipAddress = eth->getIp(); |
SomeRandomBloke | 0:a29a0225f203 | 318 | } |
SomeRandomBloke | 0:a29a0225f203 | 319 | |
SomeRandomBloke | 0:a29a0225f203 | 320 | |
SomeRandomBloke | 0:a29a0225f203 | 321 | // Get Current time |
SomeRandomBloke | 0:a29a0225f203 | 322 | Host server(IpAddr(), 123, "0.uk.pool.ntp.org"); |
SomeRandomBloke | 0:a29a0225f203 | 323 | ntp.setTime(server); |
SomeRandomBloke | 0:a29a0225f203 | 324 | |
SomeRandomBloke | 0:a29a0225f203 | 325 | time_t ctTime = time(NULL); |
SomeRandomBloke | 0:a29a0225f203 | 326 | printf("\nTime is now (UTC): %s\n", ctime(&ctTime)); |
SomeRandomBloke | 0:a29a0225f203 | 327 | |
SomeRandomBloke | 0:a29a0225f203 | 328 | outPachube = OutputPachube(); |
SomeRandomBloke | 0:a29a0225f203 | 329 | outPachube.setApiKey( pachubeApiKey ); |
SomeRandomBloke | 0:a29a0225f203 | 330 | // IpAddr serverIpAddr(192,168,1,77); |
SomeRandomBloke | 0:a29a0225f203 | 331 | // OutputMqtt outMqtt = OutputMqtt(); |
SomeRandomBloke | 0:a29a0225f203 | 332 | // outMqtt.setHost( &serverIpAddr ); |
SomeRandomBloke | 0:a29a0225f203 | 333 | |
SomeRandomBloke | 0:a29a0225f203 | 334 | // printf("MAIN: outPachube = %ld\n",(int)&outPachube); |
SomeRandomBloke | 0:a29a0225f203 | 335 | rtr.addOutput( (OutputDef*)&outPachube , 1); |
SomeRandomBloke | 0:a29a0225f203 | 336 | // rtr.addOutput( (OutputDef*)&outMqtt , 2); |
SomeRandomBloke | 0:a29a0225f203 | 337 | |
SomeRandomBloke | 0:a29a0225f203 | 338 | rtr.initRouting(); |
SomeRandomBloke | 0:a29a0225f203 | 339 | |
SomeRandomBloke | 0:a29a0225f203 | 340 | printf("Setup OK\n"); |
SomeRandomBloke | 0:a29a0225f203 | 341 | |
SomeRandomBloke | 0:a29a0225f203 | 342 | printf( "Setting RFM12B ID %d, Band %d Group %d\n",rfm12bId, rfm12bBand, rfm12bGroup); |
SomeRandomBloke | 0:a29a0225f203 | 343 | rfm12b.init(rfm12bId, rfm12bBand, rfm12bGroup ); //id = 2, band 866, group 5 |
SomeRandomBloke | 0:a29a0225f203 | 344 | |
SomeRandomBloke | 0:a29a0225f203 | 345 | printf("Listening...\n"); |
SomeRandomBloke | 0:a29a0225f203 | 346 | |
SomeRandomBloke | 0:a29a0225f203 | 347 | Timer tm; |
SomeRandomBloke | 0:a29a0225f203 | 348 | tm.start(); |
SomeRandomBloke | 0:a29a0225f203 | 349 | |
SomeRandomBloke | 0:a29a0225f203 | 350 | printf("\nHEAP STATS\n"); |
SomeRandomBloke | 0:a29a0225f203 | 351 | __heapstats((__heapprt)fprintf,stderr); |
SomeRandomBloke | 0:a29a0225f203 | 352 | |
SomeRandomBloke | 0:a29a0225f203 | 353 | short dataLen = 0; |
SomeRandomBloke | 0:a29a0225f203 | 354 | uint8_t *rf12Buf; |
SomeRandomBloke | 0:a29a0225f203 | 355 | |
SomeRandomBloke | 0:a29a0225f203 | 356 | // Start receiving data |
SomeRandomBloke | 0:a29a0225f203 | 357 | rfm12b.rf12_recvStart(); |
SomeRandomBloke | 0:a29a0225f203 | 358 | while (true) { |
SomeRandomBloke | 0:a29a0225f203 | 359 | Net::poll(); |
SomeRandomBloke | 0:a29a0225f203 | 360 | |
SomeRandomBloke | 0:a29a0225f203 | 361 | // This is RFM12B specific |
SomeRandomBloke | 0:a29a0225f203 | 362 | if ( rfm12b.available() > 0 ) { |
SomeRandomBloke | 0:a29a0225f203 | 363 | statusLED = 1; |
SomeRandomBloke | 0:a29a0225f203 | 364 | rf12Buf = rfm12b.get_data(); |
SomeRandomBloke | 0:a29a0225f203 | 365 | dataLen = rfm12b.length(); |
SomeRandomBloke | 0:a29a0225f203 | 366 | |
SomeRandomBloke | 0:a29a0225f203 | 367 | if ( rtr.routePayload( rf12Buf, dataLen + 3 ) ) { |
SomeRandomBloke | 0:a29a0225f203 | 368 | // Successfully dealt with a packet |
SomeRandomBloke | 0:a29a0225f203 | 369 | |
SomeRandomBloke | 0:a29a0225f203 | 370 | // Do something different if routed |
SomeRandomBloke | 0:a29a0225f203 | 371 | |
SomeRandomBloke | 0:a29a0225f203 | 372 | } |
SomeRandomBloke | 0:a29a0225f203 | 373 | |
SomeRandomBloke | 0:a29a0225f203 | 374 | // For now, acknowledge everything |
SomeRandomBloke | 0:a29a0225f203 | 375 | |
SomeRandomBloke | 0:a29a0225f203 | 376 | // Send an ack if required |
SomeRandomBloke | 0:a29a0225f203 | 377 | if ((rf12Buf[1] & ~RF12_HDR_MASK) == RF12_HDR_ACK // && |
SomeRandomBloke | 0:a29a0225f203 | 378 | //(config.nodeId & 0x20) == 0 |
SomeRandomBloke | 0:a29a0225f203 | 379 | ) { |
SomeRandomBloke | 0:a29a0225f203 | 380 | |
SomeRandomBloke | 0:a29a0225f203 | 381 | printf("RFM12B -> ack\n"); |
SomeRandomBloke | 0:a29a0225f203 | 382 | |
SomeRandomBloke | 0:a29a0225f203 | 383 | byte addr = rf12Buf[1] & RF12_HDR_MASK; |
SomeRandomBloke | 0:a29a0225f203 | 384 | rfm12b.rf12_sendStart(RF12_HDR_CTL | RF12_HDR_DST | addr, 0, 1); |
SomeRandomBloke | 0:a29a0225f203 | 385 | } |
SomeRandomBloke | 0:a29a0225f203 | 386 | |
SomeRandomBloke | 0:a29a0225f203 | 387 | statusLED = 0; |
SomeRandomBloke | 0:a29a0225f203 | 388 | } |
SomeRandomBloke | 0:a29a0225f203 | 389 | |
SomeRandomBloke | 0:a29a0225f203 | 390 | if (tm.read()>.5) { |
SomeRandomBloke | 0:a29a0225f203 | 391 | heartbeatLED=!heartbeatLED; //Show that we are alive |
SomeRandomBloke | 0:a29a0225f203 | 392 | tm.start(); |
SomeRandomBloke | 0:a29a0225f203 | 393 | } |
SomeRandomBloke | 0:a29a0225f203 | 394 | |
SomeRandomBloke | 0:a29a0225f203 | 395 | linkLED = !ethernet.link(); |
SomeRandomBloke | 0:a29a0225f203 | 396 | |
SomeRandomBloke | 0:a29a0225f203 | 397 | } |
SomeRandomBloke | 0:a29a0225f203 | 398 | |
SomeRandomBloke | 0:a29a0225f203 | 399 | } |
SomeRandomBloke | 0:a29a0225f203 | 400 | |
SomeRandomBloke | 0:a29a0225f203 | 401 | // The End! |