如何通过firebase和php更改arduino esp8266-01它的wifi SSID和密码?

时间:2019-06-24 14:33:07

标签: php firebase arduino-esp8266

我正在设置arduino的wifi SSID和密码,特别是esp-01,方法是将数据(SSID和密码)发送到firebase,然后发送到PHP和arduino < / strong>

我尝试了许多可能的解决方案,并阅读了与我想要成为结果有关的文章,但没有一个起作用

我正在使用一个应用程序将数据发送到Firebase

给出Arduino和PHP(firebase)的代码

  

Arduino代码

    #include <SoftwareSerial.h>
   #include <DHT.h>;
    SoftwareSerial Serial1(10, 11);
   #define Trash "Trash3"
   #define DHTTYPE DHT11
   #define DHTPIN 2
   #define TRIGGER_PIN 4
   #define ECHO_PIN 3

    DHT dht(DHTPIN, DHTTYPE, 11);

    float humidity, temp_f;
    int distance;
    long duration;

     String buff(64);
     String getStr(128);

    void setup() {

     Serial.begin(9600);
     Serial1.begin(9600);
    delay(2000);
     Serial1.setTimeout(5000);
    dht.begin();
     pinMode(TRIGGER_PIN, OUTPUT);
    pinMode(ECHO_PIN, INPUT);

     if (!connectWiFi()) {
    Serial.println("Can not connect to the WiFi.");
     while (true);
    }
    Serial.println("OK, Connected to WiFi.");

     sendCommand("AT+CIPSTA?");
    sendCommand("AT+CIPSTAMAC?");}

     void loop() {

    temp_f = dht.readTemperature();
     humidity = dht.readHumidity();
    digitalWrite(TRIGGER_PIN, LOW);
     delayMicroseconds(2);
    digitalWrite(TRIGGER_PIN, HIGH);
     delayMicroseconds(10);
    digitalWrite(TRIGGER_PIN, LOW);

     duration = pulseIn(ECHO_PIN, HIGH);
    distance = duration * 0.034 / 2;

     if (sendCommand("AT+CIPSTART=\"TCP\",\"gg.com\",80")) {
    Serial.println("connected to Cloud");

     getStr = "GET /upload.php?trash=";
    getStr += Trash;
     getStr += "&distance=";
    getStr += distance;
     getStr += "&temp_f=";
    getStr += temp_f;
     getStr += "&humidity=";
    getStr += humidity;
     getStr += " HTTP/1.1\r\n";
    getStr += "Host: gg.com\r\n\r\n";

    buff = "AT+CIPSEND=";
     buff += getStr.length();
    if (sendCommand(buff.c_str()) && Serial1.find(">")) { 

      Serial.println(getStr);
      Serial1.print(getStr);
      if (Serial1.find("+IPD,")) { 
        int l = Serial1.parseInt();
        while (l > 0) {
          if (Serial1.available()) {
            Serial.write(Serial1.read());
            l--;
          }
        }
        Serial.println("--------------");
      } else {
        Serial.println("no response");
      }
     } else {
      Serial.println("send error");
     }
      sendCommand("AT+CIPCLOSE");
     } else {
      Serial.println("Error connecting");
     }
      }

     bool connectWiFi() {

       if (!sendCommand("ATE0")) // echo off
     return false;
       if (!sendCommand("AT+CIPMUX=0")) // set single connection mode
     return false;
       if (!sendCommand("AT+CWMODE=1")) // set STA mode
     return false;
       return sendCommand("AT+CWJAP=\"CAPSTONE\",\"capstonemis\"");
     }

     bool sendCommand(const char* cmd) {
       Serial.println(cmd);
     Serial1.println(cmd);
       while (true) {
     buff = Serial1.readStringUntil('\n');
       buff.trim();
     if (buff.length() > 0) {
      Serial.println(buff);
     if (buff == "OK" || buff == "SEND OK" || buff == "ALREADY CONNECTED")
       return true;
     if (buff == "ERROR" || buff == "FAIL" || buff == "SEND FAIL")
       return false;
     }
      }
}
  

PHP代码

<?php
      require 'firebaseLib.php';
      $Trash = $_GET["trash"];
      $Distance = $_GET["distance"];
      $Temperature = $_GET["temp_f"];
      $Humidity= $_GET["humidity"];

        $baseURI = 'https://thesis1-69.firebaseio.com';
        $token = 'gg';
    $devicestatus= array('Distance' => $Distance,'Temperature' => 
      $Temperature,'Humidity' => $Humidity);
    $firebasePath = '/thesis1-69/';
      $full= array($Trash => $devicestatus);
    $fb = new Firebase($baseURI, $token);
      $fb -> update($firebasePath, $full);
     ?>

我希望您能帮助我实现我想要完成的目标 非常感谢 更多的力量!

0 个答案:

没有答案