esp8266自定义工程框架
如何再起官方rtos sdk中规范的建立一个工程了,现在我们基于上一篇博客-esp8266对接阿里云平台做下规范。
首先将user_main.c中mqtt连接部分分离出来,写入user_mqtt.c中,分离是注意头文件包含,及一些全局变量设置。
user_main.c
/*
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 <stdio.h>
//#include "esp_system.h"
//#include "freertos/FreeRTOS.h"
//#include "freertos/task.h"
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "MQTTClient.h"
#include "user_mqtt.h"
#define DBG_C(...) printf("[%s,%d]",__FUNCTION__,__LINE__);printf(__VA_ARGS__)
/* FreeRTOS event group to signal when we are connected & ready to make a request */
EventGroupHandle_t wifi_event_group;
/* The event group allows multiple bits for each event,
but we only care about one event - are we connected
to the AP with an IP? */
int CONNECTED_BIT = BIT0;
#define MQTT_CLIENT_THREAD_NAME "mqtt_client_thread"
#define MQTT_CLIENT_THREAD_STACK_WORDS 4096
#define MQTT_CLIENT_THREAD_PRIO 8
static const char *TAG = "example";
static esp_err_t event_handler(void *ctx, system_event_t *event)
{
switch (event->event_id) {
case SYSTEM_EVENT_STA_START:
DBG_C("SYSTEM_EVENT_STA_START\n");
esp_wifi_connect();
break;
case SYSTEM_EVENT_STA_GOT_IP:
DBG_C("SYSTEM_EVENT_STA_GOT_IP\n");
xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
/* This is a workaround as ESP32 WiFi libs don't currently
auto-reassociate. */
DBG_C("SYSTEM_EVENT_STA_DISCONNECTED\n");
esp_wifi_connect();
xEvent