0

In the project I am working on

I want to send data to firebase going to arduino this specifically by sending an information to change the wifi

SSID

password

materials I used:

esp8266-01

arduino uno r3

firebase

web server (000webhost)

code for 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 3DHT 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);  //Serial1.resetESP();  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)      ; // do nothing  }  Serial.println("OK, Connected to WiFi.");  sendCommand("AT+CIPSTA?");  //sendCommand("AT+CIPDNS_CUR?");  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;  // connect to server  if (sendCommand("AT+CIPSTART=\"TCP\",\"gg.com\",80")) {    Serial.println("connected to Cloud");    // build HTTP request    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";    // open send buffer    buff = "AT+CIPSEND=";    buff += getStr.length();    if (sendCommand(buff.c_str()) && Serial1.find(">")) { // AT firmware is ready to accept data      // send HTTP request      Serial.println(getStr);      Serial1.print(getStr);      // print HTTP response      if (Serial1.find("+IPD,")) { // response received        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;    }  }}

code for firebase

<?phprequire 'firebaseLib.php';$Trash = $_GET["trash"];$Distance = $_GET["distance"];$Temperature = $_GET["temp_f"];$Humidity= $_GET["humidity"];// --- This is your Firebase URL$baseURI = 'https://thesis1-69.firebaseio.com';// --- Use your token from Firebase here$token = 'gg';// --- Here is your parameter from the http GET$devicestatus= array('Distance' => $Distance,'Temperature' => $Temperature,'Humidity' => $Humidity);$firebasePath = '/thesis1-69/';$full= array($Trash => $devicestatus);/// --- Making calls$fb = new Firebase($baseURI, $token);$fb -> update($firebasePath, $full);?>

any guide and help can help me a lotthank you so much :)

askedMay 29, 2019 at 12:13
None's user avatar
28
  • this is code from my chat with Xiaomychat.stackexchange.com/rooms/85432/…CommentedMay 29, 2019 at 14:04
  • yeah I am xiaomyCommentedMay 29, 2019 at 14:06
  • @Juraj can we create a chat?CommentedMay 29, 2019 at 14:12
  • why as new user?CommentedMay 29, 2019 at 14:42
  • I can't reply on the conversation and can't post new question in the recent accountCommentedMay 29, 2019 at 15:13

1 Answer1

0

add global variableString data; and in loop()

data = "trash=";data += Trash;data += "&distance=";data += distance;data += "&temp_f=";data += temp_f;data += "&humidity=";data += humidity;// build HTTP requestgetStr = "POST /upload.php HTTP/1.1\r\n";getStr += "Content-type: application/x-www-form-urlencoded\r\n";getStr += "Content-length: ";getStr += data.length();getStr += "\r\nHost: gg.com\r\n\r\n";getStr += data;
answeredMay 29, 2019 at 19:47
Juraj's user avatar
6
  • Thank you sir so the data, it represents SSID and password?CommentedMay 30, 2019 at 14:16
  • I wanna keep both the GET and POSTCommentedMay 30, 2019 at 14:33
  • for example I will set variable namely data1 and data2 then on the return sendCommand("AT+CWJAP=\"CAPSTONE\",\"capstonemis\""); } will be replace return sendCommand("AT+CWJAP=\"data1\",\"data2\""); } is this right sir?CommentedMay 30, 2019 at 15:12
  • I don't need to add some PHP code on the website server?CommentedMay 30, 2019 at 15:26
  • so you want to "get" data. why do you want to do it with a "post" request?CommentedMay 30, 2019 at 16:42

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.