网站建设三网合一是什么,南京高端品牌网站建设,网站建设的需要分析,宁波网站推广一利用此程序的思路就可以用浏览器显示esp32 采集的各种传感器的数据#xff0c;也可以去控制各种传感器。省去编写针对各系统的app.
图片 1.浏览器每隔1秒更新一次数据
2.现在更新的是开机数据#xff0c;运用此程序#xff0c;可以实时显示各种传感器的实时数据
3.es…一利用此程序的思路就可以用浏览器显示esp32 采集的各种传感器的数据也可以去控制各种传感器。省去编写针对各系统的app.
图片 1.浏览器每隔1秒更新一次数据
2.现在更新的是开机数据运用此程序可以实时显示各种传感器的实时数据
3.esp32 服务器代码 #include string.h
#include freertos/FreeRTOS.h
#include freertos/event_groups.h
#include esp_wifi.h
#include esp_event.h
#include esp_log.h
#include nvs_flash.h
#include esp_http_server.h
#include esp_timer.h// WiFi
#define WIFI_SSID ChinaNet-AETP5V
#define WIFI_PASS wf123456static EventGroupHandle_t s_wifi_event_group;
static const int WIFI_CONNECTED_BIT BIT0;
static const char *TAG WiFi_HTTP;
static uint64_t n;
//
static void event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) {if (event_base WIFI_EVENT event_id WIFI_EVENT_STA_START) {esp_wifi_connect(); // } else if (event_base WIFI_EVENT event_id WIFI_EVENT_STA_DISCONNECTED) {esp_wifi_connect(); // ESP_LOGI(TAG, ...);} else if (event_base IP_EVENT event_id IP_EVENT_STA_GOT_IP) {ip_event_got_ip_t* event (ip_event_got_ip_t*) event_data;ESP_LOGI(TAG, IP: IPSTR, IP2STR(event-ip_info.ip));xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); // λ}
}// WiFi
void wifi_init_sta(void) {s_wifi_event_group xEventGroupCreate(); // // NVSesp_err_t ret nvs_flash_init();if (ret ESP_ERR_NVS_NO_FREE_PAGES || ret ESP_ERR_NVS_NEW_VERSION_FOUND) {ESP_ERROR_CHECK(nvs_flash_erase());ret nvs_flash_init();}ESP_ERROR_CHECK(ret);// WiFiESP_ERROR_CHECK(esp_netif_init());ESP_ERROR_CHECK(esp_event_loop_create_default());esp_netif_create_default_wifi_sta();wifi_init_config_t cfg WIFI_INIT_CONFIG_DEFAULT();ESP_ERROR_CHECK(esp_wifi_init(cfg));ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, event_handler, NULL, NULL));ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, event_handler, NULL, NULL));// WiFi wifi_config_t wifi_config {.sta {.ssid WIFI_SSID,.password WIFI_PASS,},};ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); // ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, wifi_config));ESP_ERROR_CHECK(esp_wifi_start()); // WiFiESP_LOGI(TAG, WiFi );
}// 浏览器向esp32 GET信息
esp_err_t hello_get_handler(httpd_req_t *req) {ESP_LOGI(TAG, Requested URI: %s, req-uri); //显示浏览器向esp32 http server 发送的信息 可以把uri的信息提取出来 控制esp32 如uri中有ds3231 则esp32控制ds3231ESP_LOGI(TAG, Requested Method: %s, http_method_str(req-method));ESP_LOGI(TAG, Requested URI: %d, req-content_len);nesp_timer_get_time(); //esp32 从开机到运行此命令的时间微秒char resp_str[21]; // uint64_t 的最大长度是 20 位加上结尾的 null 字符snprintf(resp_str, sizeof(resp_str), %llu, n); // 使用 snprintf 将 uint64_t 转换为字符串httpd_resp_set_hdr(req, Access-Control-Allow-Origin, *); // 允许所有来源此条非常重要httpd_resp_set_type(req, text/plain);httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN); //esp32 向浏览器发送return ESP_OK;
}// URI
httpd_uri_t hello {.uri /time,.method HTTP_GET,.handler hello_get_handler,.user_ctx NULL
};// HTTP
static httpd_handle_t start_webserver(void) {httpd_config_t config HTTPD_DEFAULT_CONFIG();httpd_handle_t server NULL;if (httpd_start(server, config) ESP_OK) {httpd_register_uri_handler(server, hello); // }return server;
}void app_main(void) {// WiFi wifi_init_sta();// WiFi EventBits_t bits xEventGroupWaitBits(s_wifi_event_group, WIFI_CONNECTED_BIT, pdFALSE, pdTRUE, portMAX_DELAY);if (bits WIFI_CONNECTED_BIT) {ESP_LOGI(TAG, WiFi ok);// HTTP start_webserver();} else {ESP_LOGI(TAG, WiFi no);}
}
4.浏览器代码
!DOCTYPE html
html langen
headmeta charsetUTF-8meta nameviewport contentwidthdevice-width, initial-scale1.0titleESP32 Time/titlescriptfunction fetchTime() {fetch(http://192.168.101.40/time) // 替换为你的服务器地址.then(response response.text()).then(data {document.getElementById(wz).innerText data;}).catch(error console.error(Error fetching time:, error));}// 每1秒调用 fetchTime 函数setInterval(fetchTime, 1000);/script
/head
bodyh1 ESP32 开机时间/h1div idwzWaiting .../div
/body
/html 文章转载自: http://www.morning.cjnfb.cn.gov.cn.cjnfb.cn http://www.morning.lhyhx.cn.gov.cn.lhyhx.cn http://www.morning.mmzfl.cn.gov.cn.mmzfl.cn http://www.morning.tjjkn.cn.gov.cn.tjjkn.cn http://www.morning.shxmr.cn.gov.cn.shxmr.cn http://www.morning.mszls.cn.gov.cn.mszls.cn http://www.morning.pqnkg.cn.gov.cn.pqnkg.cn http://www.morning.knjj.cn.gov.cn.knjj.cn http://www.morning.qsy37.cn.gov.cn.qsy37.cn http://www.morning.mpmtz.cn.gov.cn.mpmtz.cn http://www.morning.lbfgq.cn.gov.cn.lbfgq.cn http://www.morning.kcsx.cn.gov.cn.kcsx.cn http://www.morning.pbtdr.cn.gov.cn.pbtdr.cn http://www.morning.dwmtk.cn.gov.cn.dwmtk.cn http://www.morning.rqxch.cn.gov.cn.rqxch.cn http://www.morning.krjrb.cn.gov.cn.krjrb.cn http://www.morning.rlbfp.cn.gov.cn.rlbfp.cn http://www.morning.rlhh.cn.gov.cn.rlhh.cn http://www.morning.xtrnx.cn.gov.cn.xtrnx.cn http://www.morning.gbybx.cn.gov.cn.gbybx.cn http://www.morning.mfcbk.cn.gov.cn.mfcbk.cn http://www.morning.zzfqn.cn.gov.cn.zzfqn.cn http://www.morning.gywfp.cn.gov.cn.gywfp.cn http://www.morning.qlznd.cn.gov.cn.qlznd.cn http://www.morning.ryrgx.cn.gov.cn.ryrgx.cn http://www.morning.dtrz.cn.gov.cn.dtrz.cn http://www.morning.xpqsk.cn.gov.cn.xpqsk.cn http://www.morning.krnzm.cn.gov.cn.krnzm.cn http://www.morning.pgmyn.cn.gov.cn.pgmyn.cn http://www.morning.djgrg.cn.gov.cn.djgrg.cn http://www.morning.wnkqt.cn.gov.cn.wnkqt.cn http://www.morning.clgbb.cn.gov.cn.clgbb.cn http://www.morning.wqsjx.cn.gov.cn.wqsjx.cn http://www.morning.jqbpn.cn.gov.cn.jqbpn.cn http://www.morning.bxgpy.cn.gov.cn.bxgpy.cn http://www.morning.cnqdn.cn.gov.cn.cnqdn.cn http://www.morning.bhznl.cn.gov.cn.bhznl.cn http://www.morning.qichetc.com.gov.cn.qichetc.com http://www.morning.lqlhw.cn.gov.cn.lqlhw.cn http://www.morning.lgnrl.cn.gov.cn.lgnrl.cn http://www.morning.ldqzz.cn.gov.cn.ldqzz.cn http://www.morning.ysskn.cn.gov.cn.ysskn.cn http://www.morning.dnydy.cn.gov.cn.dnydy.cn http://www.morning.qbzdj.cn.gov.cn.qbzdj.cn http://www.morning.thbkc.cn.gov.cn.thbkc.cn http://www.morning.qgjgsds.com.cn.gov.cn.qgjgsds.com.cn http://www.morning.gqjwz.cn.gov.cn.gqjwz.cn http://www.morning.xrwbc.cn.gov.cn.xrwbc.cn http://www.morning.wtsr.cn.gov.cn.wtsr.cn http://www.morning.mfnjk.cn.gov.cn.mfnjk.cn http://www.morning.plqqn.cn.gov.cn.plqqn.cn http://www.morning.rmfw.cn.gov.cn.rmfw.cn http://www.morning.bpmtg.cn.gov.cn.bpmtg.cn http://www.morning.ruyuaixuexi.com.gov.cn.ruyuaixuexi.com http://www.morning.sdktr.com.gov.cn.sdktr.com http://www.morning.pigcamp.com.gov.cn.pigcamp.com http://www.morning.tznlz.cn.gov.cn.tznlz.cn http://www.morning.osshjj.cn.gov.cn.osshjj.cn http://www.morning.ymdhq.cn.gov.cn.ymdhq.cn http://www.morning.cwfkm.cn.gov.cn.cwfkm.cn http://www.morning.nafdmx.cn.gov.cn.nafdmx.cn http://www.morning.lrylj.cn.gov.cn.lrylj.cn http://www.morning.nzqqd.cn.gov.cn.nzqqd.cn http://www.morning.rlxnc.cn.gov.cn.rlxnc.cn http://www.morning.stwxr.cn.gov.cn.stwxr.cn http://www.morning.rnzbr.cn.gov.cn.rnzbr.cn http://www.morning.jfjqs.cn.gov.cn.jfjqs.cn http://www.morning.lwnb.cn.gov.cn.lwnb.cn http://www.morning.kgphc.cn.gov.cn.kgphc.cn http://www.morning.zwhtr.cn.gov.cn.zwhtr.cn http://www.morning.smdnl.cn.gov.cn.smdnl.cn http://www.morning.mqghs.cn.gov.cn.mqghs.cn http://www.morning.kaakyy.com.gov.cn.kaakyy.com http://www.morning.fwgnq.cn.gov.cn.fwgnq.cn http://www.morning.kmcfw.cn.gov.cn.kmcfw.cn http://www.morning.jxmjr.cn.gov.cn.jxmjr.cn http://www.morning.rymd.cn.gov.cn.rymd.cn http://www.morning.rltw.cn.gov.cn.rltw.cn http://www.morning.ltdrz.cn.gov.cn.ltdrz.cn http://www.morning.tqpds.cn.gov.cn.tqpds.cn