esp8266
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
// 测试HTTP请求用的URL
// #define URL "http://162.14.107.118:8086/PC/modifyFoodPrice/0/6"// 测试HTTP请求用的URL
// 设置wifi接入信息(请根据您的WiFi信息进行修改)
const char* ssid = "hhhcc";
const char* password = "2001317829";
int counter=0; // 计数器
void setup() {
// 初始化串口
Serial.begin(115200);
//设置ESP8266工作模式为无线终端模式
WiFi.mode(WIFI_STA);
//开始连接wifi
WiFi.begin(ssid, password);
//等待WiFi连接,连接成功打印IP
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("");
Serial.print("WiFi Connected!");
}
void loop() {
// 每loop循环一次,计数器变量加1
delay(1000);
counter = counter+1;
// 输出变量
String txt=Serial.readString();
esp8266Http( txt);
Serial.println(txt);
delay(1000);
}
// 发送HTTP请求并且将服务器响应通过串口输出
void esp8266Http(String price){
String URL= "http://162.14.107.118:8086/PC/modifyFoodPrice/0/";
URL=URL+price;
WiFiClient client;
//创建 HTTPClient 对象
HTTPClient httpClient;
//配置请求地址。此处也可以不使用端口号和PATH而单纯的
httpClient.begin(client,URL);
Serial.print("URL: "); Serial.println(URL);
//启动连接并发送HTTP请求
int httpCode = httpClient.GET();
Serial.print("Send GET request to URL: ");
Serial.println(URL);
//如果服务器响应OK则从服务器获取响应体信息并通过串口输出
//如果服务器不响应OK则将服务器响应状态码通过串口输出
if (httpCode == HTTP_CODE_OK) {
String responsePayload = httpClient.getString();
Serial.println("Server Response Payload: ");
Serial.println(responsePayload);
} else {
Serial.println("Server Respose Code:");
Serial.println(httpCode);
}
//关闭ESP8266与服务器连接
httpClient.end();
}
stm32代码
#include "string.h"
#include "system.h"
#include "SysTick.h"
#include "led.h"
#include "usart.h"
/*******************************************************************************
* 函 数 名 : main
* 函数功能 : 主函数
* 输 入 : 无
* 输 出 : 无
*******************************************************************************/
int main()
{
u8 i=0;
u16 data=1234;
float fdata=12.34;
char str[]="Hello World!";
SysTick_Init(72);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //中断优先级分组 分2组
LED_Init();
USART1_Init(115200);//串口波特率115200
GPIO_ResetBits(LED_PORT,LED_PIN); //LED=0 亮
delay_ms(1000); //延迟
//以上是初始化
//---------------------------------------------------------
//发送指令,让ESP8266连接到阿里云
// printf("AT+RST\r\n"); //第一步 复位 只用一次
// delay_ms(2000); //延迟
//
// printf("AT+CWMODE=1\r\n"); //第二步 模式 只用一次
// delay_ms(2000); //延迟
printf("AT\r\n"); //第一步 AT
delay_ms(2000); //延迟
printf("AT+GMR\r\n"); //第一步 版本号
delay_ms(2000); //延迟
printf("AT+CWJAP=\"hhhcc\",\"2001317829\"\r\n"); //第四步 wifi热点
delay_ms(5000); //延迟
delay_ms(5000); //延迟
delay_ms(5000); //延迟
delay_ms(5000); //延迟
printf("6");
delay_ms(50000); //延迟
printf("7");
delay_ms(50000); //延迟
printf("8");
delay_ms(50000); //延迟
//---------------------------------------------------------
while(1)
{
if(USART_RX_STA&0x8000)//判断接收到完整的字符串 完成
{
if(strstr((const char*)USART_RX_BUF,"on"))//判断阿里云发下来的数据里是否有on
{
// GPIO_ResetBits(LED_PORT,LED_PIN); //LED=0 亮
printf("12");
}
if(strstr((const char*)USART_RX_BUF,"off"))//判断阿里云发下来的数据里是否有off
{
// GPIO_SetBits(LED_PORT,LED_PIN); //LED=1 灭
printf("20");
}
memset(USART_RX_BUF,0,600); //将接收区清0
USART_RX_STA=0;
}
}
}
//第一步 AT+RST
//
//第二步 AT+CWMODE=1
//第三步 AT+CIPSNTPCFG=1,8,"ntp1.aliyun.com"
//第四步 AT+CWJAP="WiFi名字","WiFi密码"
//第五步 AT+MQTTUSERCFG=0,1,"NULL","用户名","密码",0,0,""
//第六步 AT+MQTTCLIENTID=0,"ClienId" //clientId第二个参数注意每个逗号前加分隔符\
//第七步 AT+MQTTCONN=0,"连接域名",1883,1
//AT+MQTTSUB=0,"topic",1 //订阅指令
//AT+MQTTPUB=0,"topic","AJson格式内容",1,0 //发布指令
//注意: "前面要加转义字符 \前面要加转义字符
接线
接线图
stm32 esp8266 usb-ttl
A9(Tx) Rx
A10(Rx) TXD
Tx RXD