RESTful sensor using ESP8266

Hi, this is my first post hope it’s not too much in one go but since I found this great System for the last week I’ve been trying to convert my old automation systems sensors to work with this system using the restful API I’m not a programmer, but I get on by . at the moment for last week I have been struggling with my code & configurations of yaml . I have read so much and tried quite a bit to no avail I’m not even sure if it’s possible so I need help.
I am programming my ESP8266 through the Arduino IDE my code

#include <ESP8266WiFi.h>
#include <Base64.h>
//AP definitions
#define AP_SSID "xxxx"
#define AP_PASSWORD "xxxx"
//  server definitions
#define USERNAME    "homeassistant"
//#define PASSWORD    "test"
#define IP_ADDRESS  "192.168.1.112"
#define PORT        8123
#define NODE        "sensor 7"

#define INPUT_PIN         3
#define USER_PWD_LEN      40

char unameenc[USER_PWD_LEN];
bool oldInputState;


void setup() {
  Serial.begin(115200);
  pinMode(INPUT_PIN, INPUT);
  
  wifiConnect();
    
  char uname[USER_PWD_LEN];
  String str = String(USERNAME);  
  str.toCharArray(uname, USER_PWD_LEN); 
  memset(unameenc,0,sizeof(unameenc));
  base64_encode(unameenc, uname, strlen(uname));
  
  oldInputState = !digitalRead(INPUT_PIN);
}

void loop() {
  int inputState = digitalRead(INPUT_PIN); 
  
  if (inputState != oldInputState)
  {
    sendInputState(inputState);
    oldInputState = inputState;
  }
}

void wifiConnect()
{
    Serial.print("Connecting to AP");
    WiFi.begin(AP_SSID, AP_PASSWORD);
    while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }
  
  Serial.println("");
  Serial.println("WiFi connected");  
}

void sendInputState(bool inputState)
{  
   WiFiClient client;
   
   while(!client.connect(IP_ADDRESS, PORT)) {
    Serial.println("connection failed");
    wifiConnect(); 
  }
 
  String url = "";
  String command = "";
  
  if (inputState)
    command = "ControlOn";
  else
    command = "ControlOff";
  
  url = "/api/services/switch/"+String(command); // generate server node URL


  Serial.print("POST data to URL: ");
  Serial.println(url);
  
  client.print(String("POST ") + url + " HTTP/1.1\r\n" +
               "Host: " + String(IP_ADDRESS) + "\r\n" + 
               "Connection: close\r\n" + 
               "Authorization: Basic " + unameenc + " \r\n" + 
               "Content-Length: 0\r\n" + 
               "\r\n");

  delay(100);
    while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }
  
  Serial.println();
  Serial.println("Connection closed");
}

I’m trying to connect to home assistance and send my data to the systems to be displayed on the front end I’ve tried several different configurations but have got nowhere with this ,not sure whether my code is correct for this action as well as finding the right configurations for the .Yaml file, so if you can help it will be much appreciated, then I can get on and enjoy this great system some more.

Check the HTTP sensor docs to see the parts of a request for a sensor.The RESTful API docs contains further information about the available options.

I’m new, too, but might be able to at least push you in the right direction. Home Assistant’s API requires that you pass JSON to an endpoint. It looks like you’re sending “ControlOn/ControlOff”. For example, if you’re calling light/toggle, you can pass it: {"entity_id": "light.ge_45602_lamp_dimmer_module_level_2" }

To experiment, you can go to HA’s “services” dev tool (/dev-service - the button that looks like a remote control, bottom of left column). There you can pass JSON data. If you’re handy with your browser’s console, you can open that and see what HA is sending to the /api

What are you seeing when that Serial.print happens?

Also, Looks like you may have commented out your HA pass. Note that if you don’t want to pass the authentication header, you can append this to your API URL: ?api_password=YOUR_PASSWORD