如何解决无法在 NodeMCU ESP8266 中进行数字写入
当我使用 ESP8266 的一个例子来做一些闪烁的 LED 时。它工作正常,但是当我连接到 WIFI 和 MySQL 数据库时,它无法进行内置的数字写入 LED。
这是我在 NodeMCU ESP8266 中的代码:
//----------------------------------------Include the NodeMCU ESP8266 Library
//----------------------------------------see here: https://www.youtube.com/watch?v=8jMr94B8iN0 to add NodeMCU ESP8266 library and board
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266HTTPClient.h>
//----------------------------------------
//--> Defining an On Board LED (GPIO2 = D4),used for indicators when the process of connecting to a wifi router
#define LED_D8 15 //--> Defines an LED Pin. D8 = GPIO15
//----------------------------------------SSID and Password of your WiFi router.
const char* ssid = "Lenko1"; //--> Your wifi name or SSID.
const char* password = "heryawan1"; //--> Your wifi password.
//----------------------------------------
//----------------------------------------Web Server address / IPv4
// If using IPv4,press Windows key + R then type cmd,then type ipconfig (If using Windows OS).
const char *host = "http://up3manado.com/";
//----------------------------------------
void setup() {
// put your setup code here,to run once:
Serial.begin(115200);
delay(500);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid,password); //--> Connect to your WiFi router
Serial.println("");
pinMode(LED_BUILTIN,OUTPUT); //--> On Board LED port Direction output
digitalWrite(LED_BUILTIN,HIGH); //--> Turn on Led On Board
pinMode(LED_D8,OUTPUT); //--> LED port Direction output
digitalWrite(LED_D8,LOW); //--> Turn off Led
//----------------------------------------Wait for connection
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
//----------------------------------------Make the On Board Flashing LED on the process of connecting to the wifi router.
digitalWrite(LED_BUILTIN,LOW);
delay(250);
digitalWrite(LED_BUILTIN,HIGH);
delay(250);
//----------------------------------------
}
//----------------------------------------
digitalWrite(LED_BUILTIN,HIGH); //--> Turn off the On Board LED when it is connected to the wifi router.
//----------------------------------------If successfully connected to the wifi router,the IP Address that will be visited is displayed in the serial monitor
Serial.println("");
Serial.print("Successfully connected to : ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.println();
//----------------------------------------
}
void loop() {
// put your main code here,to run repeatedly:
HTTPClient http; //--> Declare object of class HTTPClient
//----------------------------------------Getting Data from MySQL Database
String GetAddress,LinkGet,getData;
int id = 0; //--> ID in Database
GetAddress = "NodeMCU_Get_Database/GetData.php";
LinkGet = host + GetAddress; //--> Make a Specify request destination
getData = "ID=" + String(id);
Serial.println("----------------Connect to Server-----------------");
Serial.println("Get LED Status from Server or Database");
Serial.print("Request Link : ");
Serial.println(LinkGet);
http.begin(LinkGet); //--> Specify request destination
http.addHeader("Content-Type","application/x-www-form-urlencoded"); //Specify content-type header
int httpCodeGet = http.POST(getData); //--> Send the request
String payloadGet = http.getString(); //--> Get the response payload from server
Serial.print("Response Code : "); //--> If Response Code = 200 means Successful connection,if -1 means connection failed. For more information see here : https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
Serial.println(httpCodeGet); //--> Print HTTP return code
Serial.print("Returned data from Server : ");
Serial.println(payloadGet); //--> Print request response payload
if (payloadGet == "1") {
digitalWrite(LED_BUILTIN,HIGH); //--> Turn on Led
}
if (payloadGet == "0") {
digitalWrite(LED_BUILTIN,LOW); //--> Turn off Led
}
//----------------------------------------
Serial.println("----------------Closing Connection----------------");
http.end(); //--> Close connection
Serial.println();
Serial.println("Please wait 5 seconds for the next connection.");
Serial.println();
delay(5000); //--> GET Data at every 5 seconds
}
问题是,如果payloadGet == "1",我不能做digitalWrite HIGH。事实是我可以从MySQL获取payloadGet的值,值为1。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。