Client for the DeviceHubNet gateway. (You need the gateway SW as well!)

Dependents:   DeviceHubNet_DEMO

Committer:
gume
Date:
Tue Mar 28 01:33:39 2017 +0000
Revision:
0:093f1cb20c52
Child:
1:5dca66437dd1
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gume 0:093f1cb20c52 1 #ifndef __DEVICEHUBNET_H__
gume 0:093f1cb20c52 2 #define __DEVICEHUBNET_H__
gume 0:093f1cb20c52 3
gume 0:093f1cb20c52 4 #include <mbed.h>
gume 0:093f1cb20c52 5 #include "RF24.h"
gume 0:093f1cb20c52 6 #include <list>
gume 0:093f1cb20c52 7 #include <map>
gume 0:093f1cb20c52 8 #include <string>
gume 0:093f1cb20c52 9
gume 0:093f1cb20c52 10 #define BROADCAST_ADDRESS_LL 0x3333333333LL
gume 0:093f1cb20c52 11 #define NETWORK_PREFIX 0x424D45L // All network node has a common 3 byte address prefix
gume 0:093f1cb20c52 12
gume 0:093f1cb20c52 13 #define PACKET_PING 1
gume 0:093f1cb20c52 14 #define PACKET_PONG 2
gume 0:093f1cb20c52 15 #define PACKET_DATA 3
gume 0:093f1cb20c52 16 #define PACKET_REGISTER 4
gume 0:093f1cb20c52 17 #define PACKET_UNREGISTER 5
gume 0:093f1cb20c52 18 #define PACKET_REGISTERERROR 6
gume 0:093f1cb20c52 19 #define PACKET_ERROR 15
gume 0:093f1cb20c52 20
gume 0:093f1cb20c52 21
gume 0:093f1cb20c52 22 struct __attribute__((__packed__)) Message {
gume 0:093f1cb20c52 23 uint16_t sourceAddress;
gume 0:093f1cb20c52 24 uint8_t type;
gume 0:093f1cb20c52 25 uint8_t counter;
gume 0:093f1cb20c52 26 uint8_t payload[28];
gume 0:093f1cb20c52 27 };
gume 0:093f1cb20c52 28
gume 0:093f1cb20c52 29 struct __attribute__((__packed__)) MsgRegister {
gume 0:093f1cb20c52 30 uint8_t rType; // 1: Project, 2: Device, 3: Sensor, 4: Actuator
gume 0:093f1cb20c52 31 uint8_t id[27];
gume 0:093f1cb20c52 32 };
gume 0:093f1cb20c52 33
gume 0:093f1cb20c52 34 struct __attribute__((__packed__)) MsgData {
gume 0:093f1cb20c52 35 uint16_t sensorId;
gume 0:093f1cb20c52 36 uint8_t dType; // 0: Digital, 1: Analog
gume 0:093f1cb20c52 37 union {
gume 0:093f1cb20c52 38 uint8_t ddata;
gume 0:093f1cb20c52 39 float adata;
gume 0:093f1cb20c52 40 } data;
gume 0:093f1cb20c52 41 };
gume 0:093f1cb20c52 42
gume 0:093f1cb20c52 43
gume 0:093f1cb20c52 44 class DeviceHubNet
gume 0:093f1cb20c52 45 {
gume 0:093f1cb20c52 46 private:
gume 0:093f1cb20c52 47 uint32_t projectId;
gume 0:093f1cb20c52 48 uint8_t apiKey[16];
gume 0:093f1cb20c52 49 uint8_t deviceId[16];
gume 0:093f1cb20c52 50 uint16_t nextSId; // Increasing sensor IDs
gume 0:093f1cb20c52 51
gume 0:093f1cb20c52 52 map<uint16_t, string> sensors;
gume 0:093f1cb20c52 53 map<uint16_t, string> actuators;
gume 0:093f1cb20c52 54 map<uint16_t, void*> actuator_cbs;
gume 0:093f1cb20c52 55 map<uint16_t, uint8_t> actuator_types;
gume 0:093f1cb20c52 56
gume 0:093f1cb20c52 57 RF24 *radio;
gume 0:093f1cb20c52 58 uint16_t nodeAddress;
gume 0:093f1cb20c52 59 uint8_t msgCounter;
gume 0:093f1cb20c52 60
gume 0:093f1cb20c52 61 int readHex(char *hexStr, uint8_t *hex);
gume 0:093f1cb20c52 62 bool sendData(uint8_t type, uint8_t *data, uint8_t len);
gume 0:093f1cb20c52 63 uint64_t getFullAddress();
gume 0:093f1cb20c52 64 uint64_t getGWAddress();
gume 0:093f1cb20c52 65
gume 0:093f1cb20c52 66 void registerProject();
gume 0:093f1cb20c52 67 void registerDevice();
gume 0:093f1cb20c52 68
gume 0:093f1cb20c52 69 public:
gume 0:093f1cb20c52 70 DeviceHubNet(uint32_t projectId, uint8_t* apiKey, uint8_t* deviceId);
gume 0:093f1cb20c52 71 DeviceHubNet(uint32_t projectId, char* apiKeyStr, char* deviceIdStr);
gume 0:093f1cb20c52 72 virtual ~DeviceHubNet() {};
gume 0:093f1cb20c52 73
gume 0:093f1cb20c52 74 bool radioPinConfig(PinName mosi, PinName miso, PinName sck, PinName cs, PinName ce);
gume 0:093f1cb20c52 75 bool radioConfig(uint16_t address, uint8_t channel);
gume 0:093f1cb20c52 76 void radioDump();
gume 0:093f1cb20c52 77
gume 0:093f1cb20c52 78 void processMsgs();
gume 0:093f1cb20c52 79
gume 0:093f1cb20c52 80 uint16_t registerSensor(char* sensorName);
gume 0:093f1cb20c52 81 uint16_t registerActuator(char *actuatorName, uint8_t type, void (*onReceive)(uint8_t, uint8_t, float));
gume 0:093f1cb20c52 82 void reRegisterSensor(uint16_t sensorId);
gume 0:093f1cb20c52 83 void reRegisterActuator(uint16_t actuatorId);
gume 0:093f1cb20c52 84
gume 0:093f1cb20c52 85 bool sendDigitalData(uint16_t sensorId, uint8_t data);
gume 0:093f1cb20c52 86 bool sendAnalogData(uint16_t sensorId, float data);
gume 0:093f1cb20c52 87 };
gume 0:093f1cb20c52 88
gume 0:093f1cb20c52 89 #endif