首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ESP-12 wifi模块的GPIO引脚在打开时为高电平,但应为低电平

ESP-12 wifi模块的GPIO引脚在打开时为高电平,但应为低电平
EN

Stack Overflow用户
提问于 2016-09-12 20:13:44
回答 1查看 5K关注 0票数 1

我需要帮助。我正在使用ESP-12 WiFi模块自动控制我房间的灯光。但是当我打开我的ESP-12 WiFi模块时,我为继电器输入设置的GPIO2引脚是高的(默认情况下它应该是低的),然后冻结。但是如果我在打开ESP后连接GPIO引脚,那么它工作正常。我怎样才能避免这个问题。它是连接问题还是与代码有关??

这是我的电路图

这是我的Arduino代码:

代码语言:javascript
复制
/*
*  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 = "myssid";
const char* password = "mypassword";
IPAddress ip(192, 168, 1, 10); // where xx is the desired IP Address
IPAddress gateway(192, 168, 1, 254); // set gateway to match your network
IPAddress subnet(255, 255, 255, 255); // set subnet mask to match your network 

// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);
int status = LOW;
const int pin = 2;
void setup() {
  Serial.begin(115200);
  delay(100);

  // prepare GPIO2
  pinMode(pin, OUTPUT);
  pinMode(pin, LOW);


  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.config(ip, gateway, subnet);
  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() {
  delay(3000);
  // 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('$');
  Serial.println(req);
  client.flush();

  // Match the request

  if (req.indexOf("status") != -1 || req.indexOf("/status") != -1)
    Serial.println("Switch is: "+ status);
  else if (req.indexOf("on") != -1 || req.indexOf("/on") != -1)
    status = HIGH;
  else if (req.indexOf("off") != -1 || req.indexOf("/off") != -1)
    status = LOW;
  else {
    Serial.println("invalid request");
    client.stop();
    return;
  }

  // Set GPIO2 according to the request
  digitalWrite(pin, status);
  // Prepare the response
  String response = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nSwitch is now ";
  response += status;
  response += "</html>\n";
  // Send the response to the client
  client.print(response);
  client.flush();

  delay(1);
  Serial.println("Client disonnected"+ status);

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

回答 1

Stack Overflow用户

发布于 2016-09-12 20:22:56

归功于@gre_gor指出了这一点

pinMode()配置特定引脚,digitalWrite()设置其HIGH/LOW状态。

文档可以在here上找到。

试试这个:

代码语言:javascript
复制
// prepare GPIO2 
pinMode(pin, OUTPUT); 
digitalWrite(pin, LOW);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39450064

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档