【问题标题】:ESP-12 wifi module's the GPIO pin is HIGH on turning it on but should be LOWESP-12 wifi 模块的 GPIO 引脚在打开时为高电平,但应为低电平
【发布时间】:2017-09-15 01:31:45
【问题描述】:

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

这是我的电路图

这是我的 Arduino 代码:

/*
*  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
}

【问题讨论】:

标签: arduino gpio esp8266


【解决方案1】:

感谢@gre_gor 指出这一点

pinMode() 配置特定引脚,digitalWrite() 设置它们的HIGH/LOW 状态。
文档可以在here找到。

试试这个:

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

【讨论】:

    猜你喜欢
    • 2017-03-15
    • 1970-01-01
    • 2013-11-13
    • 1970-01-01
    • 2015-07-26
    • 2018-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多