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.