esp8266 从硬件开发到app制作的简单示例 |
您所在的位置:网站首页 › 怎样制作链接在手机上 › esp8266 从硬件开发到app制作的简单示例 |
硬件:esp8266、传感器 app开发:HbuilderX 假设现在有一个项目,内容是用传感器监测数据,然后用手机app接收数据并显示。 第一步:esp8266联网,获取传感器数据,发送数据至中转网站(以dweet举例) #include const char* ssid = "WiFi账号"; const char* password = "WiFi密码"; const char* host = "dweet.io"; void setup() { Serial.begin(115200); delay(10); Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } void loop() { delay(5000); Serial.print("connecting to"); Serial.println(host); WiFiClient client; const int httpPort=80; if(!client.connect(host,httpPort)) { Serial.println("connection failed"); return; } Serial.print("Requesting URL:"); client.print(String("GET /dweet/for/自定义标识名?数据名1=") + "数据1" + "&数据名2=" + "数据2" + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n"); delay(10); unsigned long timeout=millis(); while(client.available()==0) { if(millis()-timeout>5000) { Serial.println(">>>Client Timeout!"); client.stop(); return; } } while(client.available()) { String line=client.readStringUntil('\r'); Serial.print(line); } Serial.println(); Serial.println("closing connection."); }其中自定义标识名一定要符合网址的起名标准,数据名和数据一定要是英语,发送的数据在网页上将以JSON格式显示。 然后将代码编译并烧进开发板,等开发板连接WiFi,登录网址就可以看到自己发送的数据了。这里我上传了两个数据,温度和湿度 https://dweet.io/get/latest/dweet/for/自定义标识名因为网页显示的数据是JSON格式的,所以开发app的时候就要进行数据加工。 考虑到开发效率,本实验用HbuilderX进行app开发。数据利用jQuery的AJAX爬取。菜鸟教程:jQuery – AJAX get() 和 post() 方法 | 菜鸟教程jQuery - AJAX get() 和 post() 方法 jQuery get() 和 post() 方法用于通过 HTTP GET 或 POST 请求从服务器请求数据。 HTTP 请求:GET vs. POST 两种在客户端和服务器端进行请求-响应的常用方法是:GET 和 POST。 GET - 从指定的资源请求数据 POST - 向指定的资源提交要处理的数据 GET 基本上用于从服务器获得(取回)数据。注释:G.. |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |