更改ESP8266相关路由信息

前言

ESP8266上电的时候可以通过手机的WIFI看到ESP8266的SSID,但是因为加了密码而连接不上,用户可以通过对ESP8266编程来更改ESP8266的SSID以及密码。

相关数据类型介绍

一、softap_config

成员名称数据类型功能
ssiduint8大小的数组WIFI AP SSID
passworduint8大小的数组WIFI AP 密码
ssid_lenuint8SSID 长度
channeluint8通道
authmodeAUTH_MODE-
ssid_hiddenuint8-
max_connectionuint8-
beacon_intervaluint16-

结构体原型

struct softap_config 
{
    uint8 ssid[32];
    uint8 password[64];
    uint8 ssid_len; // Note: Recommend to set it according to your ssid
    uint8 channel;  // Note: support 1 ~ 13
    AUTH_MODE authmode; // Note: Don't support AUTH_WEP in softAP mode.
    uint8 ssid_hidden;  // Note: default 0
    uint8 max_connection;   // Note: default 4, max 4
    uint16 beacon_interval; // Note: support 100 ~ 60000 ms, default 100
};

相关API介绍

一、wifi_softap_get_config

功能查询 ESP8266 WiFi soft-AP 接口当前的配置-
函数原型bool wifi_softap_get_config(struct softap_config *config)-
参数*config获取到的soft-ap当前的配置
返回值true获取配置成功
-false获取配置失败

二、wifi_softap_set_config

功能配置 ESP8266 WiFi soft-AP并保存到flash中-
函数原型bool wifi_softap_set_config(struct softap_config *config)-
参数*configsoft-ap的配置
返回值true配置成功
-false配置失败
注意需要在 ESP8266 soft_AP使能的情况下调用此API-

三、 wifi_softap_set_config_current

功能配置 ESP8266 WiFi soft-AP不保存到flash中-
函数原型bool wifi_softap_set_config_current(struct softap_config *config)-
参数*configsoft-ap的配置
返回值true配置成功
-false配置失败
注意需要在 ESP8266 soft_AP使能的情况下调用此API-

相关例程

#include "ets_sys.h"
#include "osapi.h"
#include "user_interface.h"
#include "uart.h"
#include "gpio.h"
#include "app_socket.h"
#include "string.h"

void user_init( void )
{
    struct softap_config config;

    /** 初始化ESP8266工作模式 */
    if ( wifi_get_opmode() != 0x03 )
    {
        wifi_set_opmode( 0x03 );
    }

    /** 获取当前soft-AP配置 */
    wifi_softap_get_config( &config );

    /** 清零参数 */
    os_memset( config.ssid, 0, 32 );
    os_memset( config.password, 0, 64 );

    /** 写入数据 */
    os_memcpy( config.ssid, "SMART_SOCKET_ID", os_strlen( "SMART_SOCKET_ID" ) );
    os_memcpy( config.password, "12345678", os_strlen( "12345678" ) );

    config.authmode = AUTH_WPA_WPA2_PSK;
    config.ssid_len = 0;  // or its actual length
    config.beacon_interval = 100;
    config.max_connection = 4;  // how many stations can connect to ESP8266 softAP at most.

    /** 写入配置 */
    wifi_softap_set_config( &config );
}

参考资料

[1]. ESP8266Non-OS SDK API参考

要发送MySQL数据库变化的通知给ESP8266,你需要使用以下步骤: 1. 在MySQL数据库中设置触发器(Trigger),当数据库中的数据发生变化时,触发器会发送通知。 2. 创建一个Web API,用来接收触发器发送的通知,并将通知转发给ESP8266。 3. 在ESP8266上编写代码,通过HTTP请求来接收来自Web API的通知。 具体步骤如下: 1. 在MySQL数据库中创建一个触发器,当数据库中的数据发生变化时,触发器会发送通知。 ``` CREATE TRIGGER `my_trigger` AFTER INSERT ON `my_table` FOR EACH ROW BEGIN CALL http_post('http://your-web-api.com/notify', 'data=changed'); END; ``` 上述代码中的`http_post`是一个自定义的存储过程,用来发送HTTP POST请求。具体实现可参考此链接:https://stackoverflow.com/questions/27178127/mysql-trigger-to-call-a-rest-api。 2. 创建一个Web API,用来接收MySQL触发器发送的通知,并将通知转发给ESP8266。 你可以使用任何你熟悉的Web开发框架来实现Web API。下面是一个使用Node.js和Express框架的示例: ``` const express = require('express'); const app = express(); app.use(express.json()); app.post('/notify', (req, res) => { const data = req.body.data; // 从HTTP请求中获取通知数据 // 将通知转发给ESP8266 // ... res.send('OK'); }); app.listen(3000, () => { console.log('Web API listening on port 3000'); }); ``` 上述代码中的`/notify`路由用来接收MySQL触发器发送的通知,通知数据包含在HTTP POST请求的请求体中。你需要在这个路由中添加代码,将通知数据转发给ESP8266。 3. 在ESP8266上编写代码,通过HTTP请求来接收来自Web API的通知。 你可以使用ESP8266的HTTP客户端库,如ESP8266HTTPClient库,来发送HTTP请求。下面是一个示例代码: ``` #include <ESP8266WiFi.h> #include <ESP8266HTTPClient.h> void setup() { Serial.begin(9600); WiFi.begin("your-wifi-ssid", "your-wifi-password"); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting to WiFi..."); } } void loop() { HTTPClient http; http.begin("http://your-web-api.com/notify"); int statusCode = http.POST("data=changed"); if (statusCode == 200) { Serial.println("Notification sent"); } else { Serial.println("Failed to send notification"); } http.end(); delay(1000); } ``` 上述代码中的`http.begin`方法用来设置HTTP请求的URL,`http.POST`方法用来发送HTTP POST请求,并将通知数据包含在请求体中。你需要修改这个代码,以便接收来自Web API的实际通知数据,并对ESP8266进行相关操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值