Robojax

HTTP Client Example for ESP8266 to send information to remote location

دروس آردوینو به فارسی

HTTP Client Example for ESP8266 to send information to remote location

This code which has been explained in the video above will send three parameters (below) using HTTP Client to remove location over the WiFi.

  1. User name
  2. Temperature
  3. Type
The link to use in the "preferences" of Arduino IDE https://arduino.esp8266.com/stable/package_esp8266com_index.json
The link which ESP8266 sends data too http://ddas.cc/httpTest/?user=Robojax&temp=18.34&type=

Topics shown in video

Downlaod PHP file for this article shown in video

Resources for this sketch


  /* 
 BasicHTTPClient.ino

 *  
 *  HTTP Client example modified to send 3 parameters using ESP8266 as HTTP Client
 *  
 * Modified by Ahmad Shamshiri for Robojax Robojax.com
 * on August 30, 2019 in Ajax, Ontario, Canada
 Watch the video instruction for this sketch: https://youtu.be/pLvqh57T3s4
 

 

Learn Arduino step by step with all library, codes, wiring diagram all in one place
visit my course now http://robojax.com/L/?id=62

If you found this tutorial helpful, please support me so I can continue creating 
content like this. You can support me on Patreon http://robojax.com/L/?id=63
or make donation using PayPal http://robojax.com/L/?id=64

Original code by BARRAGAN <http://barraganstudio.com>
* 
 * Code is available at http://robojax.com/learn/arduino

 * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.* 
 * This code has been download from Robojax.com
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>. 

*/

#include <Arduino.h>

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>

#include <ESP8266HTTPClient.h>

#include <WiFiClient.h>

ESP8266WiFiMulti WiFiMulti;
String mainURL ="http://ddas.cc/httpTest/?";
String user="user=ahmad22";
float temperature=45.3;
char type='j';

void setup() {

  Serial.begin(115200);
  // Serial.setDebugOutput(true);

  Serial.println();
  Serial.println();
  Serial.println();

  for (uint8_t t = 4; t > 0; t--) {
    Serial.printf("[SETUP] WAIT %d...
", t);
    Serial.flush();
    delay(1000);
  }

  WiFi.mode(WIFI_STA);
  WiFiMulti.addAP("Robojax", "4YouTubegnA&#24Qw~");

}

void loop() {
  // wait for WiFi connection
  if ((WiFiMulti.run() == WL_CONNECTED)) {

    WiFiClient client;

    HTTPClient http;

    Serial.print("[HTTP] begin...
");
    if (http.begin(client, mainURL+user+"&temp="+temperature+"&type="+type)) {  // HTTP


      Serial.print("[HTTP] GET...
");
      // start connection and send HTTP header
      int httpCode = http.GET();

      // httpCode will be negative on error
      if (httpCode > 0) {
        // HTTP header has been send and Server response header has been handled
        Serial.printf("[HTTP] GET... code: %d
", httpCode);

        // file found at server
        if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
          String payload = http.getString();
          Serial.println(payload);
        }
      } else {
        Serial.printf("[HTTP] GET... failed, error: %s
", http.errorToString(httpCode).c_str());
      }

      http.end();
    } else {
      Serial.printf("[HTTP} Unable to connect
");
    }
  }

  delay(10000);
}