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

Dependents:   DeviceHubNet_DEMO

Revision:
0:093f1cb20c52
Child:
1:5dca66437dd1
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DeviceHubNet.h	Tue Mar 28 01:33:39 2017 +0000
@@ -0,0 +1,89 @@
+#ifndef __DEVICEHUBNET_H__
+#define __DEVICEHUBNET_H__
+
+#include <mbed.h>
+#include "RF24.h"
+#include <list>
+#include <map>
+#include <string>
+
+#define BROADCAST_ADDRESS_LL 0x3333333333LL
+#define NETWORK_PREFIX 0x424D45L    // All network node has a common 3 byte address prefix
+
+#define PACKET_PING 1
+#define PACKET_PONG 2
+#define PACKET_DATA 3
+#define PACKET_REGISTER 4
+#define PACKET_UNREGISTER 5
+#define PACKET_REGISTERERROR 6
+#define PACKET_ERROR 15
+
+
+struct __attribute__((__packed__)) Message {
+    uint16_t sourceAddress;
+    uint8_t type;
+    uint8_t counter;
+    uint8_t payload[28];
+};
+
+struct __attribute__((__packed__)) MsgRegister {
+    uint8_t rType;  // 1: Project, 2: Device, 3: Sensor, 4: Actuator
+    uint8_t id[27];
+};
+
+struct __attribute__((__packed__)) MsgData {
+    uint16_t sensorId;
+    uint8_t dType;  // 0: Digital, 1: Analog
+    union {
+        uint8_t ddata;
+        float adata;
+    } data;
+};
+
+
+class DeviceHubNet
+{
+private:
+    uint32_t projectId;
+    uint8_t apiKey[16];
+    uint8_t deviceId[16];
+    uint16_t nextSId;       // Increasing sensor IDs
+
+    map<uint16_t, string> sensors;
+    map<uint16_t, string> actuators;
+    map<uint16_t, void*> actuator_cbs;
+    map<uint16_t, uint8_t> actuator_types;
+
+    RF24 *radio;
+    uint16_t nodeAddress;
+    uint8_t msgCounter;
+
+    int readHex(char *hexStr, uint8_t *hex);
+    bool sendData(uint8_t type, uint8_t *data, uint8_t len);
+    uint64_t getFullAddress();
+    uint64_t getGWAddress();
+    
+    void registerProject();
+    void registerDevice();
+
+public:
+    DeviceHubNet(uint32_t projectId, uint8_t* apiKey, uint8_t* deviceId);
+    DeviceHubNet(uint32_t projectId, char* apiKeyStr, char* deviceIdStr);
+    virtual ~DeviceHubNet() {};
+
+    bool radioPinConfig(PinName mosi, PinName miso, PinName sck, PinName cs, PinName ce);
+    bool radioConfig(uint16_t address, uint8_t channel);
+    void radioDump();
+
+    void processMsgs();
+
+    uint16_t registerSensor(char* sensorName);
+    uint16_t registerActuator(char *actuatorName, uint8_t type, void (*onReceive)(uint8_t, uint8_t, float));
+    void reRegisterSensor(uint16_t sensorId);
+    void reRegisterActuator(uint16_t actuatorId);
+
+    bool sendDigitalData(uint16_t sensorId, uint8_t data);
+    bool sendAnalogData(uint16_t sensorId, float data);
+};
+
+#endif
\ No newline at end of file