0

I have written this code working on SimpleHost example from ESP8266Ping library,

the code works,

#include <ESP8266WiFi.h>
#include <ESP8266Ping.h>
#include <ESP8266WebServer.h>
#include <ArduinoJson.h>

#define HTTP_REST_PORT 80

const char* ssid     = "usernaem";
const char* password = "password";

const char* remote_host = "10.10.10.12";


ESP8266WebServer http_rest_server(HTTP_REST_PORT);


void get_online_status() {
  StaticJsonBuffer<200> jsonBuffer;
  JsonObject& jsonObj = jsonBuffer.createObject();
  char JSONmessageBuffer[200];

  Serial.print("Pinging host ");
  Serial.println(remote_host);

  if(Ping.ping(remote_host)) {
    Serial.println("Success!!");
    jsonObj["roku_express"] = 1; 
  } else {
    Serial.println("Error :-(");
    jsonObj["roku_express"] = 0;
  }
  jsonObj.prettyPrintTo(JSONmessageBuffer, sizeof(JSONmessageBuffer));
  http_rest_server.send(200, "application/json", JSONmessageBuffer);
}


void config_rest_server_routing() {
    http_rest_server.on("/", HTTP_GET, []() {
        http_rest_server.send(200, "text/html",
            "Welcome to the ESP8266 REST Web Server");
    });
    http_rest_server.on("/ping", HTTP_GET, get_online_status);
}

void setup() {
  Serial.begin(115200);
  delay(10);

  // We start by connecting to a WiFi network

  Serial.println();
  Serial.println("Connecting to WiFi");

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(100);
    Serial.print(".");
  }

  Serial.println();
  Serial.print("WiFi connected with ip ");  
  Serial.println(WiFi.localIP());
  config_rest_server_routing();
  http_rest_server.begin();
  get_online_status();
}

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

if I try to access my wemos D1 Mini from web browser I get correct message : if offline

{
  "roku_express": 0
}

if online

{
  "roku_express": 1
}

but if I try to get the REST URL from python code either using requests or urllub3

import urllib3 
http = urllib3.PoolManager()
url = "http://10.10.10.4/ping" 
response = http.request('GET', url) 

I get below error

MaxRetryError: HTTPConnectionPool(host='10.10.10.4', port=80): Max retries exceeded with url: /ping (Caused by NewConnectionError(': Failed to establish a new connection: [Errno -536870212] Unknown error: -536870212'))

  • 1
    Is the computer running the Python code on the same network as the ESP8266? What happens if you try to access the ESP8266 URL from a web browser? – romkey Dec 29 '19 at 22:41
  • like I said in the opening post, from web browser it worked. – Ciasto piekarz Dec 29 '19 at 23:09

Your Answer

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

Browse other questions tagged or ask your own question.