ESP + Reorganizacja
This commit is contained in:
93
ESP32/UPS_BatteryLevel/DEV_Sensors.h
Normal file
93
ESP32/UPS_BatteryLevel/DEV_Sensors.h
Normal file
@@ -0,0 +1,93 @@
|
||||
|
||||
////////////////////////////////////
|
||||
// DEVICE-SPECIFIC LED SERVICES //
|
||||
////////////////////////////////////
|
||||
|
||||
struct DEV_TempSensor : Service::TemperatureSensor { // A standalone Temperature sensor
|
||||
|
||||
SpanCharacteristic *temp; // reference to the Current Temperature Characteristic
|
||||
|
||||
DEV_TempSensor() : Service::TemperatureSensor(){ // constructor() method
|
||||
|
||||
// First we instantiate the main Characteristic for a Temperature Sensor, namely the Current Temperature, and set its initial value
|
||||
// to 20 degrees. For a real sensor, we would take a reading and initialize it to that value instead. NOTE: HomeKit uses
|
||||
// Celsius for all temperature settings. HomeKit will DISPLAY temperatures in the HomeKit app according to the settings on your iPhone.
|
||||
// Though the HAP documentation includes a Characteristic that appears to allow the device to over-ride this setting by specifying a display
|
||||
// of Celsius or Fahrenheit for each Service, it does not appear to work as advertised.
|
||||
|
||||
temp=new Characteristic::CurrentTemperature(-10.0); // instantiate the Current Temperature Characteristic
|
||||
temp->setRange(-50,100); // expand the range from the HAP default of 0-100 to -50 to 100 to allow for negative temperatures
|
||||
|
||||
Serial.print("Configuring Temperature Sensor"); // initialization message
|
||||
Serial.print("\n");
|
||||
|
||||
} // end constructor
|
||||
|
||||
// Next we create the loop() method. This method take no arguments and returns no values. In order to simulate a temperature change
|
||||
// from an actual sensor we will read the current value of the temp Characteristic using the getVal() function, with <float> as the
|
||||
// template parameter; add 0.5 degrees Celsius; and then store the result in a float variable named "temperature." This will simulate
|
||||
// an increment of 0.5 degrees Celsius (a little less than 1 degree F). We will cap the temperature to 35.0 degrees C, after which
|
||||
// it resets to 10.0 and starts over. Most importantly, we will do this once every 5 seconds by checking the elapsed time since the
|
||||
// previous modification using timeVal().
|
||||
|
||||
// All of the action happens in the setVal() line where we set the value of the temp Characteristic to the new value of temperature.
|
||||
// This tells HomeKit to send an Event Notification message to all available Controllers making them aware of the new temperature.
|
||||
// Note that setVal() is NOT a template function and does not require you to specify <float> as a template parameter. This is because
|
||||
// setVal() can determine the type from the argument you specify. If there is any chance of ambiguity, you can always specifically
|
||||
// cast the argument such: setVal((float)temperature).
|
||||
|
||||
void loop(){
|
||||
|
||||
if(temp->timeVal()>5000){ // check time elapsed since last update and proceed only if greater than 5 seconds
|
||||
float temperature=temp->getVal<float>()+0.5; // "simulate" a half-degree temperature change...
|
||||
if(temperature>35.0) // ...but cap the maximum at 35C before starting over at -30C
|
||||
temperature=-30.0;
|
||||
|
||||
temp->setVal(temperature); // set the new temperature; this generates an Event Notification and also resets the elapsed time
|
||||
|
||||
LOG1("Temperature Update: ");
|
||||
LOG1(temperature*9/5+32);
|
||||
LOG1("\n");
|
||||
}
|
||||
|
||||
} // loop
|
||||
|
||||
};
|
||||
|
||||
//////////////////////////////////
|
||||
|
||||
struct DEV_Voltmeter : Service::BatteryService { // UPS battery voltmeter
|
||||
|
||||
SpanCharacteristic *BatLvl; // reference to the Battery Level, which is a int from 0 to 100
|
||||
SpanCharacteristic *LowBat; // reference to the Low Battery Status, which is a int 0 or 1
|
||||
DEV_Voltmeter() : Service::BatteryService(){ // constructor() method
|
||||
|
||||
BatLvl=new Characteristic::BatteryLevel (77); // instantiate the Battery Level and set initial value to 77
|
||||
LowBat=new Characteristic::StatusLowBattery (0); // instantiate the Low Battery Status and set initial value to 0
|
||||
|
||||
|
||||
Serial.print("Configuring UPS Bttery Level"); // initialization message
|
||||
Serial.print("\n");
|
||||
|
||||
} // end constructor
|
||||
|
||||
void loop(){
|
||||
|
||||
// Note we are NOT updating the Nitrogen Dioxide Density Characteristic. This should therefore remain steady at its initial value of 700.0
|
||||
|
||||
if(BatLvl->timeVal()>5000){ // check time elapsed since last update and proceed only if greater than 5 seconds
|
||||
int batteryLevel=BatLvl->getVal<int>()-1; // "simulate" a half-degree temperature change...
|
||||
if(batteryLevel<10) // ...but cap the maximum at 35C before starting over at -30C
|
||||
batteryLevel=85;
|
||||
|
||||
BatLvl->setVal(batteryLevel); // set the new temperature; this generates an Event Notification and also resets the elapsed time
|
||||
|
||||
LOG1("Battery Lvl Update: ");
|
||||
LOG1(batteryLevel);
|
||||
LOG1("\n");
|
||||
}
|
||||
|
||||
} // loop
|
||||
|
||||
};
|
||||
|
||||
118
ESP32/UPS_BatteryLevel/UPS_BatteryLevel.ino
Normal file
118
ESP32/UPS_BatteryLevel/UPS_BatteryLevel.ino
Normal file
@@ -0,0 +1,118 @@
|
||||
/*********************************************************************************
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020-2024 Gregg E. Berman
|
||||
*
|
||||
* https://github.com/HomeSpan/HomeSpan
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*
|
||||
********************************************************************************/
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
// //
|
||||
// HomeSpan: A HomeKit implementation for the ESP32 //
|
||||
// ------------------------------------------------ //
|
||||
// //
|
||||
// Example 12: Service Loops (and Event Notifications) //
|
||||
// * implementing a Temperature Sensor //
|
||||
// * implementing an Air Quality Sensor //
|
||||
// //
|
||||
////////////////////////////////////////////////////////////
|
||||
|
||||
#include "HomeSpan.h"
|
||||
#include "DEV_Sensors.h"
|
||||
|
||||
void setup() {
|
||||
|
||||
// So far we've seen that HomeSpan allows you to create derived Services with their own constructors and update() methods. For many applications, this
|
||||
// will be all that is needed. However, for many other types of applications you may need to take action or perform some background operations without
|
||||
// any prompting or requests from HomeKit.
|
||||
|
||||
// To perform background operations and actions, every Service implements a loop() method. The default loop() method is to do nothing, which has been
|
||||
// fine for all our prior examples. But if you need to perform some continuous background action, all you need to do is implement a loop() method for
|
||||
// your derived Service. At the end of each HomeSpan polling cycle, the loop() method is called for each Service that implements its own code.
|
||||
// In this fashion, the loop() method is similar to the main loop() method in the Arduino IDE itself - except it can be customized for each Service.
|
||||
|
||||
// In this Example 12 we explore the use of loop() methods to implement two new accessories - a Temperature Sensor and an Air Quality Sensor. Of course
|
||||
// we won't actually have these physical devices attached to the ESP32 for the purpose of this example, but we will simulate "reading" their properties.
|
||||
// This is one of the main purposes of implementing a loop() method. It allows you to read a sensor or perform some sort of repetitive, Service-specific
|
||||
// action.
|
||||
|
||||
// Once you read (or simulate reading) a sensor's values in a loop() method, you need to somehow communicate this back to HomeKit so the new values can be
|
||||
// reflected in the HomeKit Controller. This may be strictly for information purposes (such as a temperature sensor) or could be used by HomeKit itself
|
||||
// to trigger other devices (as might occur if implementing a Door Sensor).
|
||||
|
||||
// Fortunately, HomeSpan makes communicating the values of Characteristics back to HomeKit easy. In prior examples we saw how getVal() and getNewVal()
|
||||
// are used to read current and updated Characteristic values requested by HomeKit. To perform the reverse, we simply use a method called setVal().
|
||||
// Setting the value of a Characteristic with this function does two things. First, it causes HomeSpan to send an Event Notification message back to HomeKit
|
||||
// letting HomeKit know the new value of the Characteristic. Since messages create network traffic, HomeSpan keeps track of all setVal() changes across
|
||||
// all Services and creates one a single Event Notification message reporting all the changes togther at the end of each polling cycle.
|
||||
|
||||
// The second thing that HomeSpan does when you change the value of a Characteristic with setVal() is to reset an internal timer for that Characteristic that
|
||||
// keeps track of how long it's been since the last modification, whether from a previous setVal() instruction, or by HomeKit itself via a call to update().
|
||||
// You can query the time since the last modificaton using the method timeVal() which returns the elapsed time in milliseconds. By calling this function from
|
||||
// within loop() you can determine when it's time for a new sensor read, or when to perform some other action.
|
||||
|
||||
// NOTE: It it NOT recommended to continuously change Characteristic values using setVal() as this will generate a lot of network traffic since HomeSpan
|
||||
// sends Event Notifications bck to all registered HomeKit Controllers. It's fine to perform internal calculations, generate signals on different pins,
|
||||
// and perform any other internal actions you may need as frequently as you require. But limit the use of setVal() to a reasonable frequency, such as maybe
|
||||
// one per minute for a temperature sensor. Do not use setVal() unless the value of the Characteristic changes, but do use it to immediately inform HomeKit of
|
||||
// something time-sensitive, such as a door opening, or a smoke alarm triggering.
|
||||
|
||||
// As usual, all of the logic for this example are encapsulated in new standalone derived Services. You'll find fully-commented definitions for the DEV_TempSensor() and
|
||||
// the DEV_AirQualitySensor() Services instantiated below, in the DEV_Sensors.h file. As noted, this example is for instructional purposes only -- we do not actually
|
||||
// connect a Temperature Sensor or Air Quality Sensor to our ESP32 device. As such, we did not define the Services to take any arguments to specify pin numbers or any
|
||||
// other information needed to implement an actual sensor. Instead, in order to see how real a device would work, we simulate periodic changes by modifying Characteristic
|
||||
// values using setVal() with either a sequence of repeating values, or random values. See DEV_Sensors.h for complete details.
|
||||
|
||||
// Once you understand these examples, you should be able to use implement your own loop() method and utilize setVal() along with timeVal() for any combination of
|
||||
// HomeKit Services with Characteristics that require your device to send periodic update messages to HomeKit Controllers, ranging from Smoke Alarms to Door Sensors.
|
||||
|
||||
Serial.begin(115200);
|
||||
homeSpan.setPairingCode("11122333");
|
||||
homeSpan.setQRID("111-22-333");
|
||||
homeSpan.begin(Category::Bridges,"HomeSpan Bridge");
|
||||
|
||||
new SpanAccessory();
|
||||
new Service::AccessoryInformation();
|
||||
new Characteristic::Identify();
|
||||
|
||||
new SpanAccessory();
|
||||
new Service::AccessoryInformation();
|
||||
new Characteristic::Identify();
|
||||
new Characteristic::Name("Temp Sensor");
|
||||
new DEV_TempSensor(); // Create a Temperature Sensor (see DEV_Sensors.h for definition)
|
||||
|
||||
new SpanAccessory();
|
||||
new Service::AccessoryInformation();
|
||||
new Characteristic::Identify();
|
||||
new Characteristic::Name("Battery Level");
|
||||
new DEV_Voltmeter();
|
||||
} // end of setup()
|
||||
|
||||
//////////////////////////////////////
|
||||
|
||||
void loop(){
|
||||
|
||||
homeSpan.poll();
|
||||
|
||||
} // end of loop()
|
||||
|
||||
//////////////////////////////////////
|
||||
Reference in New Issue
Block a user