HTTP POST request-GSM Arduino - http

I've MKR GSM 1400 with sim and antenna, I want to send data to the KAA cloud by HTTP POST request.
By using cURL I've succeded to send data to the cloud using thise code
curl --location --request POST 'https://connect.cloud.kaaiot.com:443/kp1/<app-version-name>/dcx/<endpoint-token>/json' \
--data-raw '[
{
"temperature": 23,
"humidity": 48
}
]
Based on this I've written this code so far but I'm not sure what I'm missing for it to work :
#include <ArduinoHttpClient.h>
#include <MKRGSM.h>
#include <ArduinoJson.h>
const char PINNUMBER[] = "";
const char GPRS_APN[] = "";
const char GPRS_LOGIN[] = "";
const char GPRS_PASSWORD[] = "";
const String TOKEN = ""; // Deleted for security purpose
const String APP_VERSION = ""; // Deleted for security purpose
const unsigned long fiveSeconds = 1 * 5 * 1000UL;
static unsigned long lastPublish = 0 - fiveSeconds;
int temp;
GSMClient client;
GPRS gprs;
GSM gsmAccess;
char server[] = "connect.cloud.kaaiot.com";
char path[] = "/";
int port = 443;
HttpClient httpClient = HttpClient(client, server, port);
void setup() {
// initialize serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Starting GSM connection...");
// connection state
boolean connected = false;
// After starting the modem with GSM.begin()
// attach the shield to the GPRS network with the APN, login and password
while (!connected) {
if ((gsmAccess.begin(PINNUMBER) == GSM_READY) &&
(gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY)) {
connected = true;
} else {
Serial.println("Not connected");
delay(1000);
}
}
Serial.println("connecting...");
// if you get a connection, report back via serial:
if (client.connect(server, port)) {
Serial.println("connected");
} else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
}
void loop() {
if (client.connect(server, port)) {
unsigned long now = millis();
if (now - lastPublish >= fiveSeconds) {
lastPublish += fiveSeconds;
temp = random(18, 23);
Serial.print("temperature = ");
Serial.println(temp);
String queryString = String(" \"temperature\": ) + String(temp) ");
String topic = "/kp1/" + APP_VERSION + "/dcx/" + TOKEN + "/json";
httpClient.println("POST https://" + String(server) + ":" + String(port) + String(topic) + " HTTP/1.1");
Serial.println("POST https://" + String(server) + ":" + String(port) + String(topic) + " HTTP/1.1");
httpClient.println("Connection: close");
httpClient.println(); // end HTTP header
// send HTTP body
httpClient.print("{ \"temperature\": ");
httpClient.print(temp);
httpClient.print("}");
//httpClient.println(queryString);
}
} else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
delay(3000); // Wait for 3 seconds to post again
}
On Serial monitor it shows that I've connected to the server.

Related

ESP32 writeStringEEPROM adds unwanted nembers when I use the letter '&'

I am trying to use this code:
https://github.com/jatocode/WifiConnect/blob/master/WifiConnect/WifiConnect.ino with my ESP32.
The problem is that my wifi network name is "y&t" and when I am trying to use it the progrem saves to the EEPROM "y&26t" insted, with other networks names in my area it dosn't have a problem.
I will be greatful if somebady knows how to solve this problem.
Thanks to #Juraj I solved the problem.
I used in this code a decoder from here: https://circuits4you.com/2019/03/21/esp8266-url-encode-decode-example/
/*
Configuration AP example
The example shows a very simple Configuration Access Point.
created in August 2019
by Juraj Andrassy https://github.com/jandrassy
*/
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiAP.h>
#include <Arduino.h>
// #include <String.h>
String urldecode(String str);
unsigned char h2int(char c);
void setup() {
Serial.begin(115200);
delay(500);
// WiFi.disconnect(); // forget the persistent connection to test the Configuration AP
WiFi.persistent(true);
// waiting for connection to remembered Wifi network
Serial.println("Waiting for connection to WiFi");
WiFi.begin(); // use SSID and password stored by SDK
WiFi.waitForConnectResult();
if (WiFi.status() != WL_CONNECTED) {
Serial.println();
Serial.println("Could not connect to WiFi. Starting configuration AP...");
configAP();
} else {
Serial.println("WiFi connected");
}
}
void loop() {
}
void configAP() {
WiFiServer configWebServer(80);
WiFi.mode(WIFI_AP_STA); // starts the default AP (factory default or setup as persistent)
Serial.print("Connect your computer to the WiFi network ");
// Serial.print(WiFi.softAP());
Serial.println();
IPAddress ip = WiFi.softAPIP();
Serial.print("and enter http://");
Serial.print(ip);
Serial.println(" in a Web browser");
configWebServer.begin();
while (true) {
WiFiClient client = configWebServer.available();
if (client) {
char line[64];
int l = client.readBytesUntil('\n', line, sizeof(line));
line[l] = 0;
client.find((char*) "\r\n\r\n");
if (strncmp_P(line, PSTR("POST"), strlen("POST")) == 0) {
l = client.readBytes(line, sizeof(line));
line[l] = 0;
// parse the parameters sent by the html form
const char* delims = "=&";
strtok(line, delims);
const char* ssid = strtok(NULL, delims);
strtok(NULL, delims);
const char* pass = strtok(NULL, delims);
// decoding the ssid and the password for ASCII characters
String ssidS = String(ssid);
String ssidSdecode= urldecode(ssidS);
ssid=const_cast<char*>(ssidSdecode.c_str());
String passS = String(pass);
String passSdecode= urldecode(passS);
pass=const_cast<char*>(passSdecode.c_str());
// send a response before attemting to connect to the WiFi network
// because it will reset the SoftAP and disconnect the client station
client.println(F("HTTP/1.1 200 OK"));
client.println(F("Connection: close"));
client.println(F("Refresh: 10")); // send a request after 10 seconds
client.println();
client.println(F("<html><body><h3>Configuration AP</h3><br>connecting...</body></html>"));
client.stop();
Serial.println();
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
WiFi.waitForConnectResult();
// configuration continues with the next request
} else {
client.println(F("HTTP/1.1 200 OK"));
client.println(F("Connection: close"));
client.println();
client.println(F("<html><body><h3>Configuration AP</h3><br>"));
int status = WiFi.status();
if (status == WL_CONNECTED) {
client.println(F("Connection successful. Ending AP."));
} else {
client.println(F("<form action='/' method='POST'>WiFi connection failed. Enter valid parameters, please.<br><br>"));
client.println(F("SSID:<br><input type='text' name='i'><br>"));
client.println(F("Password:<br><input type='password' name='p'><br><br>"));
client.println(F("<input type='submit' value='Submit'></form>"));
}
client.println(F("</body></html>"));
client.stop();
if (status == WL_CONNECTED) {
delay(1000); // to let the SDK finish the communication
Serial.println("Connection successful. Ending AP.");
configWebServer.stop();
WiFi.mode(WIFI_STA);
}
}
}
}
}
String urldecode(String str) {
String encodedString="";
char c;
char code0;
char code1;
for (int i =0; i < str.length(); i++) {
c=str.charAt(i);
if (c == '+') {
encodedString+=' ';
} else if (c == '%') {
i++;
code0=str.charAt(i);
i++;
code1=str.charAt(i);
c = (h2int(code0) << 4) | h2int(code1);
encodedString+=c;
} else {
encodedString+=c;
}
yield();
}
return encodedString;
}
unsigned char h2int(char c) {
if (c >= '0' && c <='9') {
return((unsigned char)c - '0');
}
if (c >= 'a' && c <='f') {
return((unsigned char)c - 'a' + 10);
}
if (c >= 'A' && c <='F') {
return((unsigned char)c - 'A' + 10);
}
return(0);
}

How to post to a MS-teams webhook from Esp8266?

I have an ESP8266 and want to send messages to a MS-Teams channel. In the future I want to send temperature data but for now I am okay with whatever.
Right now I can connect to the host but it will not send any data to the team and it gives no error.
It works without any problems from Postman and the webhook also works from Python.
Here is my code:
#include "ESP8266HTTPClient.h"
#include "ESP8266WiFi.h"
#include <ArduinoJson.h>
#include <WiFiClientSecure.h>
void setup() {
Serial.begin(9600); //Serial connection
WiFi.begin("", ""); //WiFi connection
while (WiFi.status() != WL_CONNECTED) { //Wait for the WiFI connection completion
delay(500);
Serial.println("Waiting for connection");
}
}
void MicrosoftTeams() {
char host[] = "outlook.office.com"; //Specify request destination";
WiFiClient client;
if (client.connect(host, 443)) {
Serial.println("connected");
String url = "/webhook/Webhookcode";
char json[] = "{ \"#context\":
\"https://schema.org/extensions\",\"#type\":
\"MessageCard\",\"themeColor\": \"0072C6\",\"title\":
\"meddelande\",\"text\": \"Hello!!\"}";
int len = strlen(json);
String Output = "POST " + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Content-Type: application/json\r\n" +
"User-Agent: arduino/1.0\r\n" +
"Content-Length: " + len + "\r\n" +
"\r\n" +
json + "\n";
client.print(Output);
while (client.available()) {
String line = client.readStringUntil('\r');
Serial.print(line);
}
}
}
void loop() {
if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status
MicrosoftTeams();
} else {
Serial.println("Error in WiFi connection");
}
delay(30000); //Send a request every 30 seconds
}
Update:
Ive tested the following function and it works in this test example but not the real case. If i change the WiFiClient to WiFiClientSecure everything stops working.
void WifiCliwaySecure()
{
//Work
WiFiClient client;
const int httpPort = 80;
// //Doenst work
// WiFiClientSecure client;
// const int httpPort = 443;
char host[] = "ptsv2.com";
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
String url = "/t/erzs7-1555484154/post";
Serial.print("Requesting POST: ");
// Send request to the server:
String data = "{ \"title\": \"meddelande\",\"text\": \"Hallå alla bk!!\"}";
client.print(String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"User-Agent: BuildFailureDetectorESP8266\r\n" +
"Content-Length: " + data.length() + "\r\n" +
"Content-Type: application/json\r\n" +
"\r\n" +
data +
"\r\n");
Serial.print("url Send");
delay(500); // Can be changed
if (client.connected()) {
client.stop(); // DISCONNECT FROM THE SERVER
}
Serial.println();
Serial.println("closing connection");
}
By 2021, you should do the following:
#include <WiFi.h>
#include <HTTPClient.h>
void notifyTeams() {
// === Check WiFi connection status
if (WiFi.status()== WL_CONNECTED) {
HTTPClient http;
// == The incoming webhooks as given by Microsoft Teams
String url = "https://XXXXX.webhook.office.com/webhookb2/YYYYY/IncomingWebhook/ZZZZ/AAAA";
http.begin(url);
http.addHeader("Content-Type", "application/json");
// Send request to the server:
String data = "{ \"title\": \"Slug’s here!\", \"text\": \"Quickly go catch her!\"}";
int httpResponseCode = http.POST(data);
if(httpResponseCode>0) {
// === Get the response to the request
String response = http.getString();
Serial.println(httpResponseCode);
Serial.println(response);
}
else {
Serial.print("Error on sending POST: ");
Serial.println(httpResponseCode);
}
http.end();
}
else {
Serial.println("Error in WiFi connection");
}
}

Sending a HTTP request to Google Script in Arduino

I am using the following code which sends and receives a HTTP request to the custom website with no problem. However when I try to change the host to following I get a failure message. You can simply put this address into browser to see the actual response.
I need to change const char* host = "djxmmx.net"; to const char* host = "https://script.google.com/macros/s/AKfycby72HRgl874tst5e0FBHDa_VR6luqofn-ojiYF8KUBPmC2E3aiB/exec";
#include <ESP8266WiFi.h>
const char* ssid = "Phone";
const char* password = "aa";
const char* host = "djxmmx.net";
const uint16_t port = 17;
void setup() {
Serial.begin(115200);
delay(10);
// We start by connecting to a WiFi networkre
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
/* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default,
would try to act as both a client and an access-point and could cause
network-issues with your other WiFi-devices on your WiFi-network. */
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
Serial.print("connecting to ");
Serial.print(host);
Serial.print(':');
Serial.println(port);
// Use WiFiClient class to create TCP connections
WiFiClient client;
if (!client.connect(host, port)) {
Serial.println("connection failed");
delay(5000);
return;
}
// This will send a string to the server
Serial.println("sending data to server");
client.println("hello from ESP8266");
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
Serial.println(">>> Client Timeout !");
client.stop();
delay(60000);
return;
}
}
// Read all the lines of the reply from server and print them to Serial
Serial.println("receiving from remote server");
while (client.available()) {
char ch = static_cast<char>(client.read());
Serial.print(ch);
}
// Close the connection
//Serial.println();
//Serial.println("closing connection");
// client.stop();
//delay(300000); // execute once every 5 minutes, don't flood remote service
}
You can do it this way using some help as mentioned. This requires a data server setup in Google app in this link GoogleSheetScript. Note how "name=Amir" transfer your data.
/* HTTPS on ESP8266 with follow redirects, chunked encoding support
* Version 2.1
* Author: Sujay Phadke
* Github: #electronicsguy
* Copyright (C) 2017 Sujay Phadke <electronicsguy123#gmail.com>
* All rights reserved.
*
* Example Arduino program
*/
#include <ESP8266WiFi.h>
#include "HTTPSRedirect.h"
#include "DebugMacros.h"
// Fill ssid and password with your network credentials
const char* ssid = "AmirPhone";
const char* password = "aa112233";
const char* host = "script.google.com";
// Replace with your own script id to make server side changes
const char *GScriptId = "AKfycby72HRgl874tst5e0FBHDa_VR6luqofn-ojiYF8KUBPmC2E3aiB";
const int httpsPort = 443;
// echo | openssl s_client -connect script.google.com:443 |& openssl x509 -fingerprint -noout
const char* fingerprint = "";
// Write to Google Spreadsheet
String url = String("/macros/s/") + GScriptId + "/exec?name=Amir";
// Fetch Google Calendar events for 1 week ahead
String url2 = String("/macros/s/") + GScriptId + "/exec?cal";
// Read from Google Spreadsheet
String url3 = String("/macros/s/") + GScriptId + "/exec?read";
String payload_base = "";
String payload = "";
HTTPSRedirect* client = nullptr;
// used to store the values of free stack and heap
// before the HTTPSRedirect object is instantiated
// so that they can be written to Google sheets
// upon instantiation
unsigned int free_heap_before = 0;
unsigned int free_stack_before = 0;
void setup() {
Serial.begin(115200);
Serial.flush();
//free_heap_before = ESP.getFreeHeap();
//free_stack_before = cont_get_free_stack(&g_cont);
Serial.printf("Free heap before: %u\n", free_heap_before);
Serial.printf("unmodified stack = %4d\n", free_stack_before);
Serial.println();
Serial.print("Connecting to wifi: ");
Serial.println(ssid);
// flush() is needed to print the above (connecting...) message reliably,
// in case the wireless connection doesn't go through
Serial.flush();
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// Use HTTPSRedirect class to create a new TLS connection
client = new HTTPSRedirect(httpsPort);
client->setPrintResponseBody(true);
client->setContentTypeHeader("application/json");
Serial.print("Connecting to ");
Serial.println(host);
// Try to connect for a maximum of 5 times
bool flag = false;
for (int i=0; i<5; i++){
int retval = client->connect(host, httpsPort);
if (retval == 1) {
flag = true;
break;
}
else
Serial.println("Connection failed. Retrying...");
}
if (!flag){
Serial.print("Could not connect to server: ");
Serial.println(host);
Serial.println("Exiting...");
return;
}
if (client->verify(fingerprint, host)) {
Serial.println("Certificate match.");
} else {
Serial.println("Certificate mis-match");
}
// Send memory data to Google Sheets
payload = payload_base + "\"" + free_heap_before + "," + free_stack_before + "\"}";
client->POST(url2, host, payload, false);
payload = payload_base;// + "\"" + ESP.getFreeHeap() + "," + cont_get_free_stack(&g_cont) + "\"}";
client->POST(url2, host, payload, false);
// Note: setup() must finish within approx. 1s, or the the watchdog timer
// will reset the chip. Hence don't put too many requests in setup()
// ref: https://github.com/esp8266/Arduino/issues/34
Serial.println("\nGET: Write into cell 'A1'");
Serial.println("=========================");
// fetch spreadsheet data
client->GET(url, host);
// Send memory data to Google Sheets
payload = payload_base;// + "\"" + ESP.getFreeHeap() + "," + cont_get_free_stack(&g_cont) + "\"}";
client->POST(url2, host, payload, false);
Serial.println("\nGET: Fetch Google Calendar Data:");
Serial.println("================================");
// fetch spreadsheet data
client->GET(url2, host);
// Send memory data to Google Sheets
payload = payload_base;// + "\"" + ESP.getFreeHeap() + "," + cont_get_free_stack(&g_cont) + "\"}";
client->POST(url2, host, payload, false);
Serial.println("\nSeries of GET and POST requests");
Serial.println("===============================");
Serial.printf("Free heap: %u\n", ESP.getFreeHeap());
Serial.printf("unmodified stack = %4d\n");//, cont_get_free_stack(&g_cont));
// delete HTTPSRedirect object
delete client;
client = nullptr;
}
void loop() {
static int error_count = 0;
static int connect_count = 0;
const unsigned int MAX_CONNECT = 20;
static bool flag = false;
//Serial.printf("Free heap: %u\n", ESP.getFreeHeap());
//Serial.printf("unmodified stack = %4d\n", cont_get_free_stack(&g_cont));
if (!flag){
//free_heap_before = ESP.getFreeHeap();
// free_stack_before = cont_get_free_stack(&g_cont);
client = new HTTPSRedirect(httpsPort);
flag = true;
client->setPrintResponseBody(true);
client->setContentTypeHeader("application/json");
}
if (client != nullptr){
if (!client->connected()){
client->connect(host, httpsPort);
payload = payload_base + "\"" + free_heap_before + "," + free_stack_before + "\"}";
client->POST(url2, host, payload, false);
}
}
else{
DPRINTLN("Error creating client object!");
error_count = 5;
}
if (connect_count > MAX_CONNECT){
//error_count = 5;
connect_count = 0;
flag = false;
delete client;
return;
}
Serial.println("GET Data from cell 'A1':");
if (client->GET(url3, host)){
++connect_count;
}
else{
++error_count;
DPRINT("Error-count while connecting: ");
DPRINTLN(error_count);
}
Serial.println("POST append memory data to spreadsheet:");
payload = payload_base;// + "\"" + ESP.getFreeHeap() + "," + cont_get_free_stack(&g_cont) + "\"}";
if(client->POST(url2, host, payload)){
;
}
else{
++error_count;
DPRINT("Error-count while connecting: ");
DPRINTLN(error_count);
}
/*
if (!(client.reConnectFinalEndpoint())){
++error_count;
DPRINT("Error-count while connecting: ");
DPRINTLN(error_count);
}
else
error_count = 0;
*/
if (error_count > 3){
Serial.println("Halting processor...");
delete client;
client = nullptr;
Serial.printf("Final free heap: %u\n", ESP.getFreeHeap());
Serial.printf("Final unmodified stack = %4d\n");//, cont_get_free_stack(&g_cont));
Serial.flush();
ESP.deepSleep(0);
}
// In my testing on a ESP-01, a delay of less than 1500 resulted
// in a crash and reboot after about 50 loop runs.
delay(4000);
}
[1]: https://github.com/electronicsguy/ESP8266/blob/master/HTTPSRedirect/GoogleScript.gs

Send sensor data from Nodmcu to labview

I want to send sensor data(like DHT) from NODEMCU to LABVIEW.
Is there any library or sketch to do this ?
NODEMCU is connected to network in station mode.
In labview, I have to use which library to do this?
Solved :
1. Try this library in Arduino IDE: ESP8266WiFi.h
host is your PC like 192.168.1.11
port : 550 (for example)
#include <ESP8266WiFi.h>
const char* ssid = "xxxx"; // SSID
const char* password = "xxxx"; // Password
const char* host = "xxx.xxx.xxx.xxx"; // Server IP
const int port = 550; // Server Port
const int watchdog = 5000; // Watchdog frequency
unsigned long previousMillis = millis();
void setup() {
Serial.begin(115200);
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
unsigned long currentMillis = millis();
if ( currentMillis - previousMillis > watchdog ) {
previousMillis = currentMillis;
WiFiClient client;
if (!client.connect(host, port)) {
Serial.println("connection failed");
return;
}
String url = "/watchdog?command=watchdog&uptime=";
url += String(millis());
url += "&ip=";
url += WiFi.localIP().toString();
// Envoi la requete au serveur - This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
}
// Read all the lines of the reply from server and print them to Serial
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}
}
}
2. labview : you need 2 blocks to do this :
TCP LISTEN
TCP READ

How to use esp8266WiFi Library GET request to obtain info from a website

I'm working on the ESP8266 ESP-01 WiFi module programmed with Arduino IDE to send GET request to a URL and read the contents.
Trying to modify the below code which uses the ESP8266WiFi library, I'm not clear what to replace for host. Should i put my URL in host or something else?
Also how to edit the line.
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
#include <ESP8266WiFi.h>
const char* ssid = "your-ssid";
const char* password = "your-password";
const char* host = "data.sparkfun.com";
const char* streamId = "....................";
const char* privateKey = "....................";
void setup() {
Serial.begin(115200);
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
int value = 0;
void loop() {
delay(5000);
++value;
Serial.print("connecting to ");
Serial.println(host);
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
// We now create a URI for the request
String url = "/input/";
url += streamId;
url += "?private_key=";
url += privateKey;
url += "&value=";
url += value;
Serial.print("Requesting URL: ");
Serial.println(url);
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
}
// Read all the lines of the reply from server and print them to Serial
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("closing connection");
}
Your line client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n"); is OK. You don't need to change it.
For host you have to provide domain name or IP address. Eg. const char* host = "example.com";.
Example
If you want to get http://stackoverflow.com/questions/39707504/how-to-use-esp8266wifi-library-get-request-to-obtain-info-from-a-website you should have:
const char* host = "stackoverflow.com";
String url = "/questions/39707504/how-to-use-esp8266wifi-library-get-request-to-obtain-info-from-a-website";

Resources