From 0d5d4a0ca0769b6d0e042f5e5bf07fef58d0907b Mon Sep 17 00:00:00 2001 From: Gregg Date: Sun, 2 Oct 2022 16:41:08 -0500 Subject: [PATCH] Added RemoteTempSensor to RemoteSensor Example --- .../RemoteSensors/MainDevice/MainDevice.ino | 2 +- .../RemoteTempSensor/RemoteTempSensor.ino | 108 ++++++++++++++++++ 2 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 Other Examples/RemoteSensors/RemoteTempSensor/RemoteTempSensor.ino diff --git a/Other Examples/RemoteSensors/MainDevice/MainDevice.ino b/Other Examples/RemoteSensors/MainDevice/MainDevice.ino index 270f4e4..3783ea2 100644 --- a/Other Examples/RemoteSensors/MainDevice/MainDevice.ino +++ b/Other Examples/RemoteSensors/MainDevice/MainDevice.ino @@ -72,7 +72,7 @@ struct RemoteTempSensor : Service::TemperatureSensor { 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 + } else if(remoteTemp->time()>120000 && !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); } diff --git a/Other Examples/RemoteSensors/RemoteTempSensor/RemoteTempSensor.ino b/Other Examples/RemoteSensors/RemoteTempSensor/RemoteTempSensor.ino new file mode 100644 index 0000000..65af908 --- /dev/null +++ b/Other Examples/RemoteSensors/RemoteTempSensor/RemoteTempSensor.ino @@ -0,0 +1,108 @@ + +/********************************************************************************* + * 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 // +// ------------------------------------------------ // +// // +// This sketch is for a Remote Temperature Sensor to // +// be used in conjunction with the "MainDevice.ino" // +// sketch running on a separate ESP32 // +// // +// The purpose of these sketches is to demonstrate // +// how to use SpanPoint() to communication between // +// a remote ESP32 device that takes measurements from // +// a sensor, and a separate "main" ESP32 device that // +// is running the full HomeSpan code, and thus // +// connects to HomeKit. // +// // +// This sketch implements an Adafruit ADT7410 I2C // +// Temperature Sensor. If you don't have such a // +// device, please use the sketch "RemoteDevice.ino" // +// instead. That sketch SIMULATES a temperature // +// sensor and therefore allows you to learn how // +// SpanPoint() works even though the temperature data // +// itself isn't real. // +// // +//////////////////////////////////////////////////////////// + +#include "HomeSpan.h" +#include // include the I2C library + +#define I2C_ADD 0x48 // ICS Address to use for the Adafruit ADT7410 + +SpanPoint *mainDevice; +uint32_t timer=0; // keep track of time since last update + +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 + + homeSpan.setLogLevel(1); + mainDevice=new SpanPoint("7C:DF:A1:61:E4:A8",sizeof(float),0); // create a SpanPoint with send size=sizeof(float) and receive size=0 + + Wire.begin(); // start I2C in Controller Mode + + Wire.beginTransmission(I2C_ADD); // setup transmission + Wire.write(0x0B); // ADT7410 Identification Register + Wire.endTransmission(0); // transmit and leave in restart mode to allow reading + Wire.requestFrom(I2C_ADD,1); // request read of single byte + uint8_t id = Wire.read(); // receive a byte + + Wire.beginTransmission(I2C_ADD); // setup transmission + Wire.write(0x03); // ADT740 Configuration Register + Wire.write(0xC0); // set 16-bit temperature resolution, 1 sampler per second + Wire.endTransmission(); // transmit + + LOG0("Configuring Temperature Sensor ADT7410 version 0x%02X with address 0x%02X.\n",id,I2C_ADD); // initialization message +} + +void loop() { + + if(millis()-timer>60000){ // only sample every 5 seconds + timer=millis(); + + Wire.beginTransmission(I2C_ADD); // setup transmission + Wire.write(0x00); // ADT7410 2-byte Temperature + Wire.endTransmission(0); // transmit and leave in restart mode to allow reading + Wire.requestFrom(I2C_ADD,2); // request read of two bytes + + int16_t iTemp = ((int16_t)Wire.read()<<8)+Wire.read(); + float temperature = iTemp/128.0; + + boolean success = mainDevice->send(&temperature); // send temperature to main device + LOG1("Send temp update of %0.2f C: %s\n",temperature,success?"Succeded":"Failed"); + } + } +