我将Ir传感器与arduino uno一起使用。通过esp8266-01,我已成功将IR传感器(即清除和障碍)的状态发送到使用xampp控制面板创建的phpmyadmin数据库中。将数据从arduino发送到数据库。现在,我希望我的arduino从phpmyadmin数据库中获取数据并更改Led light的状态。如何使用AT命令从服务器获取响应并相应地更改传感器的状态。
Arduino代码
#include <SoftwareSerial.h>
#define RX 10
#define TX 11
String AP = "Tenda_2704A8";
String PASS = "********";
String Data;
int countTrueCommand;
int countTimeCommand;
boolean found = false;
int LED = 13; // Use the onboard Uno LED
int isObstaclePin = 7; // This is our input pin
int isObstacle = HIGH; // HIGH MEANS NO OBSTACLE
SoftwareSerial esp8266(RX, TX);
void setup() {
pinMode(LED, OUTPUT);
pinMode(isObstaclePin, INPUT);
Serial.begin(9600);
esp8266.begin(115200);
sendCommand("AT", 5, "OK");
sendCommand("AT+CWMODE=1", 5, "OK");
sendCommand("AT+CWJAP=\"" + AP + "\",\"" + PASS + "\"", 20, "OK");
}
void loop() {
String output;
isObstacle = digitalRead(isObstaclePin);
if (isObstacle == LOW)
{
output = "obstacle";
Serial.println("OBSTACLE!!, OBSTACLE!!");
digitalWrite(LED, HIGH);
}
else
{
output = "clear";
Serial.println("clear");
digitalWrite(LED, LOW);
}
Data = "GET /project/ajax/arduino.php?value="+output;
sendCommand("AT+CIPMUX=1",5,"OK");
sendCommand("AT+CIPSTART=0,\"TCP\",\"192.168.0.104\",80",4,"OK");
sendCommand("AT+CIPSEND=0," +String(Data.length()+4),2,">");
esp8266.println(Data);delay(100);countTrueCommand++;
sendCommand("AT+CIPCLOSE=0",2,"OK");
}
void sendCommand(String command, int maxTime, char readReplay[]) {
Serial.print(countTrueCommand);
Serial.print(". at command => ");
Serial.print(command);
Serial.print(" ");
while (countTimeCommand < (maxTime * 1))
{
esp8266.println(command);//at+cipsend
if (esp8266.find(readReplay)) //ok
{
found = true;
break;
}
countTimeCommand++;
}
if (found == true)
{
Serial.println("Yes");
countTrueCommand++;
countTimeCommand = 0;
}
if (found == false)
{
Serial.println("Fail");
countTrueCommand = 0;
countTimeCommand = 0;
}
found = false;
}
答案 0 :(得分:0)
如果我理解您的问题,则需要在代码IP服务器中添加192.168.1.4 在我的测试中,我使用了此库#include WiFiClientSecure.h并编写了此代码来接收来自我的php文件的响应...(使用esp8266)
// HTML
char servername[] = "192.168.1.4";
WiFiClient client; //library WiFiClientSecure
.....your code
void send(int temp, int hum) {
if (client.connect(servername, 80)) {
client.println(String("GET /myfolder/index.php?temp=") + temp + String("&hum=") + hum);
client.println("Host: servername");
client.println("Connection: close");
client.println("");
while (client.connected()) {
while (client.available()) {
Serial.write(client.read());//read the response that you receive...in this case on serial monitor
}
}
}
else {
Serial.println("connession faliled");
}
//stop the client
client.stop();
while (client.status() != 0) {
delay(5);
}
}
我希望这会有所帮助
另外还有一个YouTube链接,它谈论arduino和mysql https://www.youtube.com/watch?v=6hi9Wf99hfg
答案 1 :(得分:0)
我只是用nodemcu esp8266替换了arduino uno board。使用像ESP8266HTTPCLIENT.H,ESP8266.H这样的库解决了我的所有问题。现在我能够从数据库中获取数据,反之亦然。