电子说
程序的入口先进行串口初始化和按键的初始化,以及LED的初始化。串口要初始化波特率,按键初始化配网按键,用于短按配网,长按重新配网,LED只要用于判断模块是否进入配网模式以及是否配网完成。//程序入口
void ICACHE_FLASH_ATTR user_init(void)
{
uart_init(115200, 115200);
os_delay_us(60000);
keyInit();
set_uart_cb(uart_cb);
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, FUNC_GPIO12); //GPIO12初始化
GPIO_OUTPUT_SET(GPIO_ID_PIN(12), 0);//低电平
get_mac();//获取MAC地址
wifi_set_opmode(STATION_MODE);
//设置wifi信息存储数量,最大为5个
wifi_station_ap_number_set(2);
mqtt_init();
set_wifistate_cb(wifi_connect_cb, wifi_disconnect_cb);
}
/**
* 按键短按回调
*/
LOCAL void ICACHE_FLASH_ATTR key1ShortPress(void) {
start_smartconfig(smartconfig_cd);
INFO("start_smartconfig ");
}
/**
* 按键长按回调
*/
LOCAL void ICACHE_FLASH_ATTR key1LongPress(void) {
start_smartconfig(smartconfig_cd);
INFO("start_smartconfig ");
}
/**
* 按键初始化
*/
LOCAL void ICACHE_FLASH_ATTR keyInit(void) {
//设置按键数量
set_key_num(1);
//长按、短按的按键回调
key_add(D5, NULL, key1ShortPress);
key_add(D5, NULL, key1LongPress);
}
由于找不到最新的代码。这里的长按我没做处理,应该是断开WiFi重新进入配网模式, 或者软复位模块,再进入start_smartconfig()函数:
/**
* 开始Smartconfig配置
* @param cd: Smartconfig状态回调
* @retval None
*/
void ICACHE_FLASH_ATTR start_smartconfig(smartconfig_cd_t cd) {
smartconfig_flag = 1;
smartconfig_set_type(SC_TYPE_ESPTOUCH_AIRKISS); //SC_TYPE_ESPTOUCH,SC_TYPE_AIRKISS,SC_TYPE_ESPTOUCH_AIRKISS
wifi_station_disconnect();
wifi_set_opmode(STATION_MODE);
finish_cd = cd;
smartconfig_start(smartconfig_done);
os_timer_disarm(&OS_Timer_Wifichange); // 关闭定时器
if(connect_flag == 1){
w_disconnect();
connect_flag = 0;
}
os_timer_disarm(&OS_Timer_SM); // 关闭定时器
os_timer_setfn(&OS_Timer_SM, (os_timer_func_t *) sm_wait_time, NULL);// 设置定时器
os_timer_arm(&OS_Timer_SM, 1000, 1); // 使能定时器
}
/**
* Smartconfig 状态处理
* @param status: 状态
* @param *pdata: AP数据
* @retval None
*/
void ICACHE_FLASH_ATTR
smartconfig_done(sc_status status, void *pdata) {
switch (status) {
case SC_STATUS_WAIT:
INFO("SC_STATUS_WAIT ");
break;
case SC_STATUS_FIND_CHANNEL:
INFO("SC_STATUS_FIND_CHANNEL ");
break;
case SC_STATUS_GETTING_SSID_PSWD:
INFO("SC_STATUS_GETTING_SSID_PSWD ");
sc_type *type = pdata;
if (*type == SC_TYPE_ESPTOUCH) {
INFO("SC_TYPE:SC_TYPE_ESPTOUCH ");
} else {
INFO("SC_TYPE:SC_TYPE_AIRKISS ");
}
break;
case SC_STATUS_LINK:
INFO("SC_STATUS_LINK ");
sm_comfig_status = SM_STATUS_GETINFO;
struct station_config *sta_conf = pdata;
wifi_station_set_config(sta_conf);
wifi_station_disconnect();
wifi_station_connect();
break;
case SC_STATUS_LINK_OVER:
sm_comfig_status = SM_STATUS_FINISH;
INFO("SC_STATUS_LINK_OVER ");
if (pdata != NULL) {
//SC_TYPE_ESPTOUCH
uint8 phone_ip[4] = { 0 };
os_memcpy(phone_ip, (uint8*) pdata, 4);
INFO("Phone ip: %d.%d.%d.%d ", phone_ip[0], phone_ip[1],
phone_ip[2], phone_ip[3]);
} else {
//SC_TYPE_AIRKISS - support airkiss v2.0
airkiss_start_discover();
}
smartconfig_stop();
smartconfig_flag = 0;
connect_flag = 0;
os_timer_disarm(&OS_Timer_SM); // 关闭定时器
finish_cd(sm_comfig_status);
os_timer_arm(&OS_Timer_Wifichange, 3000, 1); // 使能定时器
break;
}
}
/**
* WIFI连接回调
*/
void wifi_connect_cb(void){
INFO("wifi connect! ");
os_printf("----- WiFi连接成功,打开绿灯--- ");
GPIO_OUTPUT_SET(GPIO_ID_PIN(12), 1);
MQTT_Connect(&mqttClient);
}
/**
* WIFI断开回调
*/
void wifi_disconnect_cb(void){
INFO("wifi disconnect! ");
os_printf("----- WiFi断开,关闭绿灯--- ");
GPIO_OUTPUT_SET(GPIO_ID_PIN(12), 0);
MQTT_Disconnect(&mqttClient);
}
连接MQTT服务器:
/**
* MQTT初始化
*/
void ICACHE_FLASH_ATTR mqtt_init(void) {
MQTT_InitConnection(&mqttClient, MQTT_HOST, MQTT_PORT, DEFAULT_SECURITY);
MQTT_InitClient(&mqttClient, mac_str, MQTT_USER,MQTT_PASS, MQTT_KEEPALIVE, 1);
MQTT_InitLWT(&mqttClient, lwt_topic, LWT_MESSAGE, 0, 0);
MQTT_OnConnected(&mqttClient, mqttConnectedCb);
MQTT_OnDisconnected(&mqttClient, mqttDisconnectedCb);
MQTT_OnPublished(&mqttClient, mqttPublishedCb);
MQTT_OnData(&mqttClient, mqttDataCb);
}
void ICACHE_FLASH_ATTR
MQTT_InitConnection(MQTT_Client *mqttClient, uint8_t* host, uint32_t port, uint8_t security)
{
uint32_t temp;
INFO("MQTT_InitConnection ");
os_memset(mqttClient, 0, sizeof(MQTT_Client));
temp = os_strlen(host);
mqttClient->host = (uint8_t*)os_zalloc(temp + 1);
os_strcpy(mqttClient->host, host);
mqttClient->host[temp] = 0;
mqttClient->port = port;
mqttClient->security = security;
}
void ICACHE_FLASH_ATTR
MQTT_InitClient(MQTT_Client *mqttClient, uint8_t* client_id, uint8_t* client_user, uint8_t* client_pass, uint32_t keepAliveTime, uint8_t cleanSession)
{
uint32_t temp;
INFO("MQTT_InitClient ");
os_printf("CD MQTT_InitClient++++++++++++++++++++++ ");
os_memset(&mqttClient->connect_info, 0, sizeof(mqtt_connect_info_t));
temp = os_strlen(client_id);
mqttClient->connect_info.client_id = (uint8_t*)os_zalloc(temp + 1);
os_strcpy(mqttClient->connect_info.client_id, client_id);
mqttClient->connect_info.client_id[temp] = 0;
if (client_user)
{
temp = os_strlen(client_user);
mqttClient->connect_info.username = (uint8_t*)os_zalloc(temp + 1);
os_strcpy(mqttClient->connect_info.username, client_user);
mqttClient->connect_info.username[temp] = 0;
}
if (client_pass)
{
temp = os_strlen(client_pass);
mqttClient->connect_info.password = (uint8_t*)os_zalloc(temp + 1);
os_strcpy(mqttClient->connect_info.password, client_pass);
mqttClient->connect_info.password[temp] = 0;
}
mqttClient->connect_info.keepalive = keepAliveTime;
mqttClient->connect_info.clean_session = cleanSession;
mqttClient->mqtt_state.in_buffer = (uint8_t *)os_zalloc(MQTT_BUF_SIZE);
mqttClient->mqtt_state.in_buffer_length = MQTT_BUF_SIZE;
mqttClient->mqtt_state.out_buffer = (uint8_t *)os_zalloc(MQTT_BUF_SIZE);
mqttClient->mqtt_state.out_buffer_length = MQTT_BUF_SIZE;
mqttClient->mqtt_state.connect_info = &mqttClient->connect_info;
mqtt_msg_init(&mqttClient->mqtt_state.mqtt_connection, mqttClient->mqtt_state.out_buffer, mqttClient->mqtt_state.out_buffer_length);
QUEUE_Init(&mqttClient->msgQueue, QUEUE_BUFFER_SIZE);
system_os_task(MQTT_Task, MQTT_TASK_PRIO, mqtt_procTaskQueue, MQTT_TASK_QUEUE_SIZE);
system_os_post(MQTT_TASK_PRIO, 0, (os_param_t)mqttClient);
}
WiFi连接成功和失败会触发不同的回调函数:
串口透传:/**
* MQTT连接回调
*/
void mqttConnectedCb(uint32_t *args) {
MQTT_Client* client = (MQTT_Client*) args;
INFO("MQTT: Connected ");
MQTT_Publish(client, birth_topic, BIRTH_MESSAGE, os_strlen(BIRTH_MESSAGE), 0,0);
MQTT_Subscribe(client,ota_topic, 0);
if(updata_status_check()){
MQTT_Publish(client, ota_topic, "updata_finish", os_strlen("updata_finish"), 0,0);
}
}
/**
* MQTT断开连接回调
*/
void mqttDisconnectedCb(uint32_t *args) {
MQTT_Client* client = (MQTT_Client*) args;
INFO("MQTT: Disconnected ");
}
/**
* MQTT发布消息回调
*/
void mqttPublishedCb(uint32_t *args) {
MQTT_Client* client = (MQTT_Client*) args;
INFO("MQTT: Published ");
}
其他问题:连接的服务器地址,端口号等信息需要写在代码里烧录进模块,这些信息在在mqtt_config.h文件中定义。/**
* MQTT接收数据回调(用于OTA升级和串口透传)
*/
void mqttDataCb(uint32_t *args, const char* topic, uint32_t topic_len,
const char *data, uint32_t data_len) {
char *topicBuf = (char*) os_zalloc(topic_len + 1), *dataBuf =
(char*) os_zalloc(data_len + 1);
uint8 *pdata = (uint8*)data;
uint16 len = data_len;
uart0_tx_buffer(pdata, len);//串口输出
MQTT_Client* client = (MQTT_Client*) args;
os_memcpy(topicBuf, topic, topic_len);
topicBuf[topic_len] = 0;
os_memcpy(dataBuf, data, data_len);
dataBuf[data_len] = 0;
// INFO("Receive topic: %s, data: %s ", topicBuf, dataBuf);
//data = {"url"="http://yourdomain.com:9001/ota/"}
if (os_strcmp(topicBuf, ota_topic) == 0) {
char url_data[200];
if(get_josn_str(dataBuf,"url",url_data)){
// INFO("ota_start ");
ota_upgrade(url_data,ota_finished_callback);
}
}
os_free(topicBuf);
os_free(dataBuf);
}
/**
* ota升级回调
*/
void ICACHE_FLASH_ATTR ota_finished_callback(void * arg) {
struct upgrade_server_info *update = arg;
if (update->upgrade_flag == true) {
INFO("OTA Success ! rebooting! ");
system_upgrade_reboot();
} else {
INFO("OTA Failed! ");
}
}
责任编辑:xj
原文标题:什么?单片机还在裸奔?ESP8266纯串口透传,助力设备上云端
文章出处:【微信公众号:嵌入式ARM】欢迎添加关注!文章转载请注明出处。
全部0条评论
快来发表一下你的评论吧 !