Sensor Hub MCU/WISEAgent
WiseAgentLite is a lite agent library to adapt to a poor performance platform. it is seperated into two layers, RMM layer and MQTT layer. The RMM layer includes the general protocol with RMM server. The MQTT layer is a abstract layer, it is a interface of MQTT on different platform.
Architecture
WiseAgentLite only includes the blue block. The MQTT interface must be ported to adapt to different MCU.
Functions
RMM Layer
void WiseAgent_Init(char *ip, unsigned char *mac, char *parentId, char *deviceName);
Description
- This function initializes the basic information.
Parameters
- ip [POINTER]
- the ip address of this device.
- mac [POINTER]
- the mac hex table of this device.
- parentId [POINTER]
- the parentId of this device.
- deviceName [POINTER]
- the default device name of this device.
Return Values
- None
int WiseAgent_Open(char *server_url, int port, char *username, char *password);
Description
- This function initializes MQTT connection and sends initial messages to RMM. You must call this function before using others.
Parameters
- server_url [POINTER]
- The url of cloud server.
- port [NUMBER]
- port number of cloud server.
- username [POINTER]
- The username of cloud server.
- password [POINTER]
- The password of cloud server.
Return Values
- [0|1] Error or Success.
void WiseAgent_RegisterSensor(char *deviceMac, char *deviceName, WiseAgentInfoSpec *infospec, int count);
Description
- Register the sensor hub. A sensor hub includes many sensors. Those sensors is defined in infospec array.
Parameters
- deviceMac [POINTER]
- Sensor hub mac address.
- deviceName [POINTER]
- Sensor hub name.
- infospec [ARRAY]
- Infospec array.
typedef void (*WiseAgent_SetValue)(WiseAgentData *data);
typedef struct WiseAgentInfoSpec{
WiseDataType type; //WISE_VALUE | WISE_STRING | WISE_BOOL
char *name; //Sensor Name
char *unit;
union {
int value;
char *string;
};
int min;
int max;
char *resourcetype; //IPSO unit
WiseAgent_SetValue setValue;
} WiseAgentInfoSpec;
- count [NUMBER]
- Infospec array size.
Return Values
- None.
void WiseAgent_Write(char *deviceMac, WiseAgentData* data, int count);
Description
- Update the value of sensors in sensor hub.
Parameters
- deviceMac [POINTER]
- Sensor hub mac address.
- data [ARRAY]
- Value array
typedef struct WiseAgentData{
WiseDataType type;
char *name;
union {
int value;
char *string;
};
} WiseAgentData;
- count [NUMBER]
- Array size
Return Values
- None.
void WiseAgent_Close();
Description
- Disconnect
Parameters
- None
Return Values
- None
MQTT Layer
int WiseMQTT_Open(char *server_url, char *device_mac, char *will_topic, char *will_message, /*connectionlost_callback*/);
Description
- Open MQTT connect
Parameters
- server_url [POINTER]
- MQTT broker url
- device_mac [POINTER]
- use mac to be a client id for MQTT
- will_topic and will_message [POINTER]
- will message
- connectionlost_callback [FUNCTION]
- not implement
Return Values
- [0|1] Error or Success.
int WiseMQTT_Read(char *topic,/*receive_callback*/);
Description
- Subscribe a topic into MQTT broker.
Parameters
- topic [POINTER]
- topic string
- receive_callback [FUNCTION]
- not implement
Return Values
- [0|1] Error or Success.
Callback Function
void (*receive_cb)(const char *topic, const void *payload);
- topic [POINTER]
- topic string
- payload [POINTER]
- receive message
void WiseMQTT_Write(char *topic,char *message,/*delivery_callback*/);
Description
- Publish a message to the topic of MQTT broker.
Parameters
- topic [POINTER]
- topic string
- message [POINTER]
- message string
- delivery_callback [FUNCTION]
- not implement
Return Values
- None
void WiseMQTT_Close();
Description
- Disconnect
Parameters
- None
Return Values
- None
Sensors
- Temperature
WiseAgentInfoSpec infospec[] = {
{
WISE_VALUE, //type
"Temperature", //name
"Cel", //unit
0, //value[union]
-100, //min
200, //max
"ucum.Cel" //resourceType
}
};
WiseAgentData data[] = {
{
WISE_VALUE, //type
"Temperature", //name
100 //value
}
};
- GPI
WiseAgentInfoSpec infospec[] = {
{
WISE_BOOL, //type
"GPI", //name
"", //unit
0, //value[union]
0, //min
1, //max
"gpio.din" //resourceType
}
};
WiseAgentData data[] = {
{
WISE_BOOL, //type
"GPI", //name
1 //value
}
};
- GPO
WiseAgentInfoSpec infospec[] = {
{
WISE_BOOL, //type
"GPO", //name
"", //unit
0, //value[union]
0, //min
1, //max
"gpio.dout" //resourceType
}
};
WiseAgentData data[] = {
{
WISE_BOOL, //type
"GPO", //name
1 //value
}
};
- CO2
WiseAgentInfoSpec infospec[] = {
{
WISE_VALUE, //type
"CO2", //name
"ppm", //unit
0, //value[union]
0, //min
10000, //max
"ucum.ppm" //resourceType
}
};
WiseAgentData data[] = {
{
WISE_VALUE, //type
"CO2", //name
1000 //value
}
};
- CO
WiseAgentInfoSpec infospec[] = {
{
WISE_VALUE, //type
"CO", //name
"ppm", //unit
0, //value[union]
0, //min
10000, //max
"ucum.ppm" //resourceType
}
};
WiseAgentData data[] = {
{
WISE_VALUE, //type
"CO", //name
1000 //value
}
};
- Sonar
WiseAgentInfoSpec infospec[] = {
{
WISE_VALUE, //type
"Sonar", //name
"cm", //unit
0, //value[union]
0, //min
600, //max
"ucum.cm" //resourceType
}
};
WiseAgentData data[] = {
{
WISE_VALUE, //type
"Sonar", //name
200 //value
}
};
- G-Sensor
WiseAgentInfoSpec infospec[] = {
{
WISE_STRING, //type
"GSensor", //name
"g", //unit
"[{n:x,v:0},{n:y,v:0},{n:z,v:0}]", //value[union]
0, //min
1000, //max
"ucum.g" //resourceType
}
};
WiseAgentData data[] = {
{
WISE_STRING, //type
"GSensor", //name
"[{n:x,v:50},{n:y,v:70},{n:z,v:100}]" //value
}
};
- Magnetic
WiseAgentInfoSpec infospec[] = {
{
WISE_STRING, //type
"Magnetic", //name
"G", //unit
"[{n:x,v:0},{n:y,v:0},{n:z,v:0}]", //value[union]
0, //min
1000, //max
"ucum.G" //resourceType
}
};
WiseAgentData data[] = {
{
WISE_STRING, //type
"Magnetic", //name
"[{n:x,v:50},{n:y,v:70},{n:z,v:100}]" //value
}
};
- Light
WiseAgentInfoSpec infospec[] = {
{
WISE_VALUE, //type
"Light", //name
"lx", //unit
0, //value[union]
0, //min
500, //max
"ucum.lx" //resourceType
}
};
WiseAgentData data[] = {
{
WISE_VALUE, //type
"Light", //name
200 //value
}
};
- Humidity
WiseAgentInfoSpec infospec[] = {
{
WISE_VALUE, //type
"Humidity", //name
"%", //unit
0, //value[union]
0, //min
100, //max
"ucum.%" //resourceType
}
};
WiseAgentData data[] = {
{
WISE_VALUE, //type
"Humidity", //name
55 //value
}
};
Sample Code
void SetGPIO_1(WiseAgentData *data) {
printf("(GPIO 1) = %d\r\n",data->value);
}
void SetGPIO_2(WiseAgentData *data) {
printf("(GPIO 2) = %d\r\n",data->value);
}
WiseAgentInfoSpec infospec[] = {
{
WISE_VALUE, //type
"Temperature", //name
"Cel", //unit
0, //value[union]
-100, //min
200, //max
"ucum.Cel" //resourceType
},
{
WISE_VALUE, //type
"Humidity", //name
"%", //unit
0, //value[union]
0, //min
100, //max
"ucum.%" //resourceType
},
{
WISE_IO_BOOL,
"GPIO1",
"",
0,
0,
1,
"",
SetGPIO_1
},
{
WISE_IO_BOOL,
"GPIO2",
"",
0,
0,
1,
"",
SetGPIO_2
}
};
WiseAgentData data[] = {
{
WISE_VALUE, //type
"Temperature", //name
100 //value
},
{
WISE_VALUE, //type
"Humidity", //name
55 //value
}
};
#define SERVER_ADDRESS "rmm.wise-paas.com"
//initial
WiseAgent_Init(ip, mac);
if(WiseAgent_Open(SERVER_ADDRESS) == 0) {
//Error and return
return;
} else {
//Register a SenHub “OnBoard”
WiseAgent_RegisterSensor(SN_MACADDRESS, "OnBoard", infospec, 2);
}
for(;;) {
//Get Sensor Data from internal interface.
GetSensorData(&Temperature, &Humidity);
//Update data
data[0].value = Temperature;
data[1].value = Humidity;
WiseAgent_Write(SN_MACADDRESS, data, 2);
//delay 3s
usleep(3000);
}