Giter Site home page Giter Site logo

Comments (14)

Yurik72 avatar Yurik72 commented on August 23, 2024

1.Use latest version or version tagged v1.0.7
2. Check what is ch in your code. It should be valid pointer to (homekit_characteristic_t *)

Any other question, please attach full sketch

from esphap.

chrwh avatar chrwh commented on August 23, 2024

Hi,

I'm using v.1.0.7

I now also see and unprotected wifi ESP-E4061A, when finished pairing a setting up a new device, that give access to 192.168.4.1 ?? and evrybody can lok into that?

Here is the hole sketch look for //test

/// BASIC CONFIGURATION

#define ENABLE_WIFI_MANAGER // if we want to have built-in wifi configuration
// Otherwise direct connect ssid and pwd will be used
// for Wifi manager need extra library //https://github.com/tzapu/WiFiManager

#define ENABLE_WEB_SERVER //if we want to have built in web server /site
#define ENABLE_OTA //if Over the air update need , ENABLE_WEB_SERVER must be defined first
#include <Arduino.h>

#ifdef ESP32
#include <SPIFFS.h>
#endif

#ifdef ESP8266
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include "coredecls.h"
#endif

#ifdef ENABLE_WEB_SERVER
#ifdef ESP8266
#include <ESP8266WebServer.h>
ESP8266WebServer server(80);
#endif

#ifdef ESP32
#include <WebServer.h>
WebServer server(80);
#endif
#endif

#if defined(ESP32) && defined(ENABLE_OTA)
#include <Update.h>
#endif

#ifdef ENABLE_WEB_SERVER
#include "spiffs_webserver.h"
bool isWebserver_started=false;
#endif

const int relay_gpio=2;
const int relay1_gpio=0;

#ifdef ENABLE_WIFI_MANAGER
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
#endif

const char* HOSTNAME="my_thing";
const char* ssid = "ssid";
const char* password = "pass";

extern "C"{
#include "homeintegration.h"
}
#ifdef ESP8266
#include "homekitintegrationcpp.h"
#endif

homekit_service_t* hapservice={0};
homekit_service_t* hapservice_CD={0};

String pair_file_name="/pair.dat";

void switch_callback(homekit_characteristic_t *ch, homekit_value_t value, void *context);
void switch1_callback(homekit_characteristic_t *ch, homekit_value_t value, void *context);

//Web server section
#define ENABLE_OTA //if OTA need

#include "spiffs_webserver.h"

bool getSwitchVal(){
if(hapservice){
homekit_characteristic_t * ch= homekit_service_characteristic_by_type(hapservice, HOMEKIT_CHARACTERISTIC_ON);
if(ch){
return ch->value.bool_value;
}
}

if(hapservice_CD){
  homekit_characteristic_t * ch= homekit_service_characteristic_by_type(hapservice_CD, HOMEKIT_CHARACTERISTIC_ON);
  if(ch){
    return ch->value.bool_value;
  }
}
return false;

}

void setup() {
#ifdef ESP8266
disable_extra4k_at_link_time();
#endif
Serial.begin(115200);
delay(10);

#ifdef ESP32
if (!SPIFFS.begin(true)) {
// Serial.print("SPIFFS Mount failed");
}
#endif
#ifdef ESP8266
if (!SPIFFS.begin()) {
Serial.print("SPIFFS Mount failed");
}
#endif

Serial.print("Free heap: ");
Serial.println(system_get_free_heap_size());


pinMode(relay_gpio, OUTPUT);

pinMode(relay1_gpio, OUTPUT);

init_hap_storage();

set_callback_storage_change(storage_changed);

/// We will use for this example only one accessory (possible to use a several on the same esp)
//Our accessory type is light bulb , apple interface will proper show that
hap_setbase_accessorytype(homekit_accessory_category_switch);
/// init base properties
hap_initbase_accessory_service("doswitch","Fun","0","dSwitch","1.54");

//we will add only one light bulb service and keep pointer for nest using
hapservice= hap_add_switch_service("AUX",switch_callback,(void*)&relay_gpio);
hapservice_CD= hap_add_switch_service("CD",switch1_callback,(void*)&relay1_gpio);

//test

//homekit_characteristic_t * ch = homekit_service_characteristic_by_type(hapservice, HOMEKIT_CHARACTERISTIC_ON);
//INIT_CHARACHTERISTIC_VAL(bool,ch,false); // will inform apple that lights is OFF

// homekit_characteristic_t * ch1= homekit_service_characteristic_by_type(hapservice_CD, HOMEKIT_CHARACTERISTIC_ON);
//INIT_CHARACHTERISTIC_VAL(bool,ch1,false); // will inform apple that lights is OFF
//end test

#ifdef ENABLE_WIFI_MANAGER
startwifimanager();
#else

WiFi.mode(WIFI_STA);
WiFi.begin((char*)ssid, (char*)password);
 while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
}

#endif
Serial.println(PSTR("WiFi connected"));
Serial.println(PSTR("IP address: "));
Serial.println(WiFi.localIP());

hap_init_homekit_server();   

#ifdef ENABLE_WEB_SERVER
String strIp=String(WiFi.localIP()[0]) + String(".") + String(WiFi.localIP()[1]) + String(".") + String(WiFi.localIP()[2]) + String(".") + String(WiFi.localIP()[3]);
#ifdef ESP8266
if(hap_homekit_is_paired()){
#endif
Serial.println(PSTR("Setting web server"));
SETUP_FILEHANDLES
server.on("/get", handleGetVal);
server.on("/set", handleSetVal);
server.begin();
Serial.println(String("Web site http://")+strIp);
Serial.println(String("File system http://")+strIp+String("/browse"));
Serial.println(String("Update http://")+strIp+String("/update"));
isWebserver_started=true;
#ifdef ESP8266
}else
Serial.println(PSTR("Web server is NOT SET, waiting for pairing"));
#endif

#endif
}

void handleGetVal(){
server.send(200, FPSTR(TEXT_PLAIN), getSwitchVal()?"1":"0");
}
void handleSetVal(){
if (server.args() !=2){
server.send(505, FPSTR(TEXT_PLAIN), "Bad args");
return;
}
//to do analyze
if(server.arg("var") == "ch1"){
if(hapservice){

  homekit_characteristic_t * ch= homekit_service_characteristic_by_type(hapservice, HOMEKIT_CHARACTERISTIC_ON);
  if(ch){
    set_switch(server.arg("val")=="true");
  }
}

}

if(server.arg("var") == "ch2"){
if(hapservice_CD){

  homekit_characteristic_t * ch= homekit_service_characteristic_by_type(hapservice_CD, HOMEKIT_CHARACTERISTIC_ON);
  if(ch){
    set_switch1(server.arg("val")=="true");
  }
}

}
}
void loop() {

#ifdef ESP8266
hap_homekit_loop();
#endif

if(isWebserver_started)
server.handleClient();

}

void init_hap_storage(){
Serial.print("init_hap_storage");

File fsDAT=SPIFFS.open(pair_file_name, "r");

if(!fsDAT){
Serial.println("Failed to read pair.dat");
SPIFFS.format();

}
int size=hap_get_storage_size_ex();
char* buf=new char[size];
memset(buf,0xff,size);
if(fsDAT)
fsDAT.readBytes(buf,size);

hap_init_storage_ex(buf,size);
if(fsDAT)
fsDAT.close();
delete []buf;

}
void storage_changed(char * szstorage,int bufsize){

SPIFFS.remove(pair_file_name);
File fsDAT=SPIFFS.open(pair_file_name, "w+");
if(!fsDAT){
Serial.println("Failed to open pair.dat");
return;
}
fsDAT.write((uint8_t*)szstorage, bufsize);

fsDAT.close();
}
//can be used for any logic, it will automatically inform Apple about state changes

void set_switch(bool val){
Serial.println(String("set_switch:")+String(val?"True":"False"));
digitalWrite(relay_gpio, val?HIGH:LOW);
//we need notify apple about changes

if(hapservice){
Serial.println("notify hap");
//getting on/off characteristic
homekit_characteristic_t * ch= homekit_service_characteristic_by_type(hapservice, HOMEKIT_CHARACTERISTIC_ON);
if(ch){

    if(ch->value.bool_value!=val){  //wil notify only if different
      ch->value.bool_value=val;
      homekit_characteristic_notify(ch,ch->value);
    }
}

}
}

void switch_callback(homekit_characteristic_t *ch, homekit_value_t value, void *context) {
Serial.println("switch_callback");
set_switch(ch->value.bool_value);
}

void set_switch1(bool val){
Serial.println(String("set_switch1:")+String(val?"True":"False"));
digitalWrite(relay1_gpio, val?HIGH:LOW);
//we need notify apple about changes

if(hapservice_CD){
Serial.println("notify hap");
//getting on/off characteristic
homekit_characteristic_t * ch= homekit_service_characteristic_by_type(hapservice_CD, HOMEKIT_CHARACTERISTIC_ON);
if(ch){

    if(ch->value.bool_value!=val){  //wil notify only if different
      ch->value.bool_value=val;
      homekit_characteristic_notify(ch,ch->value);
    }
}

}
}

void switch1_callback(homekit_characteristic_t *ch, homekit_value_t value, void *context) {
Serial.println("switch1_callback");
set_switch1(ch->value.bool_value);
}
#ifdef ENABLE_WIFI_MANAGER
void startwifimanager() {
WiFiManager wifiManager;

if (!wifiManager.autoConnect(HOSTNAME, NULL)) {
ESP.restart();
delay(1000);
}
}
#endif

from esphap.

Yurik72 avatar Yurik72 commented on August 23, 2024

Hi, About
I now also see and unprotected wifi ESP-E4061A, when finished pairing a setting up a new device, that give access to 192.168.4.1 ?? and evrybody can lok into that?

This is not related to this library and standard behaviour of WiFi manager, It's properly documented
Definetelly thgs can happen when ESP not able to connect to your Wifi network and i't started in configuration mode.
So, problem could be

  1. Device can't properly connect to your Wifi network , weak signal , what ever
  2. You have upload a new scetch with option clear ESP. Means all data lost

from esphap.

chrwh avatar chrwh commented on August 23, 2024

Hi,
Can you provide any further help on how to use the below, I can't make it work?
homekit_characteristic_t * ch= homekit_service_characteristic_by_type(hapservice, HOMEKIT_CHARACTERISTIC_ON);
INIT_CHARACHTERISTIC_VAL(bool,ch,false); // will inform apple that lights is OFF
Best regards,
Christian

from esphap.

Yurik72 avatar Yurik72 commented on August 23, 2024

Yes, but could you explain what is not working, compilation, behaviour...
As well please attach a log

from esphap.

chrwh avatar chrwh commented on August 23, 2024

I get this error, if you try to compile the code about (right now the lines are masked out //)

I get this error:

EspHapSwitch8266_Double_Web:146:27: error: expected primary-expression before 'bool'
INIT_CHARACHTERISTIC_VAL(bool,ch,false); // will inform apple that lights is OFF
^
EspHapSwitch8266_Double_Web:146:32: error: 'ch' was not declared in this scope
INIT_CHARACHTERISTIC_VAL(bool,ch,false); // will inform apple that lights is OFF
^
EspHapSwitch8266_Double_Web:146:40: error: 'INIT_CHARACHTERISTIC_VAL' was not declared in this scope
INIT_CHARACHTERISTIC_VAL(bool,ch,false); // will inform apple that lights is OFF

What I want, is the outputs to be OFF when power is initial turned on

from esphap.

Yurik72 avatar Yurik72 commented on August 23, 2024

from esphap.

chrwh avatar chrwh commented on August 23, 2024

from esphap.

Yurik72 avatar Yurik72 commented on August 23, 2024

from esphap.

chrwh avatar chrwh commented on August 23, 2024

from esphap.

chrwh avatar chrwh commented on August 23, 2024

Just checked, the "#define INIT_CHARACHTERISTIC_VAL" is not in the homeintegration.h file, So the link you refere to here as being V.1.0.7 is not it's V.1.0.3 https://github.com/Yurik72/ESPHap/releases/tag/v1.0.7, I now downloaded from the from page, and it can now compile, but with many warining. When trying to access the webserver it crashes
and reboots. This is run in a ESP8266-01 1mb.
File is now attached
EspHapSwitch8266_Double_Web 1.zip

from esphap.

chrwh avatar chrwh commented on August 23, 2024

Updtate: Completely wiped the ESP-01, and it seems to work now. Still getting a lot of compile warnings in regards to Wolfssl?

from esphap.

Yurik72 avatar Yurik72 commented on August 23, 2024

from esphap.

chrwh avatar chrwh commented on August 23, 2024

So far no impact with the Wolfssl errors

from esphap.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.