Arduino 基于 ESP8266 配置WIFI模块

本文介绍如何使用Arduino和ESP8266 WiFi模块配置并控制LED灯。通过HTTP请求,可以远程切换LED状态,实现物联网(IoT)的基本应用。文章提供了完整的代码示例和步骤说明。
Arduino 基于 ESP8266 配置WIFI模块
  • 选择 【文件】->【示例】->【ESP8266WIFI】->【WiFiWebServer】
  • 用Arduino新建一个文件,将刚打开的的WIFIWebServer的内容复制过去
  • 修改 ssid 和 password 为自家路由器的名称以及密码
  • 将程序上传到 ESP8266 开发板中
  • 最后就可以通过网站 [ip]/gpio/1 或 [ip]/gpio/0 控制灯开关
# http://192.168.0.57/gpio/1
# ip 地址在序列库中可以看到
  • 附上代码
/*
 *  This sketch demonstrates how to set up a simple HTTP-like server.
 *  The server will set a GPIO pin depending on the request
 *    http://server_ip/gpio/0 will set the GPIO2 low,
 *    http://server_ip/gpio/1 will set the GPIO2 high
 *  server_ip is the IP address of the ESP8266 module, will be 
 *  printed to Serial when the module is connected.
 */

#include <ESP8266WiFi.h>

const char* ssid = "YUCHANJIUYE";
const char* password = "19283746";

// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);

void setup() {
  Serial.begin(115200);
  delay(10);

  // prepare GPIO2
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, 0);
  
  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  
  // Start the server
  server.begin();
  Serial.println("Server started");

  // Print the IP address
  Serial.println(WiFi.localIP());
}

void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
  
  // Wait until the client sends some data
  Serial.println("new client");
  while(!client.available()){
    delay(1);
  }
  
  // Read the first line of the request
  String req = client.readStringUntil('\r');
  Serial.println(req);
  client.flush();
  
  // Match the request
  int val;
  if (req.indexOf("/gpio/0") != -1)
    val = 0;
  else if (req.indexOf("/gpio/1") != -1)
    val = 1;
  else {
    Serial.println("invalid request");
    client.stop();
    return;
  }

  // Set GPIO2 according to the request
  digitalWrite(LED_BUILTIN, val);
  
  client.flush();

  // Prepare the response
  String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO is now ";
  s += (val)?"high":"low";
  s += "</html>\n";

  // Send the response to the client
  client.print(s);
  delay(1);
  Serial.println("Client disonnected");

  // The client will actually be disconnected 
  // when the function returns and 'client' object is detroyed
}

转载于:https://www.cnblogs.com/GetcharZp/p/11584582.html

### 使用ArduinoESP8266 WiFi模块与大语言模型通信的实践方法 在物联网领域,ArduinoESP8266模块是实现设备联网的重要工具。通过这些硬件,可以构建一个能够与大语言模型(如GPT、Bert等)进行通信的系统。以下是具体实现方法: #### 1. 硬件准备 需要以下硬件组件: - Arduino Uno开发板[^1] - ESP8266 WiFi模块(如NodeMCU或Wemos D1 Mini)[^1] - 连接线及电源供应 #### 2. 软件环境搭建 - **Arduino IDE安装**:确保已安装最新版本的Arduino IDE。 - **ESP8266支持库安装**:按照参考资料[^2]的方法,在Arduino IDE中添加ESP8266硬件支持包。 - **网络库导入**:使用`ESP8266WiFi.h`库来管理WiFi连接[^3]。 #### 3. 网络配置 为了使ESP8266连接到WiFi网络,需要编如下代码片段: ```cpp #include <ESP8266WiFi.h> const char* ssid = "your_wifi_ssid"; // 替换为你的WiFi名称 const char* password = "your_wifi_password"; // 替换为你的WiFi密码 void setup() { Serial.begin(115200); WiFi.begin(ssid, password); // 开始连接WiFi while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting to WiFi..."); } Serial.println("Connected to WiFi"); } void loop() { // 主循环逻辑 } ``` 上述代码实现了WiFi连接功能,在串口监视器中输出连接状态[^3]。 #### 4. API接口调用 大语言模型通常通过RESTful API提供服务。例如,OpenAI的GPT模型可以通过HTTP请求与其交互。以下是调用API的基本步骤: - **获取API密钥**:从大语言模型提供商处申请API密钥。 - **发送HTTP请求**:使用`HttpClient`库或其他相关库发送POST请求[^5]。 示例代码如下: ```cpp #include <ESP8266WiFi.h> #include <ESP8266HTTPClient.h> const char* ssid = "your_wifi_ssid"; const char* password = "your_wifi_password"; const char* apiEndpoint = "https://api.openai.com/v1/completions"; const char* apiKey = "your_api_key"; void setup() { Serial.begin(115200); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting to WiFi..."); } Serial.println("Connected to WiFi"); } void loop() { if (WiFi.status() == WL_CONNECTED) { HTTPClient http; http.begin(apiEndpoint); http.addHeader("Content-Type", "application/json"); http.addHeader("Authorization", String("Bearer ") + apiKey); String payload = "{\"model\":\"text-davinci-003\",\"prompt\":\"Hello world\",\"max_tokens\":50}"; int httpResponseCode = http.POST(payload); if (httpResponseCode > 0) { String response = http.getString(); Serial.println(response); } else { Serial.print("Error on sending POST: "); Serial.println(httpResponseCode); } http.end(); } delay(5000); // 每隔5秒执行一次 } ``` #### 5. 数据处理与显示 通过串口或LCD屏幕,可以将大语言模型返回的结果展示给用户。例如,使用`LiquidCrystal`库控制LCD屏幕显示文本[^1]。 --- #### 注意事项 - 确保ESP8266模块Arduino Uno之间的串口通信正常工作[^4]。 - 在实际应用中,可能需要对API响应进行解析格式化,以适配特定需求。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值