本帖最后由 hun517 于 2020-9-8 15:59 编辑
- /***********
- 局域网下控制舵机快关灯
- 需要esp8266库
- **************/
- #include <ESP8266WiFi.h>
- #include <ESP8266mDNS.h>
- #include <WiFiUdp.h>
- #include <ArduinoOTA.h>
- #include <Servo.h>
- /***********请修改以下内容**************/
- const char* ssid = "wifi01";// wifi名称
- const char* password = "c12345678";// wifi密码
- const int ESP_BUILTIN_LED = 2; //led引脚状态指示
- const int pin = 0; //舵机连接引脚
- const int pos0 = 50; // 开灯角度,多次实验调节
- const int pos1 = 130; // 关灯角度,需要多次实验调节
- const int pos2 = 90; // 正常角度
- /***********请修改以上内容**************/
- int anl=0;
- WiFiServer server(80); //创建tcp server
- Servo myservo; //创建舵机对象
- void setup() {
- Serial.begin(115200);
- myservo.write(pos2);
- delay(200);
-
- Serial.println();
- Serial.println();
- Serial.println("Connecting to ");
- Serial.print(ssid);
-
- //WiFi.mode(WIFI_STA);//设置为接收WiFi模式
- WiFi.begin(ssid, password);
- struct ip_info info;
-
- //下方修改局域网内静态IP;
- //注意:不要与其他ip冲突ip冲突
- IP4_ADDR(&info.ip,192,168,1,173);
-
- IP4_ADDR(&info.gw,192,168,1,1);
- IP4_ADDR(&info.netmask,255,255,255,0);
- wifi_station_dhcpc_stop();
- wifi_set_ip_info(STATION_IF,&info); //设置sta模式的IP
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(F("."));
- }
-
- Serial.println();
- Serial.println(F("WiFi connected"));
- // Hostname defaults to esp8266-[ChipID]
- //ArduinoOTA.setHostname("WemosEXP");
- ArduinoOTA.begin();
- pinMode(ESP_BUILTIN_LED, OUTPUT);
-
- server.begin();//启动tcp连接
- Serial.println("Server started @");
- Serial.print(WiFi.localIP());
- myservo.attach(pin);
- delay(20);
- }
- String prepareHtmlPage(){
- String htmlPage =
- String("HTTP/1.1 200 OK\r\n") +
- "Content-Type: text/html\r\n" +
- "Connection: close\r\n" + // the connection will be closed after completion of the response
- "\r\n" +
- "<!DOCTYPE html> <html>\n"+
- "<head><meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">\n"+
- "<title>LED Control</title>\n"+
- "<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n"+
- "body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;} h3 {color: #444444;margin-bottom: 50px;}\n"+
- ".button {display: block;width: 120px;background-color: #1abc9c;border: none;color: white;padding: 13px 30px;text-decoration: none;font-size: 25px;margin: 0px auto 35px;cursor: pointer;border-radius: 4px;}\n"+
- ".button-on {background-color: #5AD2FF;}\n"+
- ".button-on:active {background-color: #DE341E;}\n"+
-
- ".button-off {background-color: #34495e;}\n"+
- ".button-off:active {background-color: #DE341E;}\n"+
- "p {font-size: 14px;color: #888;margin-bottom: 10px;}\n"+
- "</style>\n"+
- "</head>\n"+
- "<body>\n"+
- "<h1>Digital PIN2 status: "+String(digitalRead(ESP_BUILTIN_LED))+"</h1>\n"+
-
- "<form id='F1' action='LEDON'><input class='button button-on' type='submit' value='ON' ></form><br>"+
- "<form id='F2' action='LEDOFF'><input class='button button-off' type='submit' value='OFF' ></form><br>"+
- "</html>" +
- "\r\n";
- return htmlPage;
- }
- void loop() {
-
- ArduinoOTA.handle();
- WiFiClient client = server.available();
- // wait for a client (web browser) to connect
- if (client){
- while (client.connected()){
- // 不断读取请求内容
- if (client.available()){
- String line = client.readStringUntil('\r');
-
- if ( line.indexOf("LEDON") > 0 ) {
- digitalWrite(ESP_BUILTIN_LED, LOW);
- myservo.write(pos0);
- delay(500);
- myservo.write(pos2);
- delay(500);
- }
- else if ( line.indexOf("LEDOFF") > 0 ) {
- digitalWrite(ESP_BUILTIN_LED, HIGH);
- myservo.write(pos1);
- delay(500);
- myservo.write(pos2);
- delay(500);
- }
-
- // Serial.print(line);
- if (line.length() == 1 && line[0] == '\n'){
- //返回响应内容
- client.println(prepareHtmlPage());
- break;
- }
- }
- }
- delay(1000); // give the web browser time to receive the data
- // close the connection:
- client.flush();//注意这里必须用client.flush(),如果是client.close会报错的
- }
- //delay(5000);
-
- }
复制代码
|