Skip to content

Commit

Permalink
Added shutter control device firmware example.
Browse files Browse the repository at this point in the history
  • Loading branch information
genemars committed Jan 8, 2024
1 parent 4de9c94 commit f016857
Show file tree
Hide file tree
Showing 22 changed files with 666 additions and 44 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,15 @@ pio run -e smart-sensor-d1-mini -t upload
pio run -e smart-sensor-d1-mini-esp32 -t upload
```

### Shutter control example

Wi-Fi controlled shutter motor.

**Generic ESP32**
```bash
pio run -e shutter -t upload
```


### X10 transceiver example

Expand Down
37 changes: 3 additions & 34 deletions examples/playground/playground.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,62 +24,31 @@
*
* Releases:
* - 2019-01-10 v1.0: initial release.
* - 2023-12-08 v1,1: added BLE support; targeting ESP32 by default.
*
*/

#include <HomeGenie.h>

#include <ESP32Servo.h>
Servo myservo;
int pos = 0;

#include "configuration.h"

using namespace IO;
using namespace Service;

HomeGenie* homeGenie;

const int frequency = 50; // Hz // Standard is 50(hz) servo
const int servoPin = 15;
int minUs = 500;
int maxUs = 2500;

int minWrite = 500; // 0
int maxWrite = 2500; // 180
int inc = 3;
unsigned long lastStepTs = millis();

void setup() {

homeGenie = HomeGenie::getInstance();
//auto miniModule = homeGenie->getDefaultModule();

auto miniModule = homeGenie->getDefaultModule();
// TODO: ..

homeGenie->begin();

myservo.setPeriodHertz(frequency);
myservo.attach(servoPin, minUs, maxUs);

//myservo.attach(servoPin);

//myservo.detach();
}

void loop()
{
homeGenie->loop();

if (millis() - lastStepTs > 15) {
lastStepTs = millis();
if (pos <= maxWrite) {
pos += inc;
Serial.println(pos);
myservo.write(pos);
} else { pos = minWrite;
delay(5000);
}
}
// TODO: ..

}
1 change: 0 additions & 1 deletion examples/rf-transceiver/rf-transceiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
*
* Releases:
* - 2019-01-10 v1.0: initial release.
* - 2023-12-08 v1,1: added BLE support; targeting ESP32 by default.
*
*/

Expand Down
Empty file added examples/shutter/README.md
Empty file.
132 changes: 132 additions & 0 deletions examples/shutter/api/ShutterHandler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* HomeGenie-Mini (c) 2018-2024 G-Labs
*
*
* This file is part of HomeGenie-Mini (HGM).
*
* HomeGenie-Mini is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HomeGenie-Mini is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HomeGenie-Mini. If not, see <http://www.gnu.org/licenses/>.
*
*
* Authors:
* - Generoso Martello <[email protected]>
*
*
* Releases:
* - 2024-01-06 Initial release
*
*/

#include "ShutterHandler.h"

namespace Service { namespace API {

ShutterHandler::ShutterHandler(ShutterControl* servoControl) {
this->servoControl = servoControl;

auto domain = IO::IOEventDomains::Automation_Components;
// Shutter module
shutterModule = new Module();
shutterModule->domain = domain;
shutterModule->address = SERVO_MODULE_ADDRESS;
shutterModule->type = "Shutter";
shutterModule->name = "Shutter 1";
// add properties
shutterLevel = new ModuleParameter(IOEventPaths::Status_Level);
shutterModule->properties.add(shutterLevel);

moduleList.add(shutterModule);
}

void ShutterHandler::init() {

}


bool ShutterHandler::handleRequest(APIRequest *command, WebServer &server) {

if (command->Domain == (IOEventDomains::Automation_Components)
&& command->Address == SERVO_MODULE_ADDRESS) {

if (command->Command == "Control.Level") {

float level = command->OptionsString.toFloat();

servoControl->setLevel(level);

command->Response = R"({ "ResponseText": "OK" })";

} else if (command->Command == "Control.Close" || command->Command == "Control.Off") {

servoControl->close();

} else if (command->Command == "Control.Open" || command->Command == "Control.On") {

servoControl->open();

} else {

return false;

}
return true;
}

return false;
}

bool ShutterHandler::canHandleDomain(String* domain) {
return domain->equals(IO::IOEventDomains::Automation_Components);
}

bool ShutterHandler::handleEvent(IIOEventSender *sender,
const char* domain, const char* address,
const unsigned char *eventPath, void *eventData, IOEventDataType dataType) {
auto module = getModule(domain, address);
if (module) {
auto event = String((char *) eventPath);
// Event Stream Message Enqueue (for MQTT/SSE/WebSocket propagation)
auto m = QueuedMessage(domain, address, event.c_str(), "");
// Data type handling
switch (dataType) {
case Number:
m.value = String(*(int32_t *) eventData);
break;
case Float:
m.value = String(*(float *) eventData);
break;
case Integer:
m.value = String(*(int16_t *) eventData);
break;
default:
m.value = String(*(int32_t *) eventData);
}
module->setProperty(event, m.value);
HomeGenie::getInstance()->getEventRouter().signalEvent(m);
}
return false;
}

Module* ShutterHandler::getModule(const char* domain, const char* address) {
for (int i = 0; i < moduleList.size(); i++) {
Module* module = moduleList.get(i);
if (module->domain.equals(domain) && module->address.equals(address))
return module;
}
return nullptr;
}
LinkedList<Module*>* ShutterHandler::getModuleList() {
return &moduleList;
}

}}
61 changes: 61 additions & 0 deletions examples/shutter/api/ShutterHandler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* HomeGenie-Mini (c) 2018-2024 G-Labs
*
*
* This file is part of HomeGenie-Mini (HGM).
*
* HomeGenie-Mini is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HomeGenie-Mini is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HomeGenie-Mini. If not, see <http://www.gnu.org/licenses/>.
*
*
* Authors:
* - Generoso Martello <[email protected]>
*
*
* Releases:
* - 2024-01-06 Initial release
*
*/

#ifndef HOMEGENIE_MINI_SHUTTERHANDLER_H
#define HOMEGENIE_MINI_SHUTTERHANDLER_H

#include <HomeGenie.h>

#include "../io/ShutterControl.h"

namespace Service { namespace API {

using namespace IO::Components;

class ShutterHandler : public APIHandler {
private:
LinkedList<Module*> moduleList;
Module* shutterModule;
ModuleParameter* shutterLevel;
ShutterControl* servoControl;
public:
ShutterHandler(ShutterControl* servoControl);
void init() override;
bool canHandleDomain(String* domain) override;
bool handleRequest(APIRequest *request, WebServer &server) override;
bool handleEvent(IIOEventSender *sender,
const char* domain, const char* address,
const unsigned char *eventPath, void *eventData, IOEventDataType dataType) override;

Module* getModule(const char* domain, const char* address) override;
LinkedList<Module*>* getModuleList() override;
};

}}
#endif //HOMEGENIE_MINI_SHUTTERHANDLER_H
Empty file.
43 changes: 43 additions & 0 deletions examples/shutter/io/IShutterDriver.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* HomeGenie-Mini (c) 2018-2024 G-Labs
*
*
* This file is part of HomeGenie-Mini (HGM).
*
* HomeGenie-Mini is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HomeGenie-Mini is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HomeGenie-Mini. If not, see <http://www.gnu.org/licenses/>.
*
*
* Authors:
* - Generoso Martello <[email protected]>
*
*
* Releases:
* - 2024-01-06 Initial release
*
*/

#ifndef HOMEGENIE_MINI_ISHUTTERDRIVER_H
#define HOMEGENIE_MINI_ISHUTTERDRIVER_H

namespace IO { namespace Components {
class IShutterDriver {
public:
virtual void init() = 0;
virtual void stop() = 0;
virtual void open() = 0;
virtual void close() = 0;
};
}}

#endif //HOMEGENIE_MINI_ISHUTTERDRIVER_H
Loading

0 comments on commit f016857

Please sign in to comment.