0

I can successfully send a GET request with the ESP8266HTTPClient library in the first sketch. However the exactly same code won't work when used in the second sketch, which is more complex and uses other libraries too. I'm not sure why, maybe a conflict between libraries?

Sketch 1 (working)

#define DEBUG

#ifdef DEBUG
  #define SB(...) Serial.begin(__VA_ARGS__)
  #define SP(...) Serial.print(__VA_ARGS__)
  #define SPL(...) Serial.println(__VA_ARGS__)
#else
  #define SB(...) 
  #define SP(...)
  #define SPL(...)
#endif

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
const char* ssid = "TALKTALK-17409C";
const char* password = "HPHRKJYX";

void setup() {

   SB(115200);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {  
    delay(500);
    SPL("Waiting to connect…");
  }

  if (WiFi.status() == WL_CONNECTED) {
    HTTPClient http;
    http.begin("http://francescosoave.com/blind/getTime.php?pw=ciao");
    int httpCode = http.GET();
    SP("HTTP: ");
    SPL(httpCode);
    if(httpCode > 0){
      String payload = http.getString();
      SP("PAYLOAD: ");
      SPL(payload);
    }

    http.end();
  }
}

void loop() {}

Sketch 1 response in the serial monitor (Tools -> Debug port -> Serial // Tools -> Debug Level -> HTTP_CLIENT)

SDK:2.2.1(cfd48f3)/Core:2.4.1/lwIP:2.0.3(STABLE-2_0_3_RELEASE/glue:arduino-2.4.1)
scandone
scandone
state: 0 -> 2 (b0)
state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 3
cnt 
Waiting to connect…

connected with TALKTALK-17409C, channel 10
dhcp client start...
Waiting to connect…
ip:192.168.1.4,mask:255.255.255.0,gw:192.168.1.1
Waiting to connect…
[HTTP-Client][begin] url: http://francescosoave.com/blind/getTime.php?pw=ciao
[HTTP-Client][begin] host: francescosoave.com port: 80 url: /blind/getTime.php?pw=ciao
[HTTP-Client] connected to francescosoave.com:80
[HTTP-Client] sending request header
-----
GET /blind/getTime.php?pw=ciao HTTP/1.1
Host: francescosoave.com
User-Agent: ESP8266HTTPClient
Connection: close
Accept-Encoding: identity;q=1,chunked;q=0.1,*;q=0

-----
[HTTP-Client][handleHeaderResponse] RX: 'HTTP/1.1 200 OK'
[HTTP-Client][handleHeaderResponse] RX: 'Server: nginx'
[HTTP-Client][handleHeaderResponse] RX: 'Date: Wed, 08 Aug 2018 21:24:46 GMT'
[HTTP-Client][handleHeaderResponse] RX: 'Content-Type: text/html; charset=UTF-8'
[HTTP-Client][handleHeaderResponse] RX: 'Transfer-Encoding: chunked'
[HTTP-Client][handleHeaderResponse] RX: 'Connection: close'
[HTTP-Client][handleHeaderResponse] RX: 'Vary: Accept-Encoding'
[HTTP-Client][handleHeaderResponse] RX: 'X-Powered-By: PHP/5.6.32'
[HTTP-Client][handleHeaderResponse] RX: 'Vary: Accept-Encoding'
[HTTP-Client][handleHeaderResponse] RX: 'Access-Control-Allow-Origin: *'
[HTTP-Client][handleHeaderResponse] RX: ''
[HTTP-Client][handleHeaderResponse] code: 200
[HTTP-Client][handleHeaderResponse] Transfer-Encoding: chunked
HTTP: 200
[HTTP-Client] read chunk len: 11
[HTTP-Client][writeToStreamDataBlock] connection closed or file end (written: 11).
[HTTP-Client] read chunk len: 0
[HTTP-Client][end] still data in buffer (2), clean up.
[HTTP-Client][end] tcp stop
PAYLOAD: TIME: 22:24
[HTTP-Client][end] tcp is closed
pm open,type:2 0

Sketch 2 (not working)

#define DEBUG

#ifdef DEBUG
  #define SB(...) Serial.begin(__VA_ARGS__)
  #define SP(...) Serial.print(__VA_ARGS__)
  #define SPL(...) Serial.println(__VA_ARGS__)
#else
  #define SB(...) 
  #define SP(...)
  #define SPL(...)
#endif

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>

const char* ssid = "TALKTALK-17409C";
const char* password = "HPHRKJYX";

ESP8266WebServer server(80);   //instantiate server at port 80 (http port)
IPAddress ip(192, 168, 1, 7);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);

String pageFinal = "";
byte curPos;
boolean timerState = true; //true = on, false = off
//int[] curTime = {0, 0}; // {hour, minutes

// HTML PAGES
String page1="<!DOCTYPE html>"
"<html>"
"<head>"
"<meta charset='utf-8'/>"
"<meta name='viewport' content='width=device-width, initial-scale=1'>"
"<meta http-equiv='Content-Type' content='text/html;charset=UTF-8' />"
"<link rel='stylesheet' href='https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css' integrity='sha384-Smlep5jCw/wG7hdkwQ/Z5nLIefveQRIY9nfy6xoR1uRYBtpZgI6339F5dgvm/e9B' crossorigin='anonymous'>"
"<link rel='stylesheet' type='text/css' href='http://www.francescosoave.com/blind/style.css'>"
"<script src='https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js' type='text/javascript'></script>"
"<script src='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js'></script>"
"</head>"
"<body>"
"<div class='container'>"
"<div class='row'>"
"<table class='table' id='radioForm'>"
"<thead>"
"<tr><th>Fran's Blind</th></tr>"
"</thead>"
"<tbody>"
"<tr><td align='center'>"
"<form action='/' method='get' id='manualForm'>"
"<input id='1' type='submit' class='btn btn-primary' name='manualBtn' value='1' />"
"</td></tr>"
"<tr><td align='center'>"
"<input id='2' type='submit' class='btn btn-primary' name='manualBtn' value='2' />"
"</td></tr>"
"<tr><td align='center'>"
"<input id='3' type='submit' class='btn btn-primary' name='manualBtn' value='3' />"
"</td></tr>"
"<tr><td align='center'>"
"<input id='4' type='submit' class='btn btn-primary' name='manualBtn' value='4' />"
"</td></tr>"
"<tr><td align='center'>"
"<input id='5' type='submit' class='btn btn-primary' name='manualBtn' value='5' />"
"</td></tr>"
"<tr><td align='center'>"
"<input id='6' type='submit' class='btn btn-primary' name='manualBtn' value='6' />"
"</td></tr>"
"<tr><td align='center'>"
"<input id='7' type='submit' class='btn btn-primary' name='manualBtn' value='7' />"
"</form>"
"</td></tr>"
"<tr><td align='center'><form action='/' method='get'>"
"<input id='timer' name='timerBtn' value='' class='' type='submit'></form><p>Timer is <span id='timerMsg'></span></p>"
"</td></tr>"
"</tbody>"
"</table>"
"</div>"
"</div>"
"</body>"
"<script>"
"$( document ).ready(function() {"
"setInterval(function(){"
"$('#timerMsg').fadeOut(function () {"
"$(this).fadeIn();"
"});"
"} ,1000);";

// page2 / page2_5 change timer button
// page 3/ page3_5 change current position button
String page2 = "$('#timer').attr({'class': 'btn btn-danger', value: 'TURN OFF'});$('#timerMsg').html('on');});";
String page2_5 = "$('#timer').attr({'class': 'btn btn-success', value: 'TURN ON'});$('#timerMsg').html('off');});";
String page3 = "$('#";
String page3_5 = "').attr({'class': 'btn btn-warning'});";
String page4 = "</script></html>";

void setup(void){

  WiFi.config(ip, gateway, subnet); 
  delay(1000);
  SB(115200);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {  
    delay(500);
    SPL("Waiting to connect…");
  }

  SP("IP address: ");
  SPL(WiFi.localIP());

  //here's the same GET request as in Sketch 1 but here it doesn't work
if (WiFi.status() == WL_CONNECTED) {
    HTTPClient http;
    http.begin("http://francescosoave.com/blind/getTime.php?pw=ciao");
    int httpCode = http.GET();
   SP("HTTP: ");
   SPL(httpCode);
    if(httpCode > 0){
      String payload = http.getString();
      SP("PAYLOAD: ");
      SPL(payload);
    }

    http.end();
  }

  server.on("/", control);  
  server.begin();
  SPL("Server listening");
}

int  i = 0;

void loop(void){
  server.handleClient();
}

void control(){

  if(server.args() > 0){ //there is something to change
    if(server.argName(0) == "timerBtn"){
      timerState = !timerState; //change timer
    }else if(server.argName(0) == "manualBtn"){
      curPos = server.arg(0).toInt(); //change current (manual)
    }
  }

  //build the page
  if(timerState)
    pageFinal = page1 + page2;
  else
    pageFinal = page1 + page2_5;

  pageFinal += page3 + curPos + page3_5 + page4;        
  server.send(200, "text/html", pageFinal);
}

Sketch 2 response in the serial monitor (Tools -> Debug port -> Serial // Tools -> Debug Level -> HTTP_CLIENT)

SDK:2.2.1(cfd48f3)/Core:2.4.1/lwIP:2.0.3(STABLE-2_0_3_RELEASE/glue:arduino-2.4.1)
scandone
Waiting to connect…
IP address: 192.168.1.7
[HTTP-Client][begin] url: http://francescosoave.com/blind/getTime.php?pw=ciao
[HTTP-Client][begin] host: francescosoave.com port: 80 url: /blind/getTime.php?pw=ciao
[HTTP-Client] failed connect to francescosoave.com:80
[HTTP-Client][returnError] error(-1): connection refused
HTTP: -1
[HTTP-Client][end] tcp is closed
Server listening
pm open,type:2 0

edit: the issue was in the Wifi.config() line. It should be WiFi.config(ip, gateway, subnet, gateway);

3
  • 1
    Chances are it's that insane string you're constructing that is causing your problems.
    – Majenko
    Aug 8 '18 at 22:01
  • that part works well..you can try it since it's the full code..
    – SF1
    Aug 8 '18 at 22:04
  • Humour me and replace that string with something tiny. See what the effect is. I am not at my desk to try it myself.
    – Majenko
    Aug 8 '18 at 22:07
1

If you want to configure a static IP address, make sure it is unique in you LAN and outside the range managed by the DHCP server.

EDIT: the WiFi.config() function doesn't assign the gateway address as DNS address. add 4th parameter with DNS server address.

WiFi.config(ip, gateway, subnet, gateway);
13
  • Hi, it is indeed unique in my LAN since it's working. Not sure how to check "outside the range managed by the DHCP server"?
    – SF1
    Aug 9 '18 at 14:38
  • ok so the static IP seems to be the issue. If I remove that the HTTP request will work. However I need a static IP. Any idea?
    – SF1
    Aug 9 '18 at 14:46
  • it is not working, if you can't connect. is the AP in your control? do you know the DHCP range of IPs? do you know what other devices with static IP are in your network? for start try to use the IP that was assigned by the DHCP 192.168.1.4
    – Juraj
    Aug 9 '18 at 14:53
  • sorry maybe I wasn't clear. So: right now with DHCP (removing the WiFi.config() line) everything works fine: the HTTP request is sent and the html page is displayed at 192.168.1.8. If I set the static IP instead, the html page won't work and the HTTP request will return the error. BUT, for my application I need a static IP. So I need to find a solution so that both works with a static IP.
    – SF1
    Aug 9 '18 at 15:02
  • 1
    the 4th parameter is the DNS server. your url has hostname that needs to be resolved by DNS
    – Juraj
    Aug 9 '18 at 15:44
0

Comment out the WiFi.config() line in the second example. You’re probably not configuring your ESP correctly. Let it get it’s configuration from its DHCP server.

2
  • Hi, unfortunately I need a static IP address. However that part is working fine. It looks like there's a conflict somewhere.
    – SF1
    Aug 9 '18 at 14:39
  • Hi, unfortunately I need a static IP address. And that seems to be the issue. If I remove the line, the HTTP request is working. But I need the static IP. any solution? Thanks!
    – SF1
    Aug 9 '18 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.