From 5d88bf4bf845f179aff812e3be66261a74e404fd Mon Sep 17 00:00:00 2001 From: Finn_Dane Date: Tue, 14 Nov 2023 15:32:16 +0100 Subject: [PATCH] adds check if sensor is still on bus --- arduino/src/Sensor.cpp | 12 ++++++++---- arduino/src/Sensor.hpp | 4 ++-- arduino/src/main.cpp | 10 +++++----- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/arduino/src/Sensor.cpp b/arduino/src/Sensor.cpp index 362c936..1ce58f2 100644 --- a/arduino/src/Sensor.cpp +++ b/arduino/src/Sensor.cpp @@ -13,17 +13,21 @@ Sensor::Sensor(int id, float resolution, float accuracy, DallasTemperature &bus, }; float Sensor::read() const { + if(!bus.isConnected(address)) return NAN; + bus.requestTemperaturesByAddress(address); return bus.getTempC(address); } -uint16_t Sensor::makeDataPacket(byte (&buffer)[dataPacketMaxSize]) const { +bool Sensor::makeDataPacket(byte (&buffer)[dataPacketSize]) const { + float readVal = read(); + if(isnan(readVal)) return false; + size_t written = 0; - written += writeType(&buffer[written], id); - written += writeType(&buffer[written], read()); + written += writeType(&buffer[written], readVal); - return written + 1; + return true; } void Sensor::registerSensor(byte (&buffer)[serializedSize]) const { diff --git a/arduino/src/Sensor.hpp b/arduino/src/Sensor.hpp index a7efec5..d93885d 100644 --- a/arduino/src/Sensor.hpp +++ b/arduino/src/Sensor.hpp @@ -15,7 +15,7 @@ class Sensor { public: static constexpr uint16_t serializedSize = sizeof(id) + sizeof(resolution) + sizeof(accuracy); - static constexpr uint16_t dataPacketMaxSize = 256; + static constexpr uint16_t dataPacketSize = sizeof(id) + sizeof(float); Sensor(int id, float resolution, float accuracy, DallasTemperature &bus, DeviceAddress address); @@ -24,7 +24,7 @@ class Sensor { uint16_t getId() const {return id;}; - uint16_t makeDataPacket(byte (&buffer)[dataPacketMaxSize]) const; + bool makeDataPacket(byte (&buffer)[dataPacketSize]) const; void registerSensor(byte (&buffer)[serializedSize]) const; }; diff --git a/arduino/src/main.cpp b/arduino/src/main.cpp index 18cd770..14ea399 100644 --- a/arduino/src/main.cpp +++ b/arduino/src/main.cpp @@ -40,15 +40,15 @@ int main() { packetSerial.send(buff, sizeof(buff)); } - byte dataBuff[Sensor::dataPacketMaxSize + 1]; + byte dataBuff[Sensor::dataPacketSize + 1]; dataBuff[0] = (byte)PacketType::data; - uint16_t written; while(true) { for(int i = 0; i < sensors.size(); ++i) { - written = sensors.get(i)->makeDataPacket((byte(&)[Sensor::dataPacketMaxSize])dataBuff[1]); - packetSerial.send(dataBuff, written); + if(sensors.get(i)->makeDataPacket((byte(&)[Sensor::dataPacketSize])dataBuff[1])) { + packetSerial.send(dataBuff, sizeof(dataBuff)); + } } - delay(500); + delay(5000); } for(int i = 0; i < sensors.size(); ++i) {