2023-07-20 10:17:11 +08:00
|
|
|
|
/* WiFi station Example
|
|
|
|
|
|
|
|
|
|
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, this
|
|
|
|
|
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
|
|
|
CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
*/
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
|
#include "freertos/task.h"
|
|
|
|
|
#include "freertos/event_groups.h"
|
|
|
|
|
#include "esp_system.h"
|
|
|
|
|
#include "esp_wifi.h"
|
|
|
|
|
#include "esp_event.h"
|
|
|
|
|
#include "esp_log.h"
|
|
|
|
|
#include "nvs_flash.h"
|
|
|
|
|
#include "lwip/err.h"
|
|
|
|
|
#include "lwip/sys.h"
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include "driver/ledc.h"
|
|
|
|
|
#include "esp_err.h"
|
|
|
|
|
#include "can_network.h"
|
|
|
|
|
#include "freertos/queue.h"
|
|
|
|
|
#include "esp_check.h"
|
|
|
|
|
#include "soc/rtc.h"
|
|
|
|
|
#include "driver/mcpwm.h"
|
2024-02-19 09:58:15 +08:00
|
|
|
|
#include "driver/uart.h"
|
2023-11-15 22:06:47 +08:00
|
|
|
|
/*蓝牙相关的头文件*/
|
2024-02-23 17:21:13 +08:00
|
|
|
|
// #include "esp_bt.h"
|
|
|
|
|
// #include "esp_gap_ble_api.h"
|
|
|
|
|
// #include "esp_gatts_api.h"
|
|
|
|
|
// #include "esp_bt_defs.h"
|
|
|
|
|
// #include "esp_bt_main.h"
|
|
|
|
|
//#include "ble_server.h"
|
|
|
|
|
#include "stm32/config.h"
|
|
|
|
|
#include "ble_gatts_server.h"
|
2023-11-15 22:06:47 +08:00
|
|
|
|
|
2024-02-19 09:58:15 +08:00
|
|
|
|
|
2023-07-20 10:17:11 +08:00
|
|
|
|
/* The examples use WiFi configuration that you can set via project configuration menu
|
|
|
|
|
|
|
|
|
|
If you'd rather not, just change the below entries to strings with
|
|
|
|
|
the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
|
|
|
|
|
*/
|
2023-07-20 13:51:10 +08:00
|
|
|
|
#define EXAMPLE_ESP_WIFI_SSID CONFIG_ESP_WIFI_SSID
|
|
|
|
|
#define EXAMPLE_ESP_WIFI_PASS CONFIG_ESP_WIFI_PASSWORD
|
|
|
|
|
#define EXAMPLE_ESP_MAXIMUM_RETRY CONFIG_ESP_MAXIMUM_RETRY
|
2023-07-20 10:17:11 +08:00
|
|
|
|
|
|
|
|
|
#if CONFIG_ESP_WIFI_AUTH_OPEN
|
|
|
|
|
#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_OPEN
|
|
|
|
|
#elif CONFIG_ESP_WIFI_AUTH_WEP
|
|
|
|
|
#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WEP
|
|
|
|
|
#elif CONFIG_ESP_WIFI_AUTH_WPA_PSK
|
|
|
|
|
#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_PSK
|
|
|
|
|
#elif CONFIG_ESP_WIFI_AUTH_WPA2_PSK
|
|
|
|
|
#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_PSK
|
|
|
|
|
#elif CONFIG_ESP_WIFI_AUTH_WPA_WPA2_PSK
|
|
|
|
|
#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_WPA2_PSK
|
|
|
|
|
#elif CONFIG_ESP_WIFI_AUTH_WPA3_PSK
|
|
|
|
|
#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA3_PSK
|
|
|
|
|
#elif CONFIG_ESP_WIFI_AUTH_WPA2_WPA3_PSK
|
|
|
|
|
#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_WPA3_PSK
|
|
|
|
|
#elif CONFIG_ESP_WIFI_AUTH_WAPI_PSK
|
|
|
|
|
#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WAPI_PSK
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/* FreeRTOS event group to signal when we are connected*/
|
2023-11-15 22:06:47 +08:00
|
|
|
|
//static EventGroupHandle_t s_wifi_event_group;
|
2023-07-20 10:17:11 +08:00
|
|
|
|
|
|
|
|
|
/* The event group allows multiple bits for each event, but we only care about two events:
|
|
|
|
|
* - we are connected to the AP with an IP
|
|
|
|
|
* - we failed to connect after the maximum amount of retries */
|
|
|
|
|
#define WIFI_CONNECTED_BIT BIT0
|
2023-07-20 13:51:10 +08:00
|
|
|
|
#define WIFI_FAIL_BIT BIT1
|
|
|
|
|
#define GPIO_4G_PWR GPIO_NUM_4
|
2024-01-20 17:44:50 +08:00
|
|
|
|
#define PWR_4G_ON (0)
|
|
|
|
|
#define PWR_4G_OFF (1)
|
2023-07-20 10:17:11 +08:00
|
|
|
|
|
|
|
|
|
static const char *TAG = "main";
|
|
|
|
|
|
2023-11-15 22:06:47 +08:00
|
|
|
|
//static int s_retry_num = 0;
|
2023-07-20 10:17:11 +08:00
|
|
|
|
|
|
|
|
|
extern void wifi_init_softap(void);
|
|
|
|
|
|
2023-07-20 13:51:10 +08:00
|
|
|
|
void PWR_4G_Init(void);
|
2023-07-20 10:17:11 +08:00
|
|
|
|
extern void can_init(void);
|
|
|
|
|
extern void FLOW_init();
|
|
|
|
|
extern void DEPTH_init();
|
|
|
|
|
extern void BL0939_init(void);
|
|
|
|
|
extern void ads1220_task_start(void);
|
|
|
|
|
extern void ModBusTCPSlave_init(void);
|
|
|
|
|
extern esp_err_t i2c_master_init(void);
|
|
|
|
|
extern void config_load(void);
|
|
|
|
|
extern void uart0_modbus_slave_init(void);
|
2023-11-15 22:06:47 +08:00
|
|
|
|
extern void bt_client_init(void);
|
|
|
|
|
//extern void ESP32_Uart_Receive_Data(void);
|
2023-07-20 10:17:11 +08:00
|
|
|
|
uint32_t rtc_clk_apb_freq;
|
|
|
|
|
|
2024-02-23 17:21:13 +08:00
|
|
|
|
|
|
|
|
|
extern cal_4_20ma_t *cal_4_20ma;//电流数据结构体
|
|
|
|
|
extern flow_config_t *flow_config;//流量数据结构体
|
|
|
|
|
extern depth_config_t *depth_config;//深度数据结构体
|
|
|
|
|
|
2023-07-20 10:17:11 +08:00
|
|
|
|
void app_main(void)
|
|
|
|
|
{
|
2023-07-20 13:51:10 +08:00
|
|
|
|
// Initialize NVS
|
|
|
|
|
esp_err_t ret = nvs_flash_init();
|
|
|
|
|
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
|
|
|
|
|
{
|
2023-07-20 10:17:11 +08:00
|
|
|
|
ESP_ERROR_CHECK(nvs_flash_erase());
|
|
|
|
|
ret = nvs_flash_init();
|
2023-07-20 13:51:10 +08:00
|
|
|
|
}
|
|
|
|
|
ESP_ERROR_CHECK(ret);
|
|
|
|
|
ESP_ERROR_CHECK(i2c_master_init());
|
2024-02-23 17:21:13 +08:00
|
|
|
|
//restore_default();
|
2024-01-20 17:44:50 +08:00
|
|
|
|
config_load();//读取保存在FRAM里的数据
|
2023-07-20 13:51:10 +08:00
|
|
|
|
ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
|
2024-02-19 09:58:15 +08:00
|
|
|
|
// rtc_clk_apb_freq = rtc_clk_apb_freq_get();
|
|
|
|
|
// ESP_LOGI(TAG, "rtc_clk_apb_freq=%u", rtc_clk_apb_freq);
|
2023-11-15 22:06:47 +08:00
|
|
|
|
|
|
|
|
|
wifi_init_softap();//ok
|
2023-07-20 13:51:10 +08:00
|
|
|
|
// wifi_init_sta();
|
|
|
|
|
// can_init();
|
2024-02-23 17:21:13 +08:00
|
|
|
|
// PWR_4G_Init();
|
2023-07-20 13:51:10 +08:00
|
|
|
|
ModBusTCPSlave_init();
|
2023-11-15 22:06:47 +08:00
|
|
|
|
|
2024-02-23 17:21:13 +08:00
|
|
|
|
ads1220_task_start(); /* 两路电流AD采样获取瞬时流量,计算累计流量 */
|
|
|
|
|
BL0939_init(); /* 实时采集通道电流,根据depth_config_t中配置的启动与结束电流,以及持续时间进行move_t机器开关状态的判断 */
|
|
|
|
|
DEPTH_init(); /* 编码器计算深度,同时进行流量的按深度计数,以及record的记录 */
|
|
|
|
|
FLOW_init(); /* 两一种计算流量的方法,计算累计流量 */
|
2023-11-15 22:06:47 +08:00
|
|
|
|
|
2023-07-20 13:51:10 +08:00
|
|
|
|
uart0_modbus_slave_init();
|
2023-11-15 22:06:47 +08:00
|
|
|
|
|
2024-02-23 17:21:13 +08:00
|
|
|
|
// ble_server_init();
|
|
|
|
|
ble_gatts_server_init();
|
|
|
|
|
|
|
|
|
|
// bt_client_init();//ok
|
2023-11-15 22:06:47 +08:00
|
|
|
|
//ESP32_Uart_Receive_Data();
|
|
|
|
|
|
2023-07-20 10:17:11 +08:00
|
|
|
|
}
|
2023-07-20 13:51:10 +08:00
|
|
|
|
|
|
|
|
|
void PWR_4G_Init(void)
|
|
|
|
|
{
|
2024-02-23 17:21:13 +08:00
|
|
|
|
|
|
|
|
|
ESP_LOGI(TAG, "HELLO~");
|
|
|
|
|
save_cal_4_20ma();
|
2023-07-20 13:51:10 +08:00
|
|
|
|
// zero-initialize the config structure.
|
2024-01-20 17:44:50 +08:00
|
|
|
|
gpio_config_t io_conf = {
|
|
|
|
|
// disable interrupt
|
|
|
|
|
.intr_type = GPIO_INTR_DISABLE,
|
|
|
|
|
// set as output mode
|
|
|
|
|
.mode = GPIO_MODE_OUTPUT,
|
|
|
|
|
// bit mask of the pins that you want to set,e.g.GPIO18/19
|
|
|
|
|
.pin_bit_mask = GPIO_4G_PWR,
|
|
|
|
|
// disable pull-down mode
|
|
|
|
|
.pull_down_en = 0,
|
|
|
|
|
// disable pull-up mode
|
|
|
|
|
.pull_up_en = 0,
|
|
|
|
|
};
|
2024-02-23 17:21:13 +08:00
|
|
|
|
|
2023-07-20 13:51:10 +08:00
|
|
|
|
// configure GPIO with the given settings
|
|
|
|
|
gpio_config(&io_conf);
|
2024-01-20 17:44:50 +08:00
|
|
|
|
gpio_set_level(GPIO_4G_PWR, PWR_4G_OFF);
|
|
|
|
|
vTaskDelay(pdMS_TO_TICKS(1000));
|
|
|
|
|
gpio_set_level(GPIO_4G_PWR, PWR_4G_ON);
|
2023-07-20 13:51:10 +08:00
|
|
|
|
}
|