STM32F103单片机怎么用ESP8266实现手机WIFI无线APP控灯亮灭及亮度
时间: 2023-11-18 16:04:23 浏览: 212
实现STM32F103单片机通过ESP8266实现手机WIFI无线APP控灯亮灭及亮度的方法如下:
1. 硬件连接
将ESP8266模块的TXD和RXD分别连接到STM32F103单片机的USART1的RXD和TXD上,将ESP8266的VCC和GND分别连接到单片机的3.3V和GND上。
2. 软件实现
(1) 初始化ESP8266模块
使用单片机的USART1与ESP8266模块通信,发送AT指令初始化ESP8266模块,如下所示:
```c
void esp8266_init(void)
{
USART_SendString(USART1,"AT+RST\r\n"); //重启ESP8266
delay_ms(500);
USART_SendString(USART1,"AT+CWMODE=1\r\n"); //设置WIFI模式为STA
delay_ms(500);
USART_SendString(USART1,"AT+CIPMUX=0\r\n"); //设置单连接模式
delay_ms(500);
USART_SendString(USART1,"AT+CIPSTART=\"TCP\",\"192.168.1.100\",8080\r\n"); //连接APP服务器
delay_ms(500);
}
```
(2) 实现TCP/IP协议栈
使用lwIP协议栈实现TCP连接和数据传输,通过AT指令实现与ESP8266模块的通信,如下所示:
```c
void lwip_demo(void)
{
struct netconn *conn;
struct netbuf *buf;
char *data;
uint16_t len;
//创建TCP连接
conn = netconn_new(NETCONN_TCP);
netconn_connect(conn, IP_ADDR_ANY, 8080);
//循环接收数据
while (1) {
if (netconn_recv(conn, &buf) == ERR_OK) {
netbuf_data(buf, (void**)&data, &len);
//处理接收到的数据
//...
netbuf_delete(buf);
}
}
}
```
(3) 实现控制逻辑
通过数据解析实现控制逻辑,将APP发送的控制指令解析成PWM输出和开关控制,如下所示:
```c
void control_logic(char *data, uint16_t len)
{
//解析JSON数据
cJSON *root = cJSON_Parse(data);
cJSON *cmd = cJSON_GetObjectItem(root, "cmd");
cJSON *param = cJSON_GetObjectItem(root, "param");
if (strcmp(cmd->valuestring, "switch") == 0) {
//控制灯的开关
if (strcmp(param->valuestring, "on") == 0) {
GPIO_SetBits(GPIOA, GPIO_Pin_0);
} else {
GPIO_ResetBits(GPIOA, GPIO_Pin_0);
}
} else if (strcmp(cmd->valuestring, "brightness") == 0) {
//控制灯的亮度
uint8_t brightness = param->valueint;
TIM_SetCompare1(TIM2, brightness*10);
}
cJSON_Delete(root);
}
```
3. 编写APP代码
在APP中实现控制界面,通过HTTP协议向ESP8266发送控制指令,如下所示:
```java
public class MainActivity extends AppCompatActivity {
private String host = "192.168.1.100";
private int port = 8080;
private Button btnOn, btnOff, btnBright;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnOn = findViewById(R.id.btnOn);
btnOff = findViewById(R.id.btnOff);
btnBright = findViewById(R.id.btnBright);
btnOn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String url = "http://" + host + ":" + port + "/?cmd=switch¶m=on";
new HttpTask().execute(url);
}
});
btnOff.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String url = "http://" + host + ":" + port + "/?cmd=switch¶m=off";
new HttpTask().execute(url);
}
});
btnBright.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String url = "http://" + host + ":" + port + "/?cmd=brightness¶m=" + 50;
new HttpTask().execute(url);
}
});
}
private class HttpTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
try {
URL url = new URL(urls[0]);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
conn.connect();
if (conn.getResponseCode() == 200) {
InputStream is = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
return sb.toString();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
}
```
需要注意的是,以上代码仅为示例代码,实现功能需根据具体需求进行修改和完善。
阅读全文