我正在尝试制作一个简单的硬件,以使用我拥有的数据库远程打开和关闭房间里的灯。为此,我想在ESP-01板上使用ESP8266。
对于工作台测试,我有一个2安培的5v电源。我使用3.3V稳压器(LD33V)来打开ESP-01,并设法与具有“打开/关闭标记”的数据库建立连接。配备LED和330欧姆电阻,就像魅力一样。
片刻过后(我还没有测量,但是只有几分钟),板子完全关闭了。我意识到这可能会消耗大量电流,因此我将相同的代码上传到NodeMCU板上,并用手机充电器为其供电,打开和关闭内置LED。发生了同样的事情。
我猜这可能是软件问题,因为经过这些测试,它似乎不是硬件。也许我需要设置任何虚拟标志或其他东西。
ESP-01的插针中包含以下内容:
VCC:来自稳压器的3.3V
GND:来自稳压器的0V
GPI02:330欧姆电阻器和LED
GPIO0:浮动(是的,我在编程时确实将其接地)
TX / RX:浮动
RST:Vcc
CH_PD(启用):Vcc
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>
#define led 2 //pin of the external led
// Inicialização do WiFi
const char* ssid = "xxxxxx";
const char* pass = "xxxxxx";
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // made-up MAC
WiFiClient client;
// Inicialização do MySQL
MySQL_Connection conn((Client *)&client);
// Create an instance of the cursor passing in the connection
MySQL_Cursor cur = MySQL_Cursor(&conn);
IPAddress server_addr(XXX, XXX , XXX, XXX); // MySQL server IP
char user[] = "xxxxxx"; // MySQL user
char password[] = "xxxxxx"; // MySQL password
char BANCODEDADOS[] = "USE xxxxxx";
void toggle_lamp(void){
// FAZ UM SELECT SIMPLES
char query[] = "SELECT STATUS FROM devices WHERE ID = 1";
MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
cur_mem->execute(query);
Serial.println("");
Serial.print("Executando query: ");
Serial.println(query);
// Fetch the columns (required) but we don't use them.
column_names *columns = cur_mem->get_columns();
// Read the row (we are only expecting the one)
row_values *row = NULL;
//char* result = NULL;
int lamp_status;
do {
row = cur_mem->get_next_row();
if (row != NULL) {
// result = row->values[0];
lamp_status = atoi(row->values[0]);
}
} while (row != NULL);
// Deleting the cursor also frees up memory used
delete cur_mem;
// USA O RESULTADO PARA ACENDER UM LED
//int lamp_status = atoi(result);
//Serial.println(result);
Serial.println(lamp_status);
if(lamp_status == 1){
digitalWrite(led,HIGH);
}
else{
lamp_status = 0;
digitalWrite(led,LOW);
}
}
void setup(void){
// Define a saída como output
pinMode(led,OUTPUT);
// Inicializa a porta Serial
Serial.begin(115200);
Serial.println("");
// Inicializa a conexão WiFi
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// REALIZA A CONEXÃO COM O MYSQL
if (conn.connect(server_addr, 3306, user, password))
{
delay(1000);
// Initiate the query class instance
MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
cur_mem->execute(BANCODEDADOS);
Serial.println("");
Serial.println("Conectado ao MySQL");
Serial.print("Usuario:");
Serial.println(user);
}
else
{
Serial.println("A conexão falhou");
conn.close();
}
} // end setup()
void loop(void){
toggle_lamp();
delay(1250);
}