basic general http

master
hhCandy 6 years ago
parent f7aef86444
commit ebe585bcca
  1. BIN
      mainCode/data/css/bootstrap-theme.min.css.gz
  2. BIN
      mainCode/data/css/bootstrap.min.css.gz
  3. 1
      mainCode/data/data.json
  4. 78
      mainCode/data/index.html
  5. BIN
      mainCode/data/js/bootstrap.js.gz
  6. BIN
      mainCode/data/js/bootstrap.min.js.gz
  7. 112
      mainCode/mainCode.ino

@ -0,0 +1 @@
{"test":"hh"}

@ -3,76 +3,16 @@
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>ESP8266</title>
<link href="css/bootstrap.min.css" rel="stylesheet">
<style type="text/css">
button {
width: 150px;
padding:8px;
background-color: #428bca;
border-color: #357ebd;
color: #fff;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
border-radius: 10px; /* future proofing */
-khtml-border-radius: 5px; /* for old Konqueror browsers */
text-align: center;
vertical-align: middle;
border: 1px solid transparent;
font-weight: 900;
font-size:100%
}
</style>
</head>
<body>
<nav class="navbar navbar-inverse">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">ESP8266</a>
</div>
</div>
</nav>
<div class="container">
<div class="starter-template">
<h1>欢迎使用WiFiduino</h1>
<br>
<p>点击下面按钮</p>
<P>可控制蓝色LED灯</P>
<br>
<button onclick='setPinValue("on")' >点亮板载LED</button>
<br>
<br>
<button onclick='setPinValue("off")' >熄灭板载LED</button>
</div>
</div>
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script type="text/javascript">
function setPinValue(value) {
var xh = new XMLHttpRequest();
xh.onreadystatechange = function() {
if (xh.readyState == 4){
if(xh.status == 200) {
//alert(xh.responseText);
alert("操作成功");
}
}
}
var requestString = "/pin?a=" + value + "&pin=2";
xh.open("GET", requestString, true);
xh.send(null);
}
</script>
<h3>Welcome to wIot!</h3>
<br/>
<h4>Control url:</h4>
<p>/ctl?p=/*Pin Code*/&a=/*Action: high or low*/</p>
<p>e.g. /ctl?p=2&a=low Means to open the led on the board.</p>
<br/>
<h4>Data Get:</h4>
<p>/data</p>
<p><a href="/data">Click Here to see!</a></p>
</body>
</html>

Binary file not shown.

@ -3,11 +3,8 @@
#include <FS.h>
ESP8266WebServer server ( 80 );
String ssid = "TOTOLINK_3be124"; //你的wifi热点名称
String password = "13371035727"; //你的wifi热点密码
//String ssid = "236-iot"; //你的wifi热点名称
//String password = "236236236"; //你的wifi热点密码
String ssid = "yimian-iot"; //你的wifi热点名称
String password = "1234567890."; //你的wifi热点密码
String getContentType(String filename){
if(server.hasArg("download")) return "application/octet-stream";
@ -25,8 +22,33 @@ String getContentType(String filename){
else if(filename.endsWith(".gz")) return "application/x-gzip";
return "text/plain";
}
/* NotFound处理
*/
/* wifi 连接 */
void wifiCnct() {
int connectCount = 0;
WiFi.begin ( ssid.c_str(), password.c_str() );
while ( WiFi.status() != WL_CONNECTED ) {
digitalWrite(2, HIGH);
delay ( 1000 );
Serial.print ( "." );
if(connectCount > 30) {
Serial.println( "Connect fail!" );
break;
}
connectCount += 1;
}
if(WiFi.status() == WL_CONNECTED) {
Serial.println ( "" );
Serial.print ( "Connected to " );
Serial.println ( ssid );
Serial.print ( "IP address: " );
Serial.println ( WiFi.localIP() );
connectCount = 0;
digitalWrite(2, LOW);
}
}
/* NotFound处理 */
void handleNotFound() {
String path = server.uri();
Serial.print("load url:");
@ -54,6 +76,8 @@ void handleNotFound() {
}
server.send ( 404, "text/plain", message );
}
/* index提示页面 */
void handleMain() {
Serial.print("handleMain");
File file = SPIFFS.open("/index.html", "r");
@ -61,45 +85,58 @@ void handleMain() {
file.close();
return;
}
/* data提取url */
void handleData() {
Serial.print("handleData");
char cmd[135];
sprintf(cmd,"{\"D0\":\"%d\",\"D1\":\"%d\",\"D2\":\"%d\",\"D3\":\"%d\",\"D4\":\"%d\",\"D5\":\"%d\",\"D6\":\"%d\",\"D7\":\"%d\",\"D8\":\"%d\",\"D9\":\"%d\",\"D10\":\"%d\",\"D11\":\"%d\",\"D12\":\"%d\",\"D13\":\"%d\",\"A0\":\"%f\"}",digitalRead(D0),digitalRead(D1),digitalRead(D2),digitalRead(D3),digitalRead(D4),digitalRead(D5),digitalRead(D6),digitalRead(D7),digitalRead(D8),digitalRead(D9),digitalRead(D10),digitalRead(D11),digitalRead(D12),digitalRead(D13),analogRead(A0));
File file = SPIFFS.open("/data.json","w+");
if (!file) {
Serial.println("There was an error opening the file for writing");
return;
}
if (file.print(cmd)) {
Serial.println("File was written");
} else {
Serial.println("File write failed");
}
file.close();
file = SPIFFS.open("/data.json","r");
size_t sent = server.streamFile(file, "application/json");
file.close();
return;
}
/* execute url */
void handlePin() {
if(server.hasArg("a")) {
String action = server.arg("a");
if(action == "on") { //a=on
digitalWrite(2, LOW); //点亮8266上的蓝色led
server.send ( 200, "text/html", "Pin 2 has turn on"); return; //返回数据
} else if(action == "off") { // a=off
digitalWrite(2, HIGH); //熄灭板载led
server.send ( 200, "text/html", "Pin 2 has turn off"); return;
if(server.hasArg("p")) {
String pin = server.arg("p");
if(server.hasArg("a")) {
String action = server.arg("a");
if(action == "high") { //a=on
digitalWrite(atoi(pin.c_str()), HIGH); //抬高电平
server.send ( 200, "application/json", "{\"code\":\"2\",\"state\":\"HIGH\"}"); return; //返回数据
} else if(action == "low") { // a=off
digitalWrite(atoi(pin.c_str()), LOW); //拉低电平
server.send ( 200, "application/json", "{\"code\":\"1\",\"state\":\"LOW\"}"); return;
}
server.send ( 200, "application/json", "{\"code\":\"-1\",\"state\":\"UNKNOW ACTION!\"}"); return;
}
server.send ( 200, "text/html", "unknown action"); return;
server.send ( 200, "application/json", "{\"code\":\"-2\",\"state\":\"ACTION NO FOUND!\"}");
}
server.send ( 200, "text/html", "action no found");
}
void setup() {
pinMode(2, OUTPUT);
Serial.begin ( 115200 );
SPIFFS.begin();
int connectCount = 0;
WiFi.begin ( ssid.c_str(), password.c_str() );
while ( WiFi.status() != WL_CONNECTED ) {
delay ( 1000 );
Serial.print ( "." );
if(connectCount > 30) {
Serial.println( "Connect fail!" );
break;
}
connectCount += 1;
}
if(WiFi.status() == WL_CONNECTED) {
Serial.println ( "" );
Serial.print ( "Connected to " );
Serial.println ( ssid );
Serial.print ( "IP address: " );
Serial.println ( WiFi.localIP() );
connectCount = 0;
}
wifiCnct();
server.on ("/", handleMain);
server.on ("/pin", HTTP_GET, handlePin);
server.on ("/ctl", handlePin);
server.on ("/data", HTTP_GET, handleData);
server.onNotFound ( handleNotFound );
server.begin();
Serial.println ( "HTTP server started" );
@ -107,4 +144,5 @@ void setup() {
void loop() {
server.handleClient();
//if( WiFi.status() != WL_CONNECTED) wifiCnct();
}

Loading…
Cancel
Save