博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
深入理解Web Server原理----在CC3200 WiFi模块上构建轻量级Web Server
阅读量:5141 次
发布时间:2019-06-13

本文共 5721 字,大约阅读时间需要 19 分钟。

作为博客园的处女作,本文将引导大家理解Web Server的原理。

Table of contents

  1.   常见Web Server及其功能
  2.   低功耗WiFi
  3.   嵌入式Web Server的应用
  4.   Energia Project无缝支持Arduino框架在TI LaunchPad上的扩展
  5.   基于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中修改。

 

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 
.*/#include "SPI.h"#include "WiFi.h"#include "WebServer.h"//your network name and passwordchar ssid[] = "COAP-STATION";char password[] = "这个真不能告诉你";// CHANGE THIS TO YOUR OWN UNIQUE VALUEstatic uint8_t mac[6] = { 0x02, 0xAA, 0xBB, 0xCC, 0x00, 0x22 };// CHANGE THIS TO MATCH YOUR HOST NETWORKstatic 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 loopiteration. */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) = "" "CC3200 LaunchPad" "
" "" "" "" "" """""" "

Control the Device on LaunchPad:

" "
" "

0

" "Red Led"""""; /* 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");}

 

转载于:https://www.cnblogs.com/openIoT/p/4031757.html

你可能感兴趣的文章
Java变量命名规范
查看>>
爬虫大作业-爬取B站弹幕
查看>>
delta3d与ode物理引擎的结合。
查看>>
重载与重写的区别
查看>>
小甲鱼pe结构讲解
查看>>
OD使用教程9 - 调试篇09|解密系列
查看>>
外中断01 - 零基础入门学习汇编语言69
查看>>
PE格式详细讲解11 - 系统篇11|解密系列
查看>>
Qt532_自定义QWebView_01
查看>>
QtTCP_ZC
查看>>
iphone应用程序生命周期浅析
查看>>
Git 中 SSH key 生成步骤
查看>>
ActiveMQ消息游标 --转载
查看>>
Expectation Maximization and GMM
查看>>
Java语言基础-多线程
查看>>
ArcGIS中的坐标系定义与转换 (转载)
查看>>
Nginx 负载平衡 支持域名转发的方法
查看>>
[转]Extjs combo数据绑定与获取
查看>>
spring Controller 层注解获取 properties 里面的值
查看>>
Element-ui多选下拉实现全部与其他互斥
查看>>