关于http服务器的学习笔记 基于CC3200

来源:互联网 发布:制作动画片软件是什么 编辑:程序博客网 时间:2024/06/08 08:08

本文记录通过网页控制CC3200device设备的过程。 
首先贴出过程框架,如图1 sw,图2 hw 
sw 
图1 
hw 
图2 
是图一中可以看出 Customer Application 运行在最上层。本文中指的是网页。图2为硬件框架图。 
CC3200HTTP服务器支持以下功能: 
1、http支持版本:1.0 
2、http请求:Get和Post。 
3、支持文件类型:HTML,.htm,.css,.xml,.png和.gif 
4、HTML表单通过post方法提交数据 
5、默认端口:80 
主要用到getpost处理 
以用网页控制LED亮灭为例子,说明过程。 
先看网页代码 
$(function() { 
var sprinklerSwitch = $('#sprinklerSwitch'), 
switchBtn = $('#switchImage'), 
sprinkler = $('#sprinkler'), 
spriklerRunning = false, 
imageOn = false, 
sprinklerToggle = setInterval(function() { 
if (spriklerRunning) { 
if (imageOn) { 
sprinkler.attr('src',"images/demo-sprinkler-off.jpg"); 
imageOn = false; 
} else { 
sprinkler.attr('src',"images/demo-sprinkler-on.jpg"); 
imageOn = true; 


}, 300); 
sprinklerSwitch.click(function(){ 
if (switchBtn.hasClass('on')) { 
switchBtn.removeClass('on'); 
com.TI.toggleLED('_OFF'); 
spriklerRunning = false; 
sprinkler.attr('src',"images/demo-sprinkler-off.jpg"); 
imageOn = false; 
} else { 
switchBtn.addClass('on'); 
com.TI.toggleLED('_Blink'); 
spriklerRunning = true; 
imageOn = true; 

});

sprinklerSwitch.click(function()函数获取网页开关状态,之后调用com.TI.toggleLED函数

if(!window.com) com = new Object();if(!com.TI) com.TI = new Object();com.TI.toggleLED = function(whichOne) {    var LEDnum = "1",        params = "LED"+LEDnum;    params += whichOne;    $.post("No_content", {"__SL_P_ULD": params});}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

这里重点看$.post(“No_content”, {“__SL_P_ULD”: params}); 即通过post传递数据给回调函数。 
函数com.TI.toggleLED()首先定义变量params: 
1,OFF时params=LED1_OFF; 
2,ON时 params=LED1_Blink; 
然后处理http请求:以post方法发送“”_SL_P_ULD“”和params。 
http服务器接受到http请求后,对用户post请求标记“”_SL_P_ULD“”调用回调函数post事件处理程序对params参数传递的内容进行处理: 
1,对LED1_OFF调用GPIO_IF_Ledoff()熄灭红led,同时将led状态g_ucledstatus设为LED_BLINK,OBB任务函数OBBTask()通过判断g_ucLEDStatus再次调用GPIO_IF_LedOff()熄灭红LED. 
2,对LED1_BLINK的处理过程不再赘与。 
以上都是网页上的程序,通过chrome开发者工具可以观察并调试。 
现在去找回调函数,运行在device上

//*****************************************************************************////! \brief This function handles HTTP server events//!//! \param[in]  pServerEvent - Contains the relevant event information//! \param[in]    pServerResponse - Should be filled by the user with the//!                                      relevant response information//!//! \return None//!//****************************************************************************void SimpleLinkHttpServerCallback(SlHttpServerEvent_t *pSlHttpServerEvent,                                 SlHttpServerResponse_t *pSlHttpServerResponse){    switch (pSlHttpServerEvent->Event)    {        case SL_NETAPP_HTTPGETTOKENVALUE_EVENT:        {            unsigned char *ptr;            ptr = pSlHttpServerResponse->ResponseData.token_value.data;            pSlHttpServerResponse->ResponseData.token_value.len = 0;            if(memcmp(pSlHttpServerEvent->EventData.httpTokenName.data,                     GET_token_TEMP, strlen((const char *)GET_token_TEMP)) == 0)            {                float fCurrentTemp;                TMP006DrvGetTemp(&fCurrentTemp);                char cTemp = (char)fCurrentTemp;                short sTempLen = itoa(cTemp,(char*)ptr);                ptr[sTempLen++] = ' ';                ptr[sTempLen] = 'F';                pSlHttpServerResponse->ResponseData.token_value.len += sTempLen;            }            if(memcmp(pSlHttpServerEvent->EventData.httpTokenName.data,                       GET_token_UIC, strlen((const char *)GET_token_UIC)) == 0)            {                if(g_iInternetAccess==0)                    strcpy((char*)pSlHttpServerResponse->ResponseData.token_value.data,"1");                else                    strcpy((char*)pSlHttpServerResponse->ResponseData.token_value.data,"0");                pSlHttpServerResponse->ResponseData.token_value.len =  1;            }            if(memcmp(pSlHttpServerEvent->EventData.httpTokenName.data,                        GET_token_ACC, strlen((const char *)GET_token_ACC)) == 0)            {                ReadAccSensor();                if(g_ucDryerRunning)                {                    strcpy((char*)pSlHttpServerResponse->ResponseData.token_value.data,"Running");                    pSlHttpServerResponse->ResponseData.token_value.len += strlen("Running");                }                else                {                    strcpy((char*)pSlHttpServerResponse->ResponseData.token_value.data,"Stopped");                    pSlHttpServerResponse->ResponseData.token_value.len += strlen("Stopped");                }            }        }            break;        case SL_NETAPP_HTTPPOSTTOKENVALUE_EVENT:        {            unsigned char led;            unsigned char *ptr = pSlHttpServerEvent->EventData.httpPostData.token_name.data;            //g_ucLEDStatus = 0;            if(memcmp(ptr, POST_token, strlen((const char *)POST_token)) == 0)            {                ptr = pSlHttpServerEvent->EventData.httpPostData.token_value.data;                if(memcmp(ptr, "LED", 3) != 0)                    break;                ptr += 3;                led = *ptr;                ptr += 2;                if(led == '1')                {                    if(memcmp(ptr, "ON", 2) == 0)                    {                        GPIO_IF_LedOn(MCU_RED_LED_GPIO);                                                g_ucLEDStatus = LED_ON;                    }                    else if(memcmp(ptr, "Blink", 5) == 0)                    {                        GPIO_IF_LedOn(MCU_RED_LED_GPIO);                        g_ucLEDStatus = LED_BLINK;                    }                    else                    {                        GPIO_IF_LedOff(MCU_RED_LED_GPIO);                                                g_ucLEDStatus = LED_OFF;                    }                }                else if(led == '2')                {                    if(memcmp(ptr, "ON", 2) == 0)                    {                        GPIO_IF_LedOn(MCU_ORANGE_LED_GPIO);                    }                    else if(memcmp(ptr, "Blink", 5) == 0)                    {                        GPIO_IF_LedOn(MCU_ORANGE_LED_GPIO);                        g_ucLEDStatus = 1;                    }                    else                    {                        GPIO_IF_LedOff(MCU_ORANGE_LED_GPIO);                    }                }            }        }            break;        default:            break;    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124

可以看到device对来自web命令的相应回复。 
这里需要注意,在device上运行有http事件处理函数,且处在待命状态。

void SimpleLinkWlanEventHandler(SlWlanEvent_t *pWlanEvent){    if(pWlanEvent == NULL)    {        UART_PRINT("Null pointer\n\r");        LOOP_FOREVER();    }    switch(pWlanEvent->Event)    {        case SL_WLAN_CONNECT_EVENT:        {            SET_STATUS_BIT(g_ulStatus, STATUS_BIT_CONNECTION);            //            // Information about the connected AP (like name, MAC etc) will be            // available in 'slWlanConnectAsyncResponse_t'            // Applications can use it if required            //            //  slWlanConnectAsyncResponse_t *pEventData = NULL;            // pEventData = &pWlanEvent->EventData.STAandP2PModeWlanConnected;            //            // Copy new connection SSID and BSSID to global parameters            memcpy(g_ucConnectionSSID,pWlanEvent->EventData.                   STAandP2PModeWlanConnected.ssid_name,                   pWlanEvent->EventData.STAandP2PModeWlanConnected.ssid_len);            memcpy(g_ucConnectionBSSID,                   pWlanEvent->EventData.STAandP2PModeWlanConnected.bssid,                   SL_BSSID_LENGTH);            UART_PRINT("[WLAN EVENT] Device Connected to the AP: %s , "                       "BSSID: %x:%x:%x:%x:%x:%x\n\r",                      g_ucConnectionSSID,g_ucConnectionBSSID[0],                      g_ucConnectionBSSID[1],g_ucConnectionBSSID[2],                      g_ucConnectionBSSID[3],g_ucConnectionBSSID[4],                      g_ucConnectionBSSID[5]);        }        break;        case SL_WLAN_DISCONNECT_EVENT:        {            slWlanConnectAsyncResponse_t*  pEventData = NULL;            CLR_STATUS_BIT(g_ulStatus, STATUS_BIT_CONNECTION);            CLR_STATUS_BIT(g_ulStatus, STATUS_BIT_IP_AQUIRED);            pEventData = &pWlanEvent->EventData.STAandP2PModeDisconnected;            // If the user has initiated 'Disconnect' request,            //'reason_code' is SL_WLAN_DISCONNECT_USER_INITIATED_DISCONNECTION            if(SL_WLAN_DISCONNECT_USER_INITIATED_DISCONNECTION == pEventData->reason_code)            {                UART_PRINT("[WLAN EVENT] Device disconnected from the AP: %s, "                           "BSSID: %x:%x:%x:%x:%x:%x on application's "                           "request \n\r",                           g_ucConnectionSSID,g_ucConnectionBSSID[0],                           g_ucConnectionBSSID[1],g_ucConnectionBSSID[2],                           g_ucConnectionBSSID[3],g_ucConnectionBSSID[4],                           g_ucConnectionBSSID[5]);            }            else            {                UART_PRINT("[WLAN ERROR] Device disconnected from the AP AP: %s, "                           "BSSID: %x:%x:%x:%x:%x:%x on an ERROR..!! \n\r",                           g_ucConnectionSSID,g_ucConnectionBSSID[0],                           g_ucConnectionBSSID[1],g_ucConnectionBSSID[2],                           g_ucConnectionBSSID[3],g_ucConnectionBSSID[4],                           g_ucConnectionBSSID[5]);            }            memset(g_ucConnectionSSID,0,sizeof(g_ucConnectionSSID));            memset(g_ucConnectionBSSID,0,sizeof(g_ucConnectionBSSID));        }        break;        case SL_WLAN_STA_CONNECTED_EVENT:        {            // when device is in AP mode and any client connects to device cc3xxx            //SET_STATUS_BIT(g_ulStatus, STATUS_BIT_CONNECTION);            //CLR_STATUS_BIT(g_ulStatus, STATUS_BIT_CONNECTION_FAILED);            //            // Information about the connected client (like SSID, MAC etc) will            // be available in 'slPeerInfoAsyncResponse_t' - Applications            // can use it if required            //            // slPeerInfoAsyncResponse_t *pEventData = NULL;            // pEventData = &pSlWlanEvent->EventData.APModeStaConnected;            //            UART_PRINT("[WLAN EVENT] Station connected to device\n\r");        }        break;        case SL_WLAN_STA_DISCONNECTED_EVENT:        {            // when client disconnects from device (AP)            //CLR_STATUS_BIT(g_ulStatus, STATUS_BIT_CONNECTION);            //CLR_STATUS_BIT(g_ulStatus, STATUS_BIT_IP_LEASED);            //            // Information about the connected client (like SSID, MAC etc) will            // be available in 'slPeerInfoAsyncResponse_t' - Applications            // can use it if required            //            // slPeerInfoAsyncResponse_t *pEventData = NULL;            // pEventData = &pSlWlanEvent->EventData.APModestaDisconnected;            //            UART_PRINT("[WLAN EVENT] Station disconnected from device\n\r");        }        break;        case SL_WLAN_SMART_CONFIG_COMPLETE_EVENT:        {            //SET_STATUS_BIT(g_ulStatus, STATUS_BIT_SMARTCONFIG_START);            //            // Information about the SmartConfig details (like Status, SSID,            // Token etc) will be available in 'slSmartConfigStartAsyncResponse_t'            // - Applications can use it if required            //            //  slSmartConfigStartAsyncResponse_t *pEventData = NULL;            //  pEventData = &pSlWlanEvent->EventData.smartConfigStartResponse;            //        }        break;        case SL_WLAN_SMART_CONFIG_STOP_EVENT:        {            // SmartConfig operation finished            //CLR_STATUS_BIT(g_ulStatus, STATUS_BIT_SMARTCONFIG_START);            //            // Information about the SmartConfig details (like Status, padding            // etc) will be available in 'slSmartConfigStopAsyncResponse_t' -            // Applications can use it if required            //            // slSmartConfigStopAsyncResponse_t *pEventData = NULL;            // pEventData = &pSlWlanEvent->EventData.smartConfigStopResponse;            //        }        break;        default:        {            UART_PRINT("[WLAN EVENT] Unexpected event [0x%x]\n\r",                       pWlanEvent->Event);        }        break;    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151

还有一个void SimpleLinkNetAppEventHandler()函数也时刻待命。

这里只用到了post。如果是从device到web,要用get。比如,在web上显示传感器的温度。用get函数获取。 
理解 
**post:推送 
get:获取**

这里只是web和device之间数据交换 只能在连接device之后才能使用web。所以范围很小。我们需要同在有互联网的任何地方控制和监察device,所以就需要

有云之后的一般过程为:

  1. 在云上注册设备
  2. 运行设备
  3. 在云上开发solution 
    可以在(https://exosite.com/)上注册,然后用python运行虚拟设备,体验整个开发过程。

关于HTML+css+js,在网上看到一则比较好理解的解释:

形象的说 
你建一座木屋,html就是他的门,墙,柱子,屋顶, 
你要是确定屋子多高,多宽,门,窗在哪,就是css确定, 
你在屋子里,把椅子从南拿到北,这个过程就是js, 
这三个才构成网页的页面。

                     《完》 
原创粉丝点击