HTTP Client Example for ESP8266 to Send Information to a Remote Location
This code, which has been explained in the video above, will send three parameters (below) using the HTTP Client to remove a location over WiFi.236-Topics shown in the video
语言: C++
/*
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 libraries, codes, wiring diagrams 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.
or make a 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 downloaded 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...\n", t);
Serial.flush();
delay(1000);
}
WiFi.mode(WIFI_STA);
WiFiMulti.addAP("Robojax", "4YouTubegnAQw~");
}
void loop() {
// wait for WiFi connection
if ((WiFiMulti.run() == WL_CONNECTED)) {
WiFiClient client;
HTTPClient http;
Serial.print("[HTTP] begin...\n");
if (http.begin(client, mainURL+user+"&temp="+temperature+"&type="+type)) { // HTTP
Serial.print("[HTTP] GET...\n");
// 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\n", 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\n", http.errorToString(httpCode).c_str());
}
http.end();
} else {
Serial.printf("[HTTP] Unable to connect\n");
}
}
delay(10000);
}
资源与参考
-
外部ESP8266 GitHub 存储库github.com
-
外部ESP8266 数据表 (PDF)espressif.com
-
外部NodeMCU文档网页nodemcu.readthedocs.io
-
外部用于发送参数的 DDAS URLddas.cc
-
内部下载视频中显示的本文的PHP文件。robojax.com
文件📁
没有可用的文件。