完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
我需要从我的 esp8266 向 Shelly 插头发出 http 请求才能打开它。如何使用 RTOS SDK 2.1.0 从我的 esp8266 发送 http 请求?
请求网址:http://{shellyplug-ip}/relay/0?turn=on |
|
相关推荐
1个回答
|
|
要使用ESP8266的RTOS SDK 2.1.0发送HTTP请求,您可以按照以下步骤操作: 1. 确保您已安装ESP8266 RTOS SDK 2.1.0。 2. 创建一个新的项目并设置您的开发环境。 3. 将以下代码添加到您的主应用程序文件中(例如:main.c): ```c #include "espressif/esp_common.h" #include "esp/uart.h" #include "FreeRTOS.h" #include "task.h" #include "lwip/sockets.h" #include "lwip/sys.h" #include "lwip/api.h" #include static const char *shelly_plug_ip = "YOUR_SHELLY_PLUG_IP"; static const char *relay_command = "/relay/0?turn=on"; void http_request_task(void *pvParameters) { char *get_request = (char *)malloc(256); sprintf(get_request, "GET %s HTTP/1.1rnHost: %srnConnection: closernrn", relay_command, shelly_plug_ip); struct sockaddr_in server_addr; int sock = socket(AF_INET, SOCK_STREAM, 0); if (sock != -1) { server_addr.sin_family = AF_INET; server_addr.sin_port = htons(80); server_addr.sin_addr.s_addr = inet_addr(shelly_plug_ip); if (connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) == 0) { write(sock, get_request, strlen(get_request)); free(get_request); char receive_buffer[1024]; int bytes_received = recv(sock, receive_buffer, sizeof(receive_buffer) - 1, 0); if (bytes_received > 0) { receive_buffer[bytes_received] = ' |