Wetterstation_Boris/include/mcp9808.h
hans-jurgen 62ec975329 2023-05-19
SystemStatus geändert.
2023-05-19 14:15:31 +02:00

67 lines
2.2 KiB
C

#include <Arduino.h>
/* Daten:
* Temperaturbereich -40 bis 125°C
* Genauigkeit: ± 0,25°C
* Auflösung: 0,0625°C
*/
#include <Wire.h>
#include "Adafruit_MCP9808.h"
float valTemp;
// Create MCP9808 temperature sensor object
Adafruit_MCP9808 tempsensor = Adafruit_MCP9808();
bool F_MCP9808;
char Temperature[15] = {0};
void init_MCP9808(){
F_MCP9808 = true;
// Make sure the sensor is found, you can also pass in a different i2c
// address with tempsensor.begin(0x19) for example, also can be left in blank for default address use
// Also there is a table with all addres possible for this sensor, you can connect multiple sensors
// to the same i2c bus, just configure each sensor with a different address and define multiple objects for that
// A2 A1 A0 address
// 0 0 0 0x18 this is the default address
// 0 0 1 0x19
// 0 1 0 0x1A
// 0 1 1 0x1B
// 1 0 0 0x1C
// 1 0 1 0x1D
// 1 1 0 0x1E
// 1 1 1 0x1F
if (!tempsensor.begin(0x18)) {
Serial.println("Couldn't find MCP9808! Check your connections and verify the address is correct.");
SystemStatus = SystemStatus | MCP9808noReady;
F_MCP9808 = false;
}
Serial.println("Found MCP9808!");
tempsensor.setResolution(3); // sets the resolution mode of reading, the modes are defined in the table bellow:
// Mode Resolution SampleTime
// 0 0.5°C 30 ms
// 1 0.25°C 65 ms
// 2 0.125°C 130 ms
// 3 0.0625°C 250 ms
}
float getTemperature_MCP9808(){
// Wake up MSP9808 - power consumption ~200 mikro Ampere
tempsensor.wake();
float temperature = tempsensor.readTempC();
tempsensor.shutdown();
dtostrf(temperature,7,1,Temperature);
Serial.print("Temperatur (MCP9808):\t");
Serial.print(Temperature);
Serial.println(" °C");
tempsensor.shutdown_wake(1); // shutdown MSP9808 - power consumption ~0.1 mikro Ampere, stops temperature sampling
return temperature;
}
void M2M_Temperatur_MCP9808(String deviceId = "4711") {
char topic[100];
dtostrf(valTemp,7,1,Temperature);
sprintf(topic, "%s%s%s", "hjk/devices/", deviceId.c_str(), "/telemetry/temperature_MCP9808");
client.publish(topic, Temperature, true);
}