diff --git a/Other Examples/RemoteSensors/MainDevice/MainDevice.ino b/Other Examples/RemoteSensors/MainDevice/MainDevice.ino new file mode 100644 index 0000000..270f4e4 --- /dev/null +++ b/Other Examples/RemoteSensors/MainDevice/MainDevice.ino @@ -0,0 +1,121 @@ +/********************************************************************************* + * MIT License + * + * Copyright (c) 2020-2022 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 // +// ------------------------------------------------ // +// // +// Demonstrates how to use SpanPoint() to implement // +// two remote temperature sensors on separate ESP32 // +// devices. // +// // +// This sketch is for the MAIN DEVICE that contains // +// all the usual HomeSpan logic, plus two instances // +// of SpanPoint to read temperatures from two other // +// remote devices. // +// // +//////////////////////////////////////////////////////////// + +#include "HomeSpan.h" + +struct RemoteTempSensor : Service::TemperatureSensor { + + SpanCharacteristic *temp; + SpanCharacteristic *fault; + SpanPoint *remoteTemp; + const char *name; + float temperature; + + RemoteTempSensor(const char *name, const char*macAddress) : Service::TemperatureSensor(){ + + this->name=name; + + temp=new Characteristic::CurrentTemperature(-10.0); // set initial temperature + temp->setRange(-50,100); // expand temperature range to allow negative values + + fault=new Characteristic::StatusFault(1); // set initial state = fault + + remoteTemp=new SpanPoint(macAddress,0,sizeof(float)); // create a SpanPoint with send size=0 and receive size=sizeof(float) + + } // end constructor + + void loop(){ + + if(remoteTemp->get(&temperature)){ // if there is data from the remote sensor + temp->setVal(temperature); // update temperature + fault->setVal(0); // clear fault + + LOG1("Sensor %s update: Temperature=%0.2f\n",name,temperature*9/5+32); + + } else if(remoteTemp->time()>60000 && !fault->getVal()){ // else if it has been a while since last update (60 seconds), and there is no current fault + fault->setVal(1); // set fault state + LOG1("Sensor %s update: FAULT\n",name); + } + + } // loop + +}; + +////////////////////////////////////// + +void setup() { + + Serial.begin(115200); + + homeSpan.setLogLevel(1); + + homeSpan.begin(Category::Bridges,"Sensor Hub"); + + new SpanAccessory(); + new Service::AccessoryInformation(); + new Characteristic::Identify(); + + new SpanAccessory(); + new Service::AccessoryInformation(); + new Characteristic::Identify(); + new Characteristic::Name("Indoor Temp"); + new RemoteTempSensor("Device 1","AC:67:B2:77:42:20"); // pass MAC Address of Remote Device + + new SpanAccessory(); + new Service::AccessoryInformation(); + new Characteristic::Identify(); + new Characteristic::Name("Outdoor Temp"); + new RemoteTempSensor("Device 2","84:CC:A8:11:B4:84"); // pass MAC Address of Remote Device + + +} // end of setup() + +////////////////////////////////////// + +void loop(){ + + homeSpan.poll(); + +} // end of loop() + +////////////////////////////////////// diff --git a/Other Examples/RemoteSensors/RemoteDevice/RemoteDevice.ino b/Other Examples/RemoteSensors/RemoteDevice/RemoteDevice.ino new file mode 100644 index 0000000..04b9dca --- /dev/null +++ b/Other Examples/RemoteSensors/RemoteDevice/RemoteDevice.ino @@ -0,0 +1,78 @@ + +/********************************************************************************* + * MIT License + * + * Copyright (c) 2020-2022 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 // +// ------------------------------------------------ // +// // +// Demonstrates how to use SpanPoint() to implement // +// two remote temperature sensors on separate ESP32 // +// devices. // +// // +// This sketch is for the REMOTE DEVICES. They are // +// very simple and don't need any of the normal // +// HomeSpan logic (except for SpanPoint). // +// // +// Note this sketch only SIMULATES a temperature // +// sensor by slowly setting the temperature from // +// -30.0 to 35.0 C in steps of 0.5 C. The sketch // +// does not contain logic for an actual physical // +// temperature sensor. // +// // +//////////////////////////////////////////////////////////// + +#include "HomeSpan.h" + +float temperature=-10.0; +SpanPoint *mainDevice; + +void setup() { + + Serial.begin(115200); + delay(1000); + + Serial.printf("Starting\n\n"); + + // In the line below, replace the MAC Address with that of your MAIN HOMESPAN DEVICE + + mainDevice=new SpanPoint("7C:DF:A1:61:E4:A8",sizeof(float),0); // create a SpanPoint with send size=sizeof(float) and receive size=0 + + homeSpan.setLogLevel(1); +} + +void loop() { + + boolean success = mainDevice->send(&temperature); // this will show as success as long as the MAIN DEVICE is running + Serial.printf("Send %s\n",success?"Succeded":"Failed"); + temperature+=0.5; + if(temperature>35.0) + temperature=-30.0; + + delay(20000); +} diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index af2f89b..56660db 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -2324,6 +2324,7 @@ void SpanPoint::dataReceived(const uint8_t *mac, const uint8_t *incomingData, in return; } + (*it)->receiveTime=millis(); // set time of receive xQueueSend((*it)->receiveQueue, incomingData, 0); // send to queue - do not wait if queue is full and instead fail immediately since we need to return from this function ASAP } diff --git a/src/HomeSpan.h b/src/HomeSpan.h index 6fd59be..3312d42 100644 --- a/src/HomeSpan.h +++ b/src/HomeSpan.h @@ -760,6 +760,7 @@ class SpanPoint { int sendSize; // size (in bytes) of messages to send esp_now_peer_info_t peerInfo; // structure for all ESP-NOW peer data QueueHandle_t receiveQueue; // queue to store data after it is received + uint32_t receiveTime=0; // time (in millis) of most recent data received static uint8_t lmk[16]; static boolean initialized; @@ -783,7 +784,8 @@ class SpanPoint { static void setPassword(const char *pwd){init(pwd);}; static void setChannelMask(uint16_t mask); boolean get(void *dataBuf); - boolean send(void *data); + boolean send(void *data); + uint32_t time(){return(millis()-receiveTime);} }; /////////////////////////////////////////////////