• I am trying to hook up an ESP01 to my smoke alarm like in this video
    and making it report to home-assistant, running on my pi. Home-assistant has an API which should make it possible for the ESP01 to alter a variable (input_boolean) and trigger other alarms.

    However, the http-request only seems to be able to reach the pi on port 80. Which is a problem, since home-assistant listens to port 8123 (in my case, pihole listens to port 80).

      var wifi = require("Wifi");
    
      wifi.connect("SSID", {password:"PASS"}, function(e) { 
        if( ! e )
        {
          wifi.setHostname("SmokeAlarm-"+getSerial() );
          wifi.stopAP();
          
          var options = {
            host: 'xd-1',
            port: 80,
            path: '/api/',
            method: 'GET'
          };
          
          require("http").request( options, function (res) {
            res.on('data', function (data) { console.log(data); });
          }).end();
        }
      });
    

    The path "/api/" does not exist on port 80 (pihole only uses /admin/*) and the ESP01 outputs the right error page, served by pihole. The script functions as it should.

    The path "/api/" DOES exist on port 8123 - I can see it in any browser on any device in my network. When changing the port from 80 to 8123, the ESP01 shows nothing, except "=undefined".

    I'm starting to lose it. Why is the request not coming through? Any thoughts?

  • Turns out the problem was not with the ESP01 or with Espruino, but with my malformed request :) The debugging methods from this thread made that clear (it showed a HTTP-400 header coming back).

    For whoever finds it useful, this is how Espruino connects to Home Assistant:

      
      var wifi   = require("Wifi");
      var entity = 'smoke_kitchen';
    
      wifi.connect("SSID", {password:"PASS"}, function (e) { 
        if( ! e )
        {
          wifi.setHostname("SmokeAlarm-"+entity+"_"+getSerial() );
          
          var http = require("http");
          var data = JSON.stringify( {state:'on'} );
          
          var options = {
            host: 'HASS-HOST',
            port: 8123,
            path: '/api/states/input_boolean.' + entity,
            method: 'POST',
            headers: {
              'Content-Type': 'application/json',
              'Content-Length': data.length,
              'x-ha-access': 'Token if needed',
            },
          };
          
          var req = http.request( options, function (res) {
            console.log('Status: ' + res.statusCode);
            console.log( res );
          });
          
          req.write( data );
          req.end();
          
        } else
        { 
          console.log("Error connecting to wifi: ", e );
        }
      });
    
  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

[SOLVED] ESP01 http request only working for port 80

Posted by Avatar for Thijsmans @Thijsmans

Actions