dtdfj08626 2018-10-23 16:14 采纳率: 0%
浏览 328

使用esp8266-01 wifi模块将数据从phpmyadmin数据库发送到arduino uno

I am using Ir sensor with arduino uno.Through esp8266-01 i successfully send status of IR sensor (i.e Clear and Obstacle) to phpmyadmin database created using xampp control panel.I am using AT command to send data from arduino to database.Now I want my arduino to fetch data from phpmyadmin database and change the status of Led light.How to get response from server using AT command and accordingly change status of sensor.

Arduino code

#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;
}
  • 写回答

2条回答 默认 最新

  • dongpo1203 2018-10-23 22:18
    关注

    If i understood your question you need add in your code ip server ex.192.168.1.4 In my test i have used this library #include WiFiClientSecure.h and wrote this code to receive response from my php file...(using 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);
      }
    }
    

    i hope that this is helpful

    inother there is a link youtube that talk about arduino and mysql https://www.youtube.com/watch?v=6hi9Wf99hfg

    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大