ESP8266連接服務(wù)器實現(xiàn)數(shù)據(jù)傳輸(http協(xié)議)

前言:
本次試驗我所用的硬件:esp8266,云服務(wù)器(寶塔),超聲波傳感器。通過esp8266發(fā)送get請求與服務(wù)器交互并保存數(shù)據(jù)到數(shù)據(jù)庫
1.服務(wù)端代碼
服務(wù)器用php寫了一個接口接收GET請求數(shù)據(jù)存入數(shù)據(jù)庫并返回json,代碼如下:
<?php
? ?//設(shè)置json格式頭部
? ?header('Content-type: application/json');
$conn=mysql_connect("數(shù)據(jù)庫地址","數(shù)據(jù)庫用戶名","數(shù)據(jù)庫密碼"); //連接數(shù)據(jù)庫地址、用戶名、密碼
mysql_query("set names 'utf8'"); //數(shù)據(jù)庫編碼
mysql_select_db("數(shù)據(jù)庫名稱"); //數(shù)據(jù)庫名稱
? ?$time = time();//獲取當(dāng)前時間戳
? ?//獲取GET請求參數(shù)
? ?$code = $_GET['code'];
? ?$hight = $_GET['hight'];
? ?if($hight != null){
? ? ? ? ? ?$sql = "INSERT INTO text (idd,time)VALUES ('$hight','$time')";
? ? ? ? ? ?$rs=mysql_query($sql);
? ? ? ? ? ?$sql1="select * from text order by rand( ) limit 1";
? ? ? ? ? ?$row1=mysql_fetch_assoc(mysql_query($sql1));
? ? ? ? ? ?//輸出json
? ? ? ? ? ?echo json_encode(array('code'=>200,'data'=>$row1['hight']));
? ?}
?//輸出json
if($code = null){
?echo json_encode(array('code'=>400,'data'=>"code is null"));
? ?}else($hight){
?echo json_encode(array('code'=>401,'data'=>"hight is null"));
? ?}
?>
2.esp8266代碼
esp8266主要通過get請求將數(shù)據(jù)作為參數(shù)提交到服務(wù)器
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
const char *ssid = "wifi名稱";
const char *password = "wifi密碼";
const int trigPin = 15;//超聲波引腳
const int echoPin = 13;//超聲波引腳
float duration;//超聲波數(shù)據(jù)
float distance;//超聲波數(shù)據(jù)
void setup()
{
?//初始化代碼
?pinMode(trigPin, OUTPUT);
?pinMode(echoPin, INPUT);
?pinMode(dian, OUTPUT);
?digitalWrite(dian, LOW);
?Serial.begin(9600);
?WiFi.mode(WIFI_STA);
?WiFi.begin(ssid, password);
//wifi連接
?while (WiFi.status() != WL_CONNECTED)
?{
? ?delay(500);
? ?Serial.print(".");
?}
?Serial.println("網(wǎng)絡(luò)連接成功 IP:");
?Serial.print(WiFi.localIP());
}
void loop()
{
?//超聲波啟動
?digitalWrite(trigPin, LOW);
?delayMicroseconds(2);
?digitalWrite(trigPin, HIGH);
?delayMicroseconds(10);
?digitalWrite(trigPin, LOW);
?duration = pulseIn(echoPin, HIGH);//獲取超聲波原始數(shù)據(jù)
?distance = duration * 0.034 / 2.0;//處理原始數(shù)據(jù)轉(zhuǎn)換為cm
?post(distance);//將超聲波數(shù)據(jù)上傳到服務(wù)器
?Serial.println("水位:");
?Serial.println(distance,"200");
?
}
//post函數(shù),用于上傳數(shù)據(jù)到服務(wù)器并且接收返回的json
void post(float hight,String code) {
?WiFiClient client;
?HTTPClient hc;
?String a = String(hight);//將獲取的超聲波數(shù)據(jù)(float)轉(zhuǎn)換為String類型
?String url = "http://127.0.0.1/api.php?hight=" + a;//拼接請求
?hc.begin(client, url);
?int httpCode = hc.GET();
?if (httpCode == HTTP_CODE_OK)
?{
? ?//請求成功
? ?String responsePayload = hc.getString();
? ?Serial.println("數(shù)據(jù)上傳成功:");
? ?Serial.println(responsePayload);//獲取服務(wù)器返回的響應(yīng)有效負載json數(shù)據(jù)
?}
?else
?{
? ?Serial.println("數(shù)據(jù)上傳失?。?#34;);
? ?Serial.print(httpCode);//獲取錯誤狀態(tài)碼
?}
?hc.end();
}
3.測試
3.1使用arduino燒錄esp8266

打開串口監(jiān)視器

打開數(shù)據(jù)庫查看數(shù)據(jù)是否寫入

最后:通過上面的方法可以實現(xiàn)遠程數(shù)據(jù)采集,通過獲取服務(wù)器響應(yīng)有效負載可以實現(xiàn)遠程開關(guān)等等
標(biāo)簽: