最近,我一直在使用 Texas Instrument 的 MSP430F5529 微控制器和 CC3100 网络处理器进行物联网项目。为了进行评估,我使用了 MSP430F5529 发射台和CC3100 助推器。我正在尝试让设备连接到云。我已经成功实现了连接到www.openweathermap.org的 CC3100获取天气示例应用程序。这是来自CC3100 SDK 示例应用程序的示例。该程序成功接收并响应来自www.openweathermap.org网站。应用程序使用 GET 方法从网站发出请求。
我还成功地针对www.mocky.io测试了代码。设备收到状态代码 200 OK 响应。但是当我针对requestb.in测试站点进行测试时,我既没有收到 408 超时错误响应代码,也没有收到 302 的 URL 重定向响应代码。
#define WEATHER_SERVER "api.openweathermap.org"
#define TEST_SERVER "requestb.in"
//#define TEST_SERVER "www.mocky.io"
#define PREFIX_BUFFER "GET /data/2.5/weather?q="
#define POST_BUFFER "&APPID=xxxxxxxxxxxxxxxxxx&mode=xml&units=imperial HTTP/1.1\r\nHost:api.openweathermap.org\r\nAccept: */"
#define POST_BUFFER2 "*\r\n\r\n"
#define PREFIX_BUFFER_TEST "GET /1m75pgt1"
#define POST_BUFFER_TEST_1 " HTTP/1.1\r\nHost:requestb.in\r\nAccept: */"
#define POST_BUFFER_TEST_2 "\r\n\r\n"*
//#define PREFIX_BUFFER_TEST "GET /v2/5967a65d1100007d16b6c2b4"
//#define POST_BUFFER_TEST_1 " HTTP/1.1\r\nHost:www.mocky.io\r\nAccept: */"
//#define POST_BUFFER_TEST_2 "\r\n\r\n"*
下面是主要的,其中包括一些设置条件。为简洁起见,删除了一些错误处理代码。
int main(int argc, char** argv)
{
_i32 retVal = -1;
retVal = initializeAppVariables();
ASSERT_ON_ERROR(retVal);
/* Stop WDT and initialize the system-clock of the MCU */
stopWDT();
initClk();
/*
* Following function configures the device to default state by cleaning
* the persistent settings stored in NVMEM (viz. connection profiles &
* policies, power policy etc)
*
* Applications may choose to skip this step if the developer is sure
* that the device is in its default state at start of application
*
* Note that all profiles and persistent settings that were done on the
* device will be lost
*/
retVal = configureSimpleLinkToDefaultState();
/*
* Assumption is that the device is configured in station mode already
* and it is in its default state
*/
retVal = sl_Start(0, 0, 0);
/* Connecting to WLAN AP */
retVal = establishConnectionWithAP();
retVal = getCredentials();
retVal = disconnectFromAP();
return 0;
}
下面是调用获取数据的getCredentials()代码。
<!-- language: lang-c -->
static _i32 getCredentials()
{
_i32 retVal = -1;
pal_Strcpy((char *)g_DeviceData.HostName, TEST_SERVER);
retVal = getHostIP_Device();
g_DeviceData.SockID = createConnection();
ASSERT_ON_ERROR(g_DeviceData.SockID);
retVal = getData();
ASSERT_ON_ERROR(retVal);
retVal = sl_Close(g_DeviceData.SockID);
ASSERT_ON_ERROR(retVal);
return 0;
}
下面是我收到错误的getdata()函数。
/*!
\brief This function Obtains the required data from the server
\param[in] none
\return 0 on success, -ve otherwise
\note
\warning
*/
static _i32 getData()
{
_u8 *p_startPtr = NULL;
_u8 *p_endPtr = NULL;
_u8* p_bufLocation = NULL;
_i32 retVal = -1;
pal_Memset(g_DeviceData.Recvbuff, 0, sizeof(g_DeviceData.Recvbuff));
/* Puts together the HTTP GET string. */
p_bufLocation = g_DeviceData.SendBuff;
pal_Strcpy(p_bufLocation, PREFIX_BUFFER_TEST);
p_bufLocation += pal_Strlen(PREFIX_BUFFER_TEST);
pal_Strcpy(p_bufLocation, POST_BUFFER_TEST_1);
p_bufLocation += pal_Strlen(POST_BUFFER_TEST_1);
pal_Strcpy(p_bufLocation, POST_BUFFER_TEST_2);
/* Send the HTTP GET string to the open TCP/IP socket. */
retVal = sl_Send(g_DeviceData.SockID, g_DeviceData.SendBuff, pal_Strlen(g_DeviceData.SendBuff), 0);
if(retVal != pal_Strlen(g_DeviceData.SendBuff))
ASSERT_ON_ERROR(HTTP_SEND_ERROR);
/* Receive response */
retVal = sl_Recv(g_DeviceData.SockID, &g_DeviceData.Recvbuff[0], MAX_SEND_RCV_SIZE, 0);
if(retVal <= 0)
ASSERT_ON_ERROR(HTTP_RECV_ERROR);
g_DeviceData.Recvbuff[pal_Strlen(g_DeviceData.Recvbuff)] = '\0';
return SUCCESS;
}
接入点的安全设置为
#define SEC_TYPE SL_SEC_TYPE_WPA_WPA2 /* Security type of the Access point */
最后,很少有使用 CC3100 制造的 POC 传感器设备需要将数据传输到云端。为简单起见,我们使用的是 boosterpack,最终我们需要让 POC 传感器设备通过 Wifi 与云通信。