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:
4:d460406ac780
Reduced debug output

Who changed what in which revision?

UserRevisionLine numberNew contents of line
SomeRandomBloke 0:a29a0225f203 1 /** IoT Gateway Routing for incoming messages
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 #ifndef _IOTROUTING_H
SomeRandomBloke 0:a29a0225f203 33 #define _IOTROUTING_H
SomeRandomBloke 0:a29a0225f203 34
SomeRandomBloke 5:0dbc27a7af55 35 #include "iotgateway.h"
SomeRandomBloke 0:a29a0225f203 36 #include "mbed.h"
SomeRandomBloke 0:a29a0225f203 37 #include "HTTPClient.h"
SomeRandomBloke 0:a29a0225f203 38 #include "OutputDef.h"
SomeRandomBloke 0:a29a0225f203 39 #include "OutputPachube.h"
SomeRandomBloke 0:a29a0225f203 40 #include "OutputMqtt.h"
SomeRandomBloke 4:d460406ac780 41 #include "OutputEmonCms.h"
SomeRandomBloke 4:d460406ac780 42 #include "OutputSenSe.h"
SomeRandomBloke 0:a29a0225f203 43 #include "PayloadV1.h"
SomeRandomBloke 0:a29a0225f203 44 #include "PayloadV2.h"
SomeRandomBloke 0:a29a0225f203 45 #include "PayloadSimple.h"
SomeRandomBloke 0:a29a0225f203 46 #include <vector>
SomeRandomBloke 0:a29a0225f203 47
SomeRandomBloke 0:a29a0225f203 48 #define STATUS_PAYLOAD_VMASK 0xf8
SomeRandomBloke 0:a29a0225f203 49 #define STATUS_PAYLOAD_VERSION 0x06
SomeRandomBloke 0:a29a0225f203 50 #define STATUS_PAYLOAD_V1 0x00
SomeRandomBloke 0:a29a0225f203 51 #define STATUS_PAYLOAD_V2 0x02
SomeRandomBloke 0:a29a0225f203 52 #define STATUS_LOW_BATTERY 0x01
SomeRandomBloke 0:a29a0225f203 53
SomeRandomBloke 0:a29a0225f203 54 #define PAYLOAD_TYPE_UNKNOWN 99
SomeRandomBloke 0:a29a0225f203 55 #define PAYLOAD_TYPE_SIMPLE 0
SomeRandomBloke 0:a29a0225f203 56 #define PAYLOAD_TYPE_V1 1
SomeRandomBloke 0:a29a0225f203 57 #define PAYLOAD_TYPE_V2 2
SomeRandomBloke 0:a29a0225f203 58
SomeRandomBloke 0:a29a0225f203 59 #define OUTPUT_UNKNOWN 0
SomeRandomBloke 0:a29a0225f203 60 #define OUTPUT_TYPE_PACHUBE 1
SomeRandomBloke 0:a29a0225f203 61 #define OUTPUT_TYPE_MQTT 2
SomeRandomBloke 4:d460406ac780 62 #define OUTPUT_TYPE_EMONCMS 3
SomeRandomBloke 4:d460406ac780 63 #define OUTPUT_TYPE_SENSE 4
SomeRandomBloke 0:a29a0225f203 64
SomeRandomBloke 0:a29a0225f203 65 /** Routing definition for a reading
SomeRandomBloke 0:a29a0225f203 66 * Input source, node ID, sensor ID, output
SomeRandomBloke 0:a29a0225f203 67 * default is OuptutDef but casts to Pachube or MQTT
SomeRandomBloke 0:a29a0225f203 68 */
SomeRandomBloke 0:a29a0225f203 69 typedef struct {
SomeRandomBloke 0:a29a0225f203 70 short nodeId;
SomeRandomBloke 0:a29a0225f203 71 short sensorId;
SomeRandomBloke 0:a29a0225f203 72 float factor;
SomeRandomBloke 0:a29a0225f203 73 byte outType;
SomeRandomBloke 0:a29a0225f203 74 OutputDef *output;
SomeRandomBloke 0:a29a0225f203 75 char *param1;
SomeRandomBloke 0:a29a0225f203 76 char *param2;
SomeRandomBloke 0:a29a0225f203 77 char *param3;
SomeRandomBloke 0:a29a0225f203 78 char *param4;
SomeRandomBloke 0:a29a0225f203 79 } __attribute__((packed)) PayloadRouting;
SomeRandomBloke 0:a29a0225f203 80
SomeRandomBloke 0:a29a0225f203 81 /** Node definition
SomeRandomBloke 0:a29a0225f203 82 */
SomeRandomBloke 0:a29a0225f203 83 typedef struct {
SomeRandomBloke 0:a29a0225f203 84 uint8_t groupId;
SomeRandomBloke 0:a29a0225f203 85 uint8_t nodeId;
SomeRandomBloke 0:a29a0225f203 86 uint8_t length;
SomeRandomBloke 0:a29a0225f203 87 uint8_t type;
SomeRandomBloke 0:a29a0225f203 88 } __attribute__((packed)) IoTNodeDef;
SomeRandomBloke 0:a29a0225f203 89
SomeRandomBloke 0:a29a0225f203 90
SomeRandomBloke 0:a29a0225f203 91 /** Main IoT Gateway RFM12B payload routing class
SomeRandomBloke 0:a29a0225f203 92 */
SomeRandomBloke 0:a29a0225f203 93 class IoTRouting {
SomeRandomBloke 0:a29a0225f203 94 public:
SomeRandomBloke 0:a29a0225f203 95 /** Default Constructor
SomeRandomBloke 0:a29a0225f203 96 */
SomeRandomBloke 0:a29a0225f203 97 IoTRouting();
SomeRandomBloke 0:a29a0225f203 98
SomeRandomBloke 0:a29a0225f203 99 /** Initialise routing class
SomeRandomBloke 0:a29a0225f203 100 */
SomeRandomBloke 0:a29a0225f203 101 void initRouting();
SomeRandomBloke 0:a29a0225f203 102
SomeRandomBloke 0:a29a0225f203 103 /** Write a list of nodes to IOTNODE.TXT
SomeRandomBloke 0:a29a0225f203 104 * Format is: group,node,length,type
SomeRandomBloke 0:a29a0225f203 105 * Where:
SomeRandomBloke 0:a29a0225f203 106 * group, node, length are first 3 bytes of received packet
SomeRandomBloke 0:a29a0225f203 107 * type is 99 for unknown, 0 for simple, 1 for Type 1, 2 for Type 2
SomeRandomBloke 0:a29a0225f203 108 */
SomeRandomBloke 0:a29a0225f203 109 bool writeNodeList();
SomeRandomBloke 0:a29a0225f203 110
SomeRandomBloke 0:a29a0225f203 111 /** Write main routing list to file IOTRTR.TXT
SomeRandomBloke 0:a29a0225f203 112 * Format is: nodeId,sensorId,factor,outputType,param1,param2,param3,param4
SomeRandomBloke 0:a29a0225f203 113 * Where:
SomeRandomBloke 0:a29a0225f203 114 * nodeId is the Node ID of the node to the sensor is attached to
SomeRandomBloke 0:a29a0225f203 115 * sensorId is the ID of the sensor to define
SomeRandomBloke 0:a29a0225f203 116 * factor is a conversion factor to convert from integer to real value. can be 100.0,10.0,1.0,0.1,0.01,0.001. For no conversion use 1.0
SomeRandomBloke 0:a29a0225f203 117 * outputType is the type of output, 1 is Pachube, 2 is MQTT
SomeRandomBloke 0:a29a0225f203 118 * param1 is Pachube feed number or MQTT topic
SomeRandomBloke 0:a29a0225f203 119 * param2 is Pachube datastream number
SomeRandomBloke 0:a29a0225f203 120 * param3 and 4 are currently unused
SomeRandomBloke 0:a29a0225f203 121 */
SomeRandomBloke 0:a29a0225f203 122 bool writeRoutingList();
SomeRandomBloke 0:a29a0225f203 123
SomeRandomBloke 0:a29a0225f203 124 /** Add a node to the nodelist
SomeRandomBloke 0:a29a0225f203 125 *
SomeRandomBloke 0:a29a0225f203 126 * @param groupId Group ID, byte 0 of received packet
SomeRandomBloke 0:a29a0225f203 127 * @param nodeId Node ID, byte 1
SomeRandomBloke 0:a29a0225f203 128 * @param length Length of payload, byte 2
SomeRandomBloke 0:a29a0225f203 129 * @param type Type of payload, simple, v1, v2 or undefined
SomeRandomBloke 0:a29a0225f203 130 */
SomeRandomBloke 0:a29a0225f203 131 void addNodeToList(uint8_t groupId, uint8_t nodeId, uint8_t length, uint8_t type );
SomeRandomBloke 0:a29a0225f203 132
SomeRandomBloke 0:a29a0225f203 133 /** Add an item to routing list
SomeRandomBloke 0:a29a0225f203 134 *
SomeRandomBloke 0:a29a0225f203 135 * @param nodeId ID of the node payload came from
SomeRandomBloke 0:a29a0225f203 136 * @param sensorId ID of the sensor within the node
SomeRandomBloke 0:a29a0225f203 137 * @param factor Sensor Reading conversion factor, 100, 10, 1, 0.1, 0.01, 0.001 etc
SomeRandomBloke 5:0dbc27a7af55 138 * @param outType Output type, 1 = Pachube, 2 = MQTT, 3 = emonCMS, 4 = Sen.Se
SomeRandomBloke 0:a29a0225f203 139 * @param param1 Parameter 1
SomeRandomBloke 0:a29a0225f203 140 * @param param2 Parameter 2
SomeRandomBloke 0:a29a0225f203 141 * @param param3 Parameter 3
SomeRandomBloke 0:a29a0225f203 142 * @param param4 Parameter 4
SomeRandomBloke 0:a29a0225f203 143 */
SomeRandomBloke 0:a29a0225f203 144 void addRoutingToList( short nodeId, short sensorId, float factor, byte outType,
SomeRandomBloke 0:a29a0225f203 145 char *param1, char *param2, char *param3, char *param4 );
SomeRandomBloke 0:a29a0225f203 146
SomeRandomBloke 0:a29a0225f203 147 /** get Payload type
SomeRandomBloke 0:a29a0225f203 148 *
SomeRandomBloke 0:a29a0225f203 149 * @param data Pointer to start of payload data
SomeRandomBloke 0:a29a0225f203 150 * @param dataLen Length of payload data
SomeRandomBloke 0:a29a0225f203 151 * @returns payload type, either simple, v1, v2 or unknown
SomeRandomBloke 0:a29a0225f203 152 */
SomeRandomBloke 0:a29a0225f203 153 short getPayloadType( uint8_t *data, short dataLen );
SomeRandomBloke 0:a29a0225f203 154
SomeRandomBloke 0:a29a0225f203 155 /** Get routing data for specified node/sensor combination
SomeRandomBloke 0:a29a0225f203 156 *
SomeRandomBloke 0:a29a0225f203 157 * @param nodeId The ID of the node
SomeRandomBloke 0:a29a0225f203 158 * @param sensorNum The ID of the sensor
SomeRandomBloke 0:a29a0225f203 159 * @returns pointer to routing data
SomeRandomBloke 0:a29a0225f203 160 */
SomeRandomBloke 0:a29a0225f203 161 PayloadRouting* getRouting( short nodeId, short sensorNum );
SomeRandomBloke 0:a29a0225f203 162
SomeRandomBloke 0:a29a0225f203 163 /** Add output definition to output list
SomeRandomBloke 0:a29a0225f203 164 *
SomeRandomBloke 0:a29a0225f203 165 * @param output Pointer to output object
SomeRandomBloke 0:a29a0225f203 166 * @param outType Numeric value for the output type
SomeRandomBloke 0:a29a0225f203 167 */
SomeRandomBloke 0:a29a0225f203 168 void addOutput( OutputDef *output, short outType );
SomeRandomBloke 0:a29a0225f203 169
SomeRandomBloke 0:a29a0225f203 170 /** Main payload routing function
SomeRandomBloke 0:a29a0225f203 171 *
SomeRandomBloke 0:a29a0225f203 172 * @param payload Pointer to payload data
SomeRandomBloke 0:a29a0225f203 173 * @param payloadLen Length of th epayload
SomeRandomBloke 0:a29a0225f203 174 * @returns flag to indicate payload routed to output or not.
SomeRandomBloke 0:a29a0225f203 175 */
SomeRandomBloke 0:a29a0225f203 176 bool routePayload( uint8_t *payload, short payloadLen );
SomeRandomBloke 0:a29a0225f203 177
SomeRandomBloke 0:a29a0225f203 178 private:
SomeRandomBloke 0:a29a0225f203 179 int sendCount;
SomeRandomBloke 0:a29a0225f203 180 OutputDef *pachubeOutput;
SomeRandomBloke 0:a29a0225f203 181 OutputDef *mqttOutput;
SomeRandomBloke 4:d460406ac780 182 OutputDef *emonCmsOutput;
SomeRandomBloke 4:d460406ac780 183 OutputDef *senSeOutput;
SomeRandomBloke 0:a29a0225f203 184
SomeRandomBloke 0:a29a0225f203 185 std::vector<PayloadRouting*> _routing;
SomeRandomBloke 0:a29a0225f203 186 std::vector<IoTNodeDef*> _nodeDefList;
SomeRandomBloke 0:a29a0225f203 187
SomeRandomBloke 0:a29a0225f203 188 };
SomeRandomBloke 0:a29a0225f203 189
SomeRandomBloke 0:a29a0225f203 190 #endif /* _IOTROUTING_H */