设为首页 收藏本站
查看: 893|回复: 0

[经验分享] 深入理解Web Server原理----在CC3200 WiFi模块上构建轻量级Web Server

[复制链接]
累计签到:7 天
连续签到:1 天
发表于 2015-10-1 08:56:00 | 显示全部楼层 |阅读模式
  作为博客园的处女作,本文将引导大家理解Web Server的原理。
  Table of contents


  •   常见Web Server及其功能
  •   低功耗WiFi
  •   嵌入式Web Server的应用
  •   Energia Project无缝支持Arduino框架在TI LaunchPad上的扩展
  •   基于CC3200如何构建一个嵌入式Web Server
  做过Web开发的同学都知道,Web Server是用来处理http(POST、GET、PUT、DELETE等)请求的系统,有大名鼎鼎的Apache http Server,也有企业应用中的Microsoft IIS。
  
  我们在IE输入URL:192.168.18.108/cc3200, 登陆到CC3200构建的Web Server上,其中,cc3200是统一资源标示符,可在Energia中修改。


  • DSC0000.jpg
DSC0001.jpg
  
  Energia是个什么东西?他是TI从Arduino那边Fork过来的,正对MSP430等TI的芯片重新封装的一个IDE。非常适合学生,创客使用。
  



/*
Copyright (c) 2014 等风的猪.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation, either
version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include "SPI.h"
#include "WiFi.h"
#include "WebServer.h"
//your network name and password
char ssid[] = "COAP-STATION";
char password[] = "这个真不能告诉你";
// CHANGE THIS TO YOUR OWN UNIQUE VALUE
static uint8_t mac[6] = { 0x02, 0xAA, 0xBB, 0xCC, 0x00, 0x22 };
// CHANGE THIS TO MATCH YOUR HOST NETWORK
static uint8_t ip[4] = { 192, 168, 1, 210 }; // area 51!
/* all URLs on this server will start with /buzz because of how we
* define the PREFIX value.  We also will listen on port 80, the
* standard HTTP service port */
#define PREFIX "/cc3200"
WebServer webserver(PREFIX, 80);
/* the piezo speaker on the Danger Shield is on PWM output pin #3 */
#define BUZZER_PIN 24
/* this is the number of microseconds to wait after turning the
* speaker on before turning it off. */
int buzzDelay = 0;
/* toggle is used to only turn on the speaker every other loop
iteration. */
char toggle = 0;
/* This command is set as the default command for the server.  It
* handles both GET and POST requests.  For a GET, it returns a simple
* page with some buttons.  For a POST, it saves the value posted to
* the buzzDelay variable, affecting the output of the speaker */
void buzzCmd(WebServer &server, WebServer::ConnectionType type, char *, bool)
{
if (type == WebServer::POST)
{
bool repeat;
char name[16], value[16];
do
{
/* readPOSTparam returns false when there are no more parameters
* to read from the input.  We pass in buffers for it to store
* the name and value strings along with the length of those
* buffers. */
repeat = server.readPOSTparam(name, 16, value, 16);
/* this is a standard string comparison function.  It returns 0
* when there's an exact match.  We're looking for a parameter
* named "buzz" here. */
if (strcmp(name, "buzz") == 0)
{
/* use the STRing TO Unsigned Long function to turn the string
* version of the delay number into our integer buzzDelay
* variable */
buzzDelay = strtoul(value, NULL, 10);
}
/* handle led */
else if(strcmp(name, "red_led") == 0)
{
int16_t state = strtoul(value, NULL, 10);
boolean pin_status = (state == 1) ? HIGH : LOW;
digitalWrite(RED_LED, pin_status);
Serial.println("red button\n");
Serial.println(value);
}
} while (repeat);
// after procesing the POST data, tell the web browser to reload
// the page using a GET method.
    server.httpSeeOther(PREFIX);
return;
}
/* for a GET or HEAD, send the standard "it's all OK headers" */
server.httpSuccess();
/* we don't output the body for a HEAD request */
if (type == WebServer::GET)
{
/* store the HTML in program memory using the P macro */
P(message) =
"<!DOCTYPE html><html><head>"
"<title>CC3200 LaunchPad</title>"
"<link href='https://code.jquery.com/ui/1.11.1/themes/smoothness/jquery-ui.css' rel=stylesheet />"
"<script src='https://code.jquery.com/jquery-1.11.1.min.js'></script>"
"<script src='https://code.jquery.com/ui/1.11.1/jquery-ui.min.js'></script>"
"<style> #slider { margin: 10px; } </style>"
"<style type='text/css'>"
"a {"  
"background-color: #dcdcdc;"        
"cursor: pointer;"
"border-top: solid 2px #eaeaea;"
"border-left: solid 2px #eaeaea;"
"border-bottom: solid 2px #777;"
"border-right: solid 2px #777;"
"padding: 5px 5px;       "
"}"
"a.down {"
"background-color: #dc143c;"        
"border-top: solid 2px #777;"
"border-left: solid 2px #777;"
"border-bottom:solid 2px  #eaeaea;"
"border-right: solid 2px #eaeaea;"
"}"
"</style>"
"<script>"
"var state = 1;"
"function changeBuzz(event, ui) { $('#indicator').text(ui.value); $.post('/cc3200', { buzz: ui.value } ); }"   
"$(document).ready(function(){"
"$('a#button').click(function(){"
"$(this).toggleClass('down');"                     
"$.post('/cc3200', { red_led: state } );"         
"if(state == 0)"
"{state = 1;}"
"else if(state == 1)"
"{state = 0;}"
"});"
"$('#slider').slider({min: 0, max:8000, change:changeBuzz});"
"});"
"</script>"
"</head>"
"<body style='font-size:62.5%;'>"
"<h1>Control the Device on LaunchPad:</h1>"
"<div id=slider></div>"
"<p id=indicator>0</p>"
"<a id=button title='button'>Red Led</a>"
"</body>"
"</html>";
/* send the html back to the browser */
server.printP(message);
}
}
void setup()
{
Serial.begin(9600);
// set the PWM output for the buzzer to out
  pinMode(BUZZER_PIN, OUTPUT);
pinMode(RED_LED, OUTPUT);
// setup the Ehternet library to talk to the Wiznet board
Serial.print("Attempting to connect to Network:");
Serial.print(ssid);
WiFi.begin(ssid, password);
while(WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(300);
}
Serial.println("\nYou're connected to the network");
Serial.println("Waiting for an ip address");
while (WiFi.localIP() == INADDR_NONE) {
// print dots while we wait for an ip addresss
Serial.print(".");
delay(300);
}
// you're connected now, so print out the status  
  printWifiStatus();
/* register our default command (activated with the request of
* http://x.x.x.x/buzz */
webserver.setDefaultCommand(&buzzCmd);
/* start the server to wait for connections */
webserver.begin();
}
void loop()
{
// process incoming connections one at a time forever
  webserver.processConnection();
/* every other time through the loop, turn on and off the speaker if
* our delay isn't set to 0. */
if ((++toggle & 1) && (buzzDelay > 0))
{
digitalWrite(BUZZER_PIN, HIGH);
delayMicroseconds(buzzDelay);
digitalWrite(BUZZER_PIN, LOW);
}
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("Network Name: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-121149-1-1.html 上篇帖子: 让手机连接到指定的WIFI网络,适用于之前已经连过的网络 下篇帖子: 几种网络通讯协议(WIFI,WAPI,GSM,GPRS,CDMA)概述
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表