Yurik72/ESPHap

Max number of switches on ESP32 (M5Stack core) ??

Closed this issue · 2 comments

chrwh commented

Hi,

Not sure this is a bug, it's probaly just me ;-)

When I use my below code with 6 switches, it all works as it should, but when I try to ad 1 or 2 more, it goes into a reboot loop. The 2 final switches are commented out in the code below.

ESPHap version 1.0.9 (1.1.0 won't compile for M5Stack on ESP32 board library V 1.0.7 in Arduino)

Any pointers would be much appriciated.

`//#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>
#include <M5Stack.h>
//RMT Test
#include "driver/rmt.h"
//#include <driver/rmt.h>
//#include <driver/periph_ctrl.h>

//Port pin 16 brugt til IR ud

#define RMT_TX_CHANNEL RMT_CHANNEL_0
#define RMT_TX_GPIO 2 // IR transmit output pin

#define PULSE_ON_T1 200
#define PULSE_OFF_T1 2925 // 3125 // 2925
#define PULSE_OFF_T2 6050 // 6250 // 6050
#define PULSE_OFF_T3 9175 // 9375 // 9175
#define PULSE_OFF_T4 12300 // 12500 // 12300
#define PULSE_OFF_T5 15425 // 15625 // 15425

#define BEO4_CMD_SIZE 22 // sizeof(beo4_cmds) / sizeof(beo4_cmds[0])
#define ALL_OFF_OFFSET (BEO4_CMD_SIZE * 0)
#define AUDIO_GO_OFFSET (BEO4_CMD_SIZE * 1)
#define AUDIO_LIST_OFFSET (BEO4_CMD_SIZE * 2)
#define AUDIO_VUP_OFFSET (BEO4_CMD_SIZE * 3)
#define AUDIO_VDOWN_OFFSET (BEO4_CMD_SIZE * 4)
#define AUDIO_CD_SELECT (BEO4_CMD_SIZE * 5)
#define AUDIO_AUX_SELECT (BEO4_CMD_SIZE * 6)
#define AUDIO_MUTE (BEO4_CMD_SIZE * 7)

static bool firstIn_CD = 0; // Only change state if it's the firste time we are in the sub
static bool firstIn_AUX = 0; // Only change state if it's the firste time we are in the sub
static bool firstIn_OFF = 0; // Only change state if it's the firste time we are in the sub
static bool firstIn_VolUp = 0; // Only change state if it's the firste time we are in the sub
static bool firstIn_VolDown = 0; // Only change state if it's the firste time we are in the sub
static bool firstIn_Mute = 0; // Only change state if it's the firste time we are in the sub

// Remote Control Commands Using 1MHz clock
static const rmt_item32_t beo4_cmds[] = {
// All Off (0x0F0C)
{{{ PULSE_ON_T1, 1, PULSE_OFF_T1, 0 }}}, // Start Bit 1 - AGC
{{{ PULSE_ON_T1, 1, PULSE_OFF_T1, 0 }}}, // Start Bit 2 - AGC
{{{ PULSE_ON_T1, 1, PULSE_OFF_T5, 0 }}}, // Start Bit 3 - Start
{{{ PULSE_ON_T1, 1, PULSE_OFF_T1, 0 }}}, // Start Bit 4 - Link Bit
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}}, // Data Start
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T3, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T1, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T3, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T1, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}}, // Data End
{{{ PULSE_ON_T1, 1, PULSE_OFF_T4, 0 }}}, // Stop Bit
{{{ PULSE_ON_T1, 1, 0, 0 }}}, // Trailer Pulse, End RMT
// Audio-Go (0x0135)
{{{ PULSE_ON_T1, 1, PULSE_OFF_T1, 0 }}}, // Start Bit 1 - AGC
{{{ PULSE_ON_T1, 1, PULSE_OFF_T1, 0 }}}, // Start Bit 2 - AGC
{{{ PULSE_ON_T1, 1, PULSE_OFF_T5, 0 }}}, // Start Bit 3 - Start
{{{ PULSE_ON_T1, 1, PULSE_OFF_T1, 0 }}}, // Start Bit 4 - Link Bit
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}}, // Data Start
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T3, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T1, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T3, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T1, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T3, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T1, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T3, 0 }}}, // Data End
{{{ PULSE_ON_T1, 1, PULSE_OFF_T4, 0 }}}, // Stop Bit
{{{ PULSE_ON_T1, 1, 0, 0 }}}, // Trailer Pulse, End RMT
// Audio-List (0x0158)
{{{ PULSE_ON_T1, 1, PULSE_OFF_T1, 0 }}}, // Start Bit 1 - AGC
{{{ PULSE_ON_T1, 1, PULSE_OFF_T1, 0 }}}, // Start Bit 2 - AGC
{{{ PULSE_ON_T1, 1, PULSE_OFF_T5, 0 }}}, // Start Bit 3 - Start
{{{ PULSE_ON_T1, 1, PULSE_OFF_T1, 0 }}}, // Start Bit 4 - Link Bit
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}}, // Data Start
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T3, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T1, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T3, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T1, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T3, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T1, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}}, // Data End
{{{ PULSE_ON_T1, 1, PULSE_OFF_T4, 0 }}}, // Stop Bit
{{{ PULSE_ON_T1, 1, 0, 0 }}}, // Trailer Pulse, End RMT
// Audio-Volume Up (0x0160)
{{{ PULSE_ON_T1, 1, PULSE_OFF_T1, 0 }}}, // Start Bit 1 - AGC
{{{ PULSE_ON_T1, 1, PULSE_OFF_T1, 0 }}}, // Start Bit 2 - AGC
{{{ PULSE_ON_T1, 1, PULSE_OFF_T5, 0 }}}, // Start Bit 3 - Start
{{{ PULSE_ON_T1, 1, PULSE_OFF_T1, 0 }}}, // Start Bit 4 - Link Bit
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}}, // Data Start
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T3, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T1, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T3, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T1, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}}, // Data End
{{{ PULSE_ON_T1, 1, PULSE_OFF_T4, 0 }}}, // Stop Bit
{{{ PULSE_ON_T1, 1, 0, 0 }}}, // Trailer Pulse, End RMT
// Audio-Volume Down (0x0164)
{{{ PULSE_ON_T1, 1, PULSE_OFF_T1, 0 }}}, // Start Bit 1 - AGC
{{{ PULSE_ON_T1, 1, PULSE_OFF_T1, 0 }}}, // Start Bit 2 - AGC
{{{ PULSE_ON_T1, 1, PULSE_OFF_T5, 0 }}}, // Start Bit 3 - Start
{{{ PULSE_ON_T1, 1, PULSE_OFF_T1, 0 }}}, // Start Bit 4 - Link Bit
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}}, // Data Start
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T3, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T1, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T3, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T1, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T3, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T1, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}}, // Data End
{{{ PULSE_ON_T1, 1, PULSE_OFF_T4, 0 }}}, // Stop Bit
{{{ PULSE_ON_T1, 1, 0, 0 }}}, // Trailer Pulse, End RMT
// CD (0x0192)
{{{ PULSE_ON_T1, 1, PULSE_OFF_T1, 0 }}}, // Start Bit 1 - AGC
{{{ PULSE_ON_T1, 1, PULSE_OFF_T1, 0 }}}, // Start Bit 2 - AGC
{{{ PULSE_ON_T1, 1, PULSE_OFF_T5, 0 }}}, // Start Bit 3 - Start
{{{ PULSE_ON_T1, 1, PULSE_OFF_T1, 0 }}}, // Start Bit 4 - Link Bit
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}}, // Data Start
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T3, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},//1
{{{ PULSE_ON_T1, 1, PULSE_OFF_T1, 0 }}},//2
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},//3
{{{ PULSE_ON_T1, 1, PULSE_OFF_T3, 0 }}},//4
{{{ PULSE_ON_T1, 1, PULSE_OFF_T1, 0 }}},//5
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},//6
{{{ PULSE_ON_T1, 1, PULSE_OFF_T3, 0 }}},//7
{{{ PULSE_ON_T1, 1, PULSE_OFF_T1, 0 }}},//8// Data End
{{{ PULSE_ON_T1, 1, PULSE_OFF_T4, 0 }}}, // Stop Bit
{{{ PULSE_ON_T1, 1, 0, 0 }}}, // Trailer Pulse, End RMT
// Audio-AUX (0x0183)
{{{ PULSE_ON_T1, 1, PULSE_OFF_T1, 0 }}}, // Start Bit 1 - AGC
{{{ PULSE_ON_T1, 1, PULSE_OFF_T1, 0 }}}, // Start Bit 2 - AGC
{{{ PULSE_ON_T1, 1, PULSE_OFF_T5, 0 }}}, // Start Bit 3 - Start
{{{ PULSE_ON_T1, 1, PULSE_OFF_T1, 0 }}}, // Start Bit 4 - Link Bit
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}}, // 1 = 0 Data Start
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}}, // 2 = 0
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}}, // 3 = 0
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}}, // 4 = 0
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}}, // 5 = 0
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}}, // 6 = 0
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}}, // 7 = 0
{{{ PULSE_ON_T1, 1, PULSE_OFF_T3, 0 }}}, // 8 = 1
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}}, // 9 = 1
{{{ PULSE_ON_T1, 1, PULSE_OFF_T1, 0 }}}, // 10 = 0
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}}, // 11 = 0
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}}, // 12 = 0
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}}, // 13 = 0
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}}, // 14 = 0
{{{ PULSE_ON_T1, 1, PULSE_OFF_T3, 0 }}}, // 15 = 1
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}}, // 16 = 1 Data End
{{{ PULSE_ON_T1, 1, PULSE_OFF_T4, 0 }}}, // Stop Bit
{{{ PULSE_ON_T1, 1, 0, 0 }}}, // Trailer Pulse, End RMT
// MUTE (0x010C)
{{{ PULSE_ON_T1, 1, PULSE_OFF_T1, 0 }}}, // Start Bit 1 - AGC
{{{ PULSE_ON_T1, 1, PULSE_OFF_T1, 0 }}}, // Start Bit 2 - AGC
{{{ PULSE_ON_T1, 1, PULSE_OFF_T5, 0 }}}, // Start Bit 3 - Start
{{{ PULSE_ON_T1, 1, PULSE_OFF_T1, 0 }}}, // Start Bit 4 - Link Bit
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}}, // Data Start
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T3, 0 }}},
{{{ PULSE_ON_T1, 1, PULSE_OFF_T1, 0 }}},//1
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},//2
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},//3
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},//4
{{{ PULSE_ON_T1, 1, PULSE_OFF_T3, 0 }}},//5
{{{ PULSE_ON_T1, 1, PULSE_OFF_T2, 0 }}},//6
{{{ PULSE_ON_T1, 1, PULSE_OFF_T1, 0 }}},//7
{{{ PULSE_ON_T1, 1, PULSE_OFF_T3, 0 }}},//8// Data End
{{{ PULSE_ON_T1, 1, PULSE_OFF_T4, 0 }}}, // Stop Bit
{{{ PULSE_ON_T1, 1, 0, 0 }}}, // Trailer Pulse, End RMT
};

// RMT Tx Channel Initialization
static void rmt_tx_init(void)
{
rmt_config_t config;

config.rmt_mode = RMT_MODE_TX;
config.channel = RMT_TX_CHANNEL;
config.gpio_num = (gpio_num_t)RMT_TX_GPIO;
config.mem_block_num = 1;
config.tx_config.carrier_level = RMT_CARRIER_LEVEL_HIGH;
config.tx_config.idle_level = RMT_IDLE_LEVEL_LOW;
config.tx_config.loop_en = false;
config.tx_config.idle_output_en = true;
config.tx_config.carrier_en = true;
config.tx_config.carrier_duty_percent = 50;
config.tx_config.carrier_freq_hz = 455000;
config.clk_div = 80;

ESP_ERROR_CHECK(rmt_config(&config));

ESP_ERROR_CHECK(rmt_driver_install(config.channel, 0, 0));
}
//RMT Test end

//#include "driver/rmt.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=34; //AUX
const int relay1_gpio=5; //CD
const int mute_gpio=25; //Mute
const int volUp_gpio=26; //VolUp
const int volDown_gpio=36; //VolDown

//const int Sluk_gpio=23; //VolDown
//const int Prev_gpio=18; //VolDown
//const int Next_gpio=19; //VolDown

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

const char* HOSTNAME="SwitchServer2";
const char* ssid = "xxxxxx";
const char* password = "xxxxxxxx";

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

homekit_service_t* hapservice={0};
homekit_service_t* hapservice_CD={0};
homekit_service_t* hapservice_VolUp={0};
homekit_service_t* hapservice_VolDown={0};
homekit_service_t* hapservice_Mute={0};
homekit_service_t* hapservice_Sluk={0};
//homekit_service_t* hapservice_Prev={0};
//homekit_service_t* hapservice_Next={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_CD, homekit_value_t value, void *context);
void switch2_callback(homekit_characteristic_t *ch_VolUp, homekit_value_t value, void *context);
void switch3_callback(homekit_characteristic_t *ch_VolDown, homekit_value_t value, void *context);
void switch4_callback(homekit_characteristic_t *ch_Mute, homekit_value_t value, void *context);
void switch5_callback(homekit_characteristic_t *ch_Sluk, homekit_value_t value, void *context);
//void switch6_callback(homekit_characteristic_t *ch_Prev, homekit_value_t value, void *context);
//void switch7_callback(homekit_characteristic_t *ch_Next, 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_CD= homekit_service_characteristic_by_type(hapservice_CD, HOMEKIT_CHARACTERISTIC_ON);
  if(ch_CD){
    return ch_CD->value.bool_value;
  }
}

    if(hapservice_VolUp){
  homekit_characteristic_t * ch_VolUp= homekit_service_characteristic_by_type(hapservice_VolUp, HOMEKIT_CHARACTERISTIC_ON);
  if(ch_VolUp){
    return ch_VolUp->value.bool_value;
  }
}

    if(hapservice_VolDown){
  homekit_characteristic_t * ch_VolDown= homekit_service_characteristic_by_type(hapservice_VolDown, HOMEKIT_CHARACTERISTIC_ON);
  if(ch_VolDown){
    return ch_VolDown->value.bool_value;
  }
}

   if(hapservice_Mute){
  homekit_characteristic_t * ch_Mute= homekit_service_characteristic_by_type(hapservice_Mute, HOMEKIT_CHARACTERISTIC_ON);
  if(ch_Mute){
    return ch_Mute->value.bool_value;
  }
}

   if(hapservice_Sluk){
  homekit_characteristic_t * ch_Sluk= homekit_service_characteristic_by_type(hapservice_Sluk, HOMEKIT_CHARACTERISTIC_ON);
  if(ch_Sluk){
    return ch_Sluk->value.bool_value;
  }
}

// if(hapservice_Prev){
// homekit_characteristic_t * ch_Prev= homekit_service_characteristic_by_type(hapservice_Prev, HOMEKIT_CHARACTERISTIC_ON);
// if(ch_Prev){
// return ch_Prev->value.bool_value;
// }
// }

// if(hapservice_Next){
// homekit_characteristic_t * ch_Next= homekit_service_characteristic_by_type(hapservice_Next, HOMEKIT_CHARACTERISTIC_ON);
// if(ch_Next){
// return ch_Next->value.bool_value;
// }
// }

return false;

}

void setup() {

M5.begin();
M5.Lcd.drawCentreString("Running...", 160, 190, 4);

#ifdef ESP8266
//disable_extra4k_at_link_time();
#endif

Serial.begin(115200);
delay(500);

#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);
pinMode(volUp_gpio, OUTPUT);
pinMode(volDown_gpio, OUTPUT);
pinMode(mute_gpio, OUTPUT);
//pinMode(Sluk_gpio, OUTPUT);
//pinMode(Prev_gpio, OUTPUT);
//pinMode(Next_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("SwitchUdestue","Test","0","SwitchHepta","1.01");

//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);
hapservice_VolUp= hap_add_switch_service("Vol UP",switch2_callback,(void*)&volUp_gpio);
hapservice_VolDown= hap_add_switch_service("Vol Down",switch3_callback,(void*)&volDown_gpio);
hapservice_Mute= hap_add_switch_service("Mute",switch4_callback,(void*)&mute_gpio);
hapservice_Sluk= hap_add_switch_service("Sluk",switch5_callback,(void*)&relay_gpio); //Dummy Mute GPIO To BE Tested
// hapservice_Prev= hap_add_switch_service("Prev",switch6_callback,(void*)&relay_gpio); //Dummy Mute GPIO To BE Tested
// hapservice_Next= hap_add_switch_service("Next",switch7_callback,(void*)&relay_gpio); //Dummy Mute GPIO To BE Tested

//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 * ch_CD= homekit_service_characteristic_by_type(hapservice_CD, HOMEKIT_CHARACTERISTIC_ON);
INIT_CHARACHTERISTIC_VAL(bool,ch_CD,false); // will inform apple that lights is OFF

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

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

homekit_characteristic_t * ch_Mute= homekit_service_characteristic_by_type(hapservice_Mute, HOMEKIT_CHARACTERISTIC_ON);

INIT_CHARACHTERISTIC_VAL(bool,ch_Mute,false); // will inform apple that lights is OFF

homekit_characteristic_t * ch_Sluk= homekit_service_characteristic_by_type(hapservice_Sluk, HOMEKIT_CHARACTERISTIC_ON);

INIT_CHARACHTERISTIC_VAL(bool,ch_Sluk,false); // will inform apple that lights is OFF

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

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

#ifdef ENABLE_WIFI_MANAGER

startwifimanager();
#else

WiFi.mode(WIFI_STA);

// WiFi.setOutputPower(35);
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
// ?? {} manglede foran og bagved Serial print
{
Serial.println(PSTR("Web server is NOT SET, waiting for pairing"));
}

#endif

#endif

// RMT Test
periph_module_enable(PERIPH_RMT_MODULE);
rmt_tx_init();
rmt_set_source_clk(RMT_TX_CHANNEL, RMT_BASECLK_APB); // RMT_BASECLK_APB or RMT_BASECLK_MAX or RMT_BASECLK_REF
rmt_write_items(RMT_TX_CHANNEL, &beo4_cmds[ALL_OFF_OFFSET], BEO4_CMD_SIZE, true);

// RMT Test end
}

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_CD= homekit_service_characteristic_by_type(hapservice_CD, HOMEKIT_CHARACTERISTIC_ON);
  if(ch_CD){
    set_switch1(server.arg("val")=="true");
  }
}

}

  if(server.arg("var") == "ch3"){
if(hapservice_VolUp){

  homekit_characteristic_t * ch_VolUp= homekit_service_characteristic_by_type(hapservice_VolUp, HOMEKIT_CHARACTERISTIC_ON);
  if(ch_VolUp){
    set_switch2(server.arg("val")=="true");
  }
}

}

  if(server.arg("var") == "ch4"){
if(hapservice_VolDown){

  homekit_characteristic_t * ch_VolDown= homekit_service_characteristic_by_type(hapservice_VolDown, HOMEKIT_CHARACTERISTIC_ON);
  if(ch_VolDown){
    set_switch3(server.arg("val")=="true");
  }
}

}

    if(server.arg("var") == "ch5"){
if(hapservice_Mute){

  homekit_characteristic_t * ch_Mute= homekit_service_characteristic_by_type(hapservice_Mute, HOMEKIT_CHARACTERISTIC_ON);
  if(ch_Mute){
    set_switch4(server.arg("val")=="true");
  }
}

}

   if(server.arg("var") == "ch6"){
if(hapservice_Sluk){

  homekit_characteristic_t * ch_Sluk= homekit_service_characteristic_by_type(hapservice_Sluk, HOMEKIT_CHARACTERISTIC_ON);
  if(ch_Sluk){
    set_switch5(server.arg("val")=="true");
  }
}

}

// if(server.arg("var") == "ch7"){
// if(hapservice_Prev){
//
// homekit_characteristic_t * ch_Prev= homekit_service_characteristic_by_type(hapservice_Prev, HOMEKIT_CHARACTERISTIC_ON);
// if(ch_Prev){
// set_switch6(server.arg("val")=="true");
// }
// }
// }

// if(server.arg("var") == "ch8"){
// if(hapservice_Next){
//
// homekit_characteristic_t * ch_Next= homekit_service_characteristic_by_type(hapservice_Next, HOMEKIT_CHARACTERISTIC_ON);
// if(ch_Next){
// set_switch7(server.arg("val")=="true");
// }
// }
// }

}

void loop() {
M5.update(); // Update the M5

#ifdef ESP8266
hap_homekit_loop();
#endif
// long rssi = WiFi.RSSI();
// Serial.print("RSSI: ");
// Serial.println(rssi);

if(isWebserver_started)
server.handleClient();

//////////////////////
// Button A pressed //
//////////////////////
if (M5.BtnA.isPressed()) {
//Turn On AUX
rmt_write_items(RMT_TX_CHANNEL, &beo4_cmds[AUDIO_AUX_SELECT], BEO4_CMD_SIZE, true);
Serial.println("B&O AUX ON\n");
delay(1000);
}

//////////////////////
// Button B pressed //
//////////////////////
if (M5.BtnB.isPressed()) {
// Turn On CD Test
rmt_write_items(RMT_TX_CHANNEL, &beo4_cmds[AUDIO_CD_SELECT], BEO4_CMD_SIZE, true);
Serial.println("B&O CD ON\n");
delay(1000);
}

//////////////////////
// Button C pressed //
//////////////////////
if (M5.BtnC.isPressed()) {
//Turn Off Test
rmt_write_items(RMT_TX_CHANNEL, &beo4_cmds[ALL_OFF_OFFSET], BEO4_CMD_SIZE, true);
Serial.println("B&O SLUK ON\n");
delay(1000);
}

} // End main Loop

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

// Switch AUX
void set_switch(bool val){
Serial.println(String("set_switch:")+String(val?"True":"False"));
//digitalWrite(relay_gpio, val?HIGH:LOW);
rmt_write_items(RMT_TX_CHANNEL, &beo4_cmds[AUDIO_AUX_SELECT], BEO4_CMD_SIZE, true);
//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);
}
// Switch AUX Slut

//Switch CD
void set_switch1(bool val){
Serial.println(String("set_switch1:")+String(val?"True":"False"));
//digitalWrite(relay1_gpio, val?HIGH:LOW);
Serial.println("CD Knap er aktiv");
rmt_write_items(RMT_TX_CHANNEL, &beo4_cmds[AUDIO_CD_SELECT], BEO4_CMD_SIZE, true);
//we need notify apple about changes

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

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

}
}

void switch1_callback(homekit_characteristic_t *ch_CD, homekit_value_t value, void *context) {
Serial.println("switch1_callback");
set_switch1(ch_CD->value.bool_value);
}
// Swicth CD Slut

// Switch VolUp
void set_switch2(bool val){
Serial.println(String("set_switch2:")+String(val?"True":"False"));
//digitalWrite(volUp_gpio, val?HIGH:LOW);
rmt_write_items(RMT_TX_CHANNEL, &beo4_cmds[AUDIO_VUP_OFFSET], BEO4_CMD_SIZE, true);
delay(100);
rmt_write_items(RMT_TX_CHANNEL, &beo4_cmds[AUDIO_VUP_OFFSET], BEO4_CMD_SIZE, true);
delay(100);
rmt_write_items(RMT_TX_CHANNEL, &beo4_cmds[AUDIO_VUP_OFFSET], BEO4_CMD_SIZE, true);
//we need notify apple about changes

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

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

}
}

void switch2_callback(homekit_characteristic_t *ch_VolUp, homekit_value_t value, void *context) {
Serial.println("switch2_callback");
set_switch2(ch_VolUp->value.bool_value);
}
// Switch VolUp Slut

// Switch VolDown
void set_switch3(bool val){
Serial.println(String("set_switch3:")+String(val?"True":"False"));
//digitalWrite(volDown_gpio, val?HIGH:LOW);
rmt_write_items(RMT_TX_CHANNEL, &beo4_cmds[AUDIO_VDOWN_OFFSET], BEO4_CMD_SIZE, true);
delay(100);
rmt_write_items(RMT_TX_CHANNEL, &beo4_cmds[AUDIO_VDOWN_OFFSET], BEO4_CMD_SIZE, true);
delay(100);
rmt_write_items(RMT_TX_CHANNEL, &beo4_cmds[AUDIO_VDOWN_OFFSET], BEO4_CMD_SIZE, true);
//we need notify apple about changes

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

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

}
}
void switch3_callback(homekit_characteristic_t *ch_VolDown, homekit_value_t value, void *context) {
Serial.println("switch3_callback");
set_switch3(ch_VolDown->value.bool_value);
}
// Switch VolDown Slut

// Switch Mute start
void set_switch4(bool val){
Serial.println(String("set_switch4:")+String(val?"True":"False"));
//digitalWrite(mute_gpio, val?HIGH:LOW);
rmt_write_items(RMT_TX_CHANNEL, &beo4_cmds[AUDIO_MUTE], BEO4_CMD_SIZE, true);
//we need notify apple about changes

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

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

}
}
void switch4_callback(homekit_characteristic_t *ch_Mute, homekit_value_t value, void *context) {
Serial.println("switch4_callback");
set_switch4(ch_Mute->value.bool_value);
}
// Switch Mute end

// Switch Sluk start
void set_switch5(bool val){
Serial.println(String("set_switch5:")+String(val?"True":"False"));
//digitalWrite(mute_gpio, val?HIGH:LOW);
rmt_write_items(RMT_TX_CHANNEL, &beo4_cmds[AUDIO_MUTE], BEO4_CMD_SIZE, true);
//we need notify apple about changes

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

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

}
}
void switch5_callback(homekit_characteristic_t *ch_Sluk, homekit_value_t value, void *context) {
Serial.println("switch5_callback");
set_switch5(ch_Sluk->value.bool_value);
}
//Switch Sluk end

//// Switch Prev start
//void set_switch6(bool val){
// Serial.println(String("set_switch6:")+String(val?"True":"False"));
// //digitalWrite(mute_gpio, val?HIGH:LOW);
// rmt_write_items(RMT_TX_CHANNEL, &beo4_cmds[AUDIO_MUTE], BEO4_CMD_SIZE, true);
// //we need notify apple about changes
//
// if(hapservice_Prev){
// Serial.println("notify hap");
// //getting on/off characteristic
// homekit_characteristic_t * ch_Prev= homekit_service_characteristic_by_type(hapservice_Prev, HOMEKIT_CHARACTERISTIC_ON);
// if(ch_Prev){
//
// if(ch_Prev->value.bool_value!=val){ //wil notify only if different
// ch_Prev->value.bool_value=val;
// homekit_characteristic_notify(ch_Prev,ch_Prev->value);
// }
// }
// }
//}
//void switch6_callback(homekit_characteristic_t *ch_Prev, homekit_value_t value, void *context) {
// Serial.println("switch6_callback");
// set_switch6(ch_Prev->value.bool_value);
//}
//// Switch Prev end

// Switch Next start
//void set_switch7(bool val){
// Serial.println(String("set_switch7:")+String(val?"True":"False"));
// //digitalWrite(mute_gpio, val?HIGH:LOW);
// rmt_write_items(RMT_TX_CHANNEL, &beo4_cmds[AUDIO_MUTE], BEO4_CMD_SIZE, true);
// //we need notify apple about changes
//
// if(hapservice_Next){
// Serial.println("notify hap");
// //getting on/off characteristic
// homekit_characteristic_t * ch_Next= homekit_service_characteristic_by_type(hapservice_Next, HOMEKIT_CHARACTERISTIC_ON);
// if(ch_Next){
//
// if(ch_Next->value.bool_value!=val){ //wil notify only if different
// ch_Next->value.bool_value=val;
// homekit_characteristic_notify(ch_Next,ch_Next->value);
// }
// }
// }
//}
//void switch7_callback(homekit_characteristic_t *ch_Next, homekit_value_t value, void *context) {
// Serial.println("switch7_callback");
// set_switch7(ch_Next->value.bool_value);
//}
//// Switch Prev end

#ifdef ENABLE_WIFI_MANAGER
void startwifimanager() {
WiFiManager wifiManager;

wifiManager.setConfigPortalTimeout(120);
wifiManager.setConnectTimeout(60);

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

m5_ESPHap.txt

Hi could you pleaase check if you are using latest version
As well please check homeintegration.c
Looks you are on above off allowed range . So what is define for MAX_HAP_SERVICES
Should be
#define ESPHAP_VERSION "v1.1"
#ifdef ESP8266
#define MAX_HAP_SERVICES 8
#define MAX_HAP_ACCESSORIES 8
#else // ESP32.
#define MAX_HAP_SERVICES 45
#define MAX_HAP_ACCESSORIES 45
#endif

As weel please comment any logic in your callbacks ( set_switch6)
I see you are usign RMT, but not able briefly analyze if this not raise an error

chrwh commented

Hi,
Thanks for the answer, I had tried the latest version thru Arduino, but that did not work. Downloaded the latest from here and installed it manually, and now, everythings is working as it should.

I would like you to know, that I really LIKE this software. I use it several places in my house :-) so THANKS for making this

Best regards
Christian