Difference between revisions of "Sensor Hub MCU/WISEAgent"

From ESS-WIKI
Jump to: navigation, search
(RMM Layer)
 
(17 intermediate revisions by one other user not shown)
Line 1: Line 1:
[[MCU/WISEAgent|WiseAgentLite]] is a lite agent library to adapt to a poor performance platform. We separate it into two layers, RMM protocol layer and MQTT protocol layer.
+
[[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==
 
==Architecture==
 
WiseAgentLite only includes the blue block. The MQTT interface must be ported to adapt to different MCU.
 
WiseAgentLite only includes the blue block. The MQTT interface must be ported to adapt to different MCU.
 +
[[File:Arch.png|none|right]]
 +
 +
 +
==Functions==
 +
====RMM Layer====
 +
<br>
 +
<source lang="C">void WiseAgent_Init(char *ip, unsigned char *mac, char *parentId, char *deviceName);</source>
 +
''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
 +
<br>
 +
<source lang="C">int WiseAgent_Open(char *server_url, int port, char *username, char *password);</source>
 +
''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.
 +
<br>
 +
<source lang="C">void WiseAgent_RegisterSensor(char *deviceMac, char *deviceName, WiseAgentInfoSpec *infospec, int count);</source>
 +
''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.
 +
<source lang="C">
 +
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;
 +
</source>
 +
* count [NUMBER]
 +
** Infospec array size.
 +
''Return Values''
 +
* None.
 +
<br>
 +
<source lang="C">void WiseAgent_Write(char *deviceMac, WiseAgentData* data, int count);</source>
 +
''Description''
 +
* Update the value of sensors in sensor hub.
 +
''Parameters''
 +
* deviceMac [POINTER]
 +
** Sensor hub mac address.
 +
* data [ARRAY]
 +
** Value array
 +
<source lang="C">
 +
typedef struct WiseAgentData{
 +
  WiseDataType type;
 +
  char *name;
 +
  union {
 +
    int value;
 +
    char *string;
 +
  };
 +
} WiseAgentData;
 +
</source>
 +
* count [NUMBER]
 +
** Array size
 +
''Return Values''
 +
* None.
 +
<br>
 +
<source lang="C">void WiseAgent_Close();</source>
 +
''Description''
 +
* Disconnect
 +
''Parameters''
 +
* None
 +
''Return Values''
 +
* None
 +
<br>
 +
 +
====MQTT Layer====
 +
<br>
 +
<source lang="C">int WiseMQTT_Open(char *server_url, char *device_mac, char *will_topic, char *will_message, /*connectionlost_callback*/);</source>
 +
''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.
 +
<br>
 +
<source lang="C">int WiseMQTT_Read(char *topic,/*receive_callback*/);</source>
 +
''Description''
 +
* Subscribe a topic into MQTT broker.
 +
''Parameters''
 +
* topic [POINTER]
 +
** topic string
 +
* receive_callback [FUNCTION]
 +
** not implement
 +
''Return Values''
 +
* [0|1] Error or Success.
 +
<br>
 +
''Callback Function''
 +
<source lang="C">void (*receive_cb)(const char *topic, const void *payload);</source>
 +
* topic [POINTER]
 +
** topic string
 +
* payload [POINTER]
 +
** receive message
 +
<br>
 +
<source lang="C">void WiseMQTT_Write(char *topic,char *message,/*delivery_callback*/);</source>
 +
''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
 +
<br>
 +
<source lang="C">void WiseMQTT_Close();</source>
 +
''Description''
 +
* Disconnect
 +
''Parameters''
 +
* None
 +
''Return Values''
 +
* None
 +
<br>
 +
 +
==Sensors==
 +
* Temperature
 +
<source lang="C">
 +
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
 +
}
 +
};
 +
</source>
 +
* GPI
 +
<source lang="C">
 +
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
 +
}
 +
};
 +
</source>
 +
* GPO
 +
<source lang="C">
 +
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
 +
}
 +
};
 +
</source>
 +
* CO2
 +
<source lang="C">
 +
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
 +
}
 +
};
 +
</source>
 +
* CO
 +
<source lang="C">
 +
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
 +
}
 +
};
 +
</source>
 +
* Sonar
 +
<source lang="C">
 +
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
 +
}
 +
};
 +
</source>
 +
* G-Sensor
 +
<source lang="C">
 +
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
 +
}
 +
};
 +
</source>
 +
* Magnetic
 +
<source lang="C">
 +
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
 +
}
 +
};
 +
</source>
 +
* Light
 +
<source lang="C">
 +
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
 +
}
 +
};
 +
</source>
 +
* Humidity
 +
<source lang="C">
 +
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
 +
}
 +
};
 +
</source>
 +
 +
 +
==Sample Code==
 +
<source lang="C">
 +
 +
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);
 +
}
 +
</source>

Latest revision as of 09:10, 10 September 2016

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.

Arch.png


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);
}