最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

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

2023-01-18 19:54 作者:悲匠  | 我要投稿

前言:

本次試驗我所用的硬件: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)等等

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

分享到微博請遵守國家法律
内黄县| 咸阳市| 曲水县| 建昌县| 保山市| 双辽市| 岳普湖县| 木兰县| 恩平市| 呈贡县| 平顺县| 陇西县| 巴马| 盐池县| 伽师县| 镇宁| 拜城县| 治多县| 洛浦县| 顺平县| 博白县| 义马市| 邵武市| 叶城县| SHOW| 鄢陵县| 青神县| 舞阳县| 泌阳县| 瑞安市| 安宁市| 理塘县| 上饶县| 贡嘎县| 湖北省| 溧阳市| 大冶市| 宜春市| 古蔺县| 大连市| 桐城市|