【问题标题】:Sending data from phpmyadmin database to arduino uno using esp8266-01 wifi module使用 esp8266-01 wifi 模块将数据从 phpmyadmin 数据库发送到 arduino uno
【发布时间】:2018-10-23 16:14:36
【问题描述】:

我正在使用带有 arduino uno 的 Ir 传感器。通过 esp8266-01,我成功地将 IR 传感器的状态 (即清除和障碍) 发送到使用 xampp 控制面板创建的 phpmyadmin 数据库。我正在使用 AT 命令将数据从 arduino 发送到数据库。现在我希望我的 arduino 从 phpmyadmin 数据库中获取数据并更改 LED 灯的状态。如何使用 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;
}

【问题讨论】:

    标签: php arduino arduino-uno arduino-esp8266


    【解决方案1】:

    如果我理解您的问题,您需要添加您的代码 ip 服务器 ex.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);
      }
    }
    

    希望对你有帮助

    另外有一个关于 arduino 和 mysql 的链接 youtube https://www.youtube.com/watch?v=6hi9Wf99hfg

    【讨论】:

      【解决方案2】:

      我只是用 nodemcu esp8266 替换了 arduino uno 板。使用 ESP8266HTTPCLIENT.H、ESP8266.H 之类的库解决了我的所有问题。现在我可以从数据库中获取数据,反之亦然。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-05
        • 1970-01-01
        • 1970-01-01
        • 2012-07-03
        • 1970-01-01
        相关资源
        最近更新 更多