ESP8266HTTPClient fails to connect to a webserver and always reponds with:
[HTTP] GET... failed, error: connection refused
This is not a WiFi problem, I get an IP from the DHCP server.
It is not clear to me what version ESP8266HTTPClient
I am using or where it is coming from! It comes from somewhere but is not included by me in the lib_deps
My config on a nodemcu
[env:nodemcuv2]
platform = espressif8266
board = nodemcuv2
framework = arduino
upload_port = /dev/ttyUSB0
lib_deps =
RCSwitch
This is not my first ESP project so I connected the MCU to a 5A stable 3.3V power supply. Should be enough power.
I tested it with the example code from the ESP8266HTTPClient here
And my code:
// ESP12 basis voor Tickles
// 2017-05-07
#include <Arduino.h>
// RadioHead --------
#include <RH_ASK.h> //radiohead
#include <SPI.h> //radiohead
#define BAUD 2000
#define RX D3
#define TX -1
RH_ASK driver(BAUD, RX, TX);
// Tickle
struct tickle {
uint16_t id;
uint16_t value1;
uint16_t value2;
} package;
//-------------------
// Web --------------
#include <ESP8266WiFi.h> //wifi
#include <ESP8266HTTPClient.h> //push to db
#include <ESP8266mDNS.h> //ota
#include <ArduinoOTA.h> //ota
#include <ESP8266HTTPClient.h>
const char* ssid = "something";
const char* password = "something";
void store() {
HTTPClient http;
Serial.print("[HTTP] begin...\n");
http.begin("http://pizerogrijs.local:5984/tickles/"); //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) {
String payload = http.getString();
Serial.println(payload);
}
} else {
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}
void setup()
{
Serial.begin(9600);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.hostname("433Huis");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
ArduinoOTA.onStart([]() {
Serial.println("Start");
});
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.begin();
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.println("Starting..");
if (!driver.init())
Serial.println("init failed");
}
void loop()
{
ArduinoOTA.handle();
uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
uint8_t buflen = sizeof(buf);
if (driver.recv(buf, &buflen)) {
memcpy(&package, buf, buflen);
Serial.print("ID: ");
Serial.println(package.id);
Serial.print("Value1: ");
Serial.println(package.value1);
Serial.print("Value2: ");
Serial.println(package.value2);
Serial.println("");
store();
}
yield;
}