Skip to content

Commit

Permalink
Switch sample revised
Browse files Browse the repository at this point in the history
New sample StatelessSwitch added
  • Loading branch information
HolgerBurkarth committed Dec 11, 2024
1 parent 30a06f1 commit a6ccceb
Show file tree
Hide file tree
Showing 8 changed files with 613 additions and 244 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,12 @@ Read more: [Garage Door Opener Project](./examples/GarageDoor/README.md)

### Change Log

#### v2.0.4 (2024-12-10)
#### v2.0.4 (2024-12-11)
* The ability to update firmware via a URL. (Firmware is the executable image).
* Evolution to improve position measurement with the SR04 sensor.
* Device information notices booting
* Switch sample revised
* New sample StatelessSwitch added

#### v2.0.3 (2024-10-11)
* Support and examples for a heater/cooler/thermostat (temperature, humidity, etc.)
Expand Down
117 changes: 117 additions & 0 deletions examples/StatelessSwitch/StatelessSwitch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#pragma region Prolog
/*******************************************************************
$CRT 11 Dez 2024 : hb
$AUT Holger Burkarth
$DAT >>StatelessSwitch.h<< 11 Dez 2024 16:24:30 - (c) proDAD
*******************************************************************/
#pragma endregion
#pragma region Spelling
// Ignore Spelling:
#pragma endregion
#pragma region Description
/*
--EN--
--DE--
*/
#pragma endregion
#pragma region Includes
#include <HomeKit_Switch.h>

using namespace HBHomeKit;
using namespace HBHomeKit::Switch;
#pragma endregion

#pragma region PIN Config

/*
* @brief
*/
#define SWITCHER_PIN D7


#pragma endregion

#pragma region HomeKit Config

/*
* @brief A HomeKit device service contains the basic information of the device
* @note The device name is also used to create the Bonjour service name.
*/
const CDeviceService Device
{
{
.DeviceName{"Switch"}, // available as http://Switch.local
/*
* ... more optional device information: You can read more about the CDeviceService in hb_homekit.h
*/
}
};


/*
* @brief Switcher as a service for HomeKit
*/
CStatelessSwitchService StatelessSwitch
({
.Name = "SlessSwitch",
});

/*
* @brief HomeKit provides everything you need to interact with Apple HomeKit and host a homepage.
*/
CHomeKit HomeKit
(
Device,
homekit_accessory_category_programmable_switch,
&StatelessSwitch
);

CPrgSwitchEventHandler gbEventHandler(SWITCHER_PIN, StatelessSwitch);

#pragma endregion


#pragma region setup
void setup()
{
Serial.begin(115200);
delay(500); // Important for ESP32: Wait until the serial interface is ready
Serial.println("\n\n\nEnter Setup");

// Installs and configures everything for Switcher.
InstallVarsAndCmds(HomeKit);
AddMenuItems(HomeKit);
gbEventHandler.Setup(HomeKit);

// Adds a menu (WiFi) that allows the user to connect to a WiFi network.
AddWiFiLoginMenu(HomeKit);

// Adds standard menu items to the controller.
AddStandardMenus(HomeKit);

// Installs the action UI, required for garage/WiFi web-pages.
InstallActionUI(HomeKit);

// Starts HomeKit
if(!HomeKit.Setup())
{
ERROR("HomeKit setup failed");
return;
}

INFO("Setup finished");
}

#pragma endregion

#pragma region loop
void loop()
{
HomeKit.Loop();
}

#pragma endregion
12 changes: 12 additions & 0 deletions examples/StatelessSwitch/StatelessSwitch.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*******************************************************************
$CRT 11 Dez 2024 : hb
$AUT Holger Burkarth
$DAT >>StatelessSwitch.ino<< 11 Dez 2024 16:24:43 - (c) proDAD
*******************************************************************/
/*
* The source code has been moved to a .h file to take advantage
* of syntax highlighting and other features of preferred IDEs
* such as Visual Studio.
*/
#include "StatelessSwitch.h"
114 changes: 5 additions & 109 deletions examples/Switch/Switch.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
$CRT 03 Okt 2024 : hb
$AUT Holger Burkarth
$DAT >>Switch.h<< 03 Okt 2024 15:36:50 - (c) proDAD
$DAT >>Switch.h<< 11 Dez 2024 15:39:39 - (c) proDAD
*******************************************************************/
#pragma endregion
#pragma region Spelling
Expand Down Expand Up @@ -58,7 +58,8 @@ const CDeviceService Device
CSwitchService Switcher
({
.Name = "Switch",
.Setter = [](homekit_characteristic_t* pC, homekit_value_t value)
#if 1
.Setter = [](homekit_characteristic_t* pC, homekit_value_t value)
{
VERBOSE("Switch::Setter");
if(modify_value(pC, value))
Expand All @@ -67,11 +68,13 @@ CSwitchService Switcher
digitalWrite(SWITCHER_PIN, Stat ? HIGH : LOW);
}
},
#else
.Getter = [](const homekit_characteristic_t* pC) -> homekit_value_t
{
VERBOSE("Switch::Getter");
return static_value_cast<bool>(digitalRead(SWITCHER_PIN) == HIGH);
}
#endif
});

/*
Expand All @@ -86,112 +89,6 @@ CHomeKit HomeKit

#pragma endregion

#pragma region InstallVarsAndCmds
void InstallVarsAndCmds(CController& c)
{
c
.SetVar("SWITCH", [](auto p)
{
if(p.Args[0] != nullptr) // case of: /?SWITCH=true
{
/* "true", "TRUE", "1", "ON", "on" ==> bool */
bool NewState = convert_value<bool>(*p.Args[0]);
modify_value_and_notify(&Switcher.State, NewState);
}

/* return "true" or "false" */
return MakeTextEmitter(Switcher.State);
})

;
}

#pragma endregion

#pragma region Switch_JavaScript
CTextEmitter Switch_JavaScript()
{
return MakeTextEmitter(F(R"(
function UIUpdateSwitch(state)
{
var State = (state == 'true');
SetElementInnerHTML('state', State ? 'ON' : 'OFF');
SetElementChecked('switch', State);
}
function SetSwitch(state)
{
ForSetVar('SWITCH', state, responseText => UIUpdateSwitch(responseText) );
}
function Update()
{
ForVar('SWITCH', responseText => UIUpdateSwitch(responseText) );
}
onload = function()
{
Update();
setInterval
(
function()
{
Update();
},
1000
);
};
)"));
}
#pragma endregion

#pragma region Switch_BodyHtml
CTextEmitter Switch_BodyHtml()
{
return MakeTextEmitter(F(R"(
<table><tr><th>Switcher is</th><td><div id="state"></div></td></tr></table>
<p>
{ACTION_CHECKBOX:switch:SetSwitch(this.checked)}
&nbsp;
&nbsp;
&nbsp;
{ACTION_BUTTON:SetSwitch(false):OFF}
{ACTION_BUTTON:SetSwitch(true):ON}
</p>
)"));
}
#pragma endregion

#pragma region AddMenuItems
void AddMenuItems(CController& c)
{
c
.AddMenuItem
(
{
.Title = "Switch",
.MenuName = "Start",
.URI = "/",
.CSS = ActionUI_CSS(),
.JavaScript = [](Stream& out)
{
out << ActionUI_JavaScript();
out << UIUtils_JavaScript();
out << Switch_JavaScript();
},
.Body = Switch_BodyHtml()
}
)
;
}

#pragma endregion


#pragma region setup
void setup()
Expand All @@ -201,7 +98,6 @@ void setup()
Serial.println("\n\n\nEnter Setup");

pinMode(SWITCHER_PIN, OUTPUT);
//digitalWrite(SWITCHER_PIN, LOW);


// Installs and configures everything for Switcher.
Expand Down
Loading

0 comments on commit a6ccceb

Please sign in to comment.