6c3381fa70684fa782759d6bf6f5fe41.jpg

 

c3 作为http client ,当gpio0 脚接地  发送/open   开信号到http server

0脚为高电平,发送/close  关信号

下一步就是改进优化client端,尽快减少功耗,采用纽扣电池供电。最终把client端移置到家用开关盒中。


#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"
#include "driver/gpio.h"
// WiFi 
#define WIFI_SSID "ChinaNet-AETP5V"
#define WIFI_PASS "wf123456"

#include "esp_http_client.h"

#define SERVER_IP      "192.168.101.41"
#define URL_OPEN       "http://" SERVER_IP "/open"
#define URL_CLOSE      "http://" SERVER_IP "/close"

static const char *TAG = "HTTP_CLIENT";
 
static EventGroupHandle_t s_wifi_event_group;
static const int WIFI_CONNECTED_BIT = BIT0;


//----------------------
#define GPIO_in  0            


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();  // 
	
	//  NVS
	esp_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);
	
	//  WiFi
	ESP_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());  //  WiFi
	
	ESP_LOGI(TAG, "WiFi ");
}


// HTTP GET 请求函数
void http_get_request(const char *url) {
    esp_http_client_config_t config = {
        .url = url,
    };
    esp_http_client_handle_t client = esp_http_client_init(&config);

    // 发送 GET 请求
    esp_err_t err = esp_http_client_perform(client);

    if (err == ESP_OK) {
    //    ESP_LOGI(TAG, "HTTP GET Status = %lld, content_length = %lld",
     //            esp_http_client_get_status_code(client),
      //           esp_http_client_get_content_length(client));
	  ;
    } else {
        ESP_LOGE(TAG, "HTTP GET request failed: %s", esp_err_to_name(err));
    }

    // 关闭客户端
    esp_http_client_cleanup(client);
}

void app_main(void) {
	//gpio 0 脚注册
    gpio_config_t io_conf = {};      
    io_conf.intr_type = GPIO_INTR_DISABLE;
    io_conf.mode = GPIO_MODE_INPUT;
    io_conf.pin_bit_mask = 1ULL<<GPIO_in;  
    io_conf.pull_down_en = 0;
    io_conf.pull_up_en = 1;
    gpio_config(&io_conf);
	
	wifi_init_sta();
	
	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");
	} else {
		ESP_LOGI(TAG, "WiFi no");
	}
    http_get_request(URL_CLOSE);     //关断
    while(1){
		if(!gpio_get_level(GPIO_in)){
			http_get_request(URL_OPEN);     //开
		}else{
			http_get_request(URL_CLOSE);
	  }
	  vTaskDelay(1000/portTICK_PERIOD_MS);	
}
}


 

 

 

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部