forked from rvdbreemen/OTGW-firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensors_ext.ino
177 lines (150 loc) · 5.43 KB
/
sensors_ext.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
** Program : output_ext.ino
** Version : v0.9.5
**
** Copyright (c) 2021-2022 Robert van den Breemen
** Contributed by Sjorsjuhmaniac
**
** TERMS OF USE: MIT License. See bottom of file.
**
** most code shamelessly copied from Miles Burton's - Arduino Dallas library
** example 'Multiple'
*/
#include <OneWire.h>
#include <DallasTemperature.h>
//prototype
char* getDallasAddress(DeviceAddress deviceAddress);
// GPIO where the DS18B20 is connected to
// Data wire is plugged TO GPIO 10
// #define ONE_WIRE_BUS 10
// Number of temperature devices found
int numberOfDevices;
// We'll use this variable to store a found device address
DeviceAddress tempDeviceAddress;
// Setup a oneWire instance to communicate with any OneWire devices
// needs a PIN to init correctly, pin is changed when we initSensors()
// this still may cause problems though because we this configures the pin already
// BUT, we're an OTGW, on a NODO print, so we are pretty sure nothing
// else is connected on these pins unlease somebody tweaked something.
// still don't like this too much:
OneWire oneWire(settingGPIOSENSORSpin);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
void initSensors() {
if (!settingGPIOSENSORSenabled) return;
DebugTf("init GPIO Sensors on GPIO%d...\r\n", settingGPIOSENSORSpin);
oneWire.begin(settingGPIOSENSORSpin);
// Start the DS18B20 sensor
sensors.begin();
// Grab a count of devices on the wire
numberOfDevices = sensors.getDeviceCount();
DebugTf("Found %d device(s)\r\n", numberOfDevices);
int realDeviceCount = 0;
// Loop through each device, print out address
for (int i = 0; i < numberOfDevices; i++)
{
// Search the wire for address
if (sensors.getAddress(tempDeviceAddress, i))
{
//TODO: get real device address, push data to mqtt topic.
DebugTf("Device address %u device(s)\r\n", (unsigned int) tempDeviceAddress);
DebugFlush();
realDeviceCount++;
}
else
{
DebugTf("Found ghost device %d but could not detect address. Check power and cabling\r\n", i);
}
}
if (numberOfDevices < 1 or realDeviceCount < 1)
{
DebugTln("No Sensors Found, disabled GPIO Sensors! Reboot node to search again.");
settingGPIOSENSORSenabled = false;
return;
}
}
int pollSensors()
{
if (!settingGPIOSENSORSenabled) return 1;
// Setup a oneWire instance to communicate with any OneWire devices
// oneWire.begin(settingGPIOSENSORSpin);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
if (numberOfDevices < 1)
{
DebugTln("No Sensors Found, please reboot the node to search for sensors");
return 1;
}
// DebugTln("start polling sensors");
sensors.requestTemperatures(); // Send the command to get temperatures
// Loop through each device, print out temperature data
for (int i = 0; i < numberOfDevices; i++)
{
// Search the wire for address
if (sensors.getAddress(tempDeviceAddress, i))
{
// Output the device ID
// Print the data
const char * strDeviceAddress = getDallasAddress(tempDeviceAddress);
float tempC = sensors.getTempC(tempDeviceAddress);
DebugTf("Device: %s, TempC: %f\r\n", strDeviceAddress, tempC);
//Build string for MQTT
char _msg[15]{0};
char _topic[50]{0};
snprintf(_topic, sizeof _topic, "otgw-firmware/sensors/%s", strDeviceAddress);
snprintf(_msg, sizeof _msg, "%f", tempC);
// DebugTf("Topic: %s -- Payload: %s\r\n", _topic, _msg);
DebugFlush();
sendMQTTData(_topic, _msg);
// Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
}
}
delay(100);
// DebugTln("end polling sensors");
DebugFlush();
return 0;
}
// function to print a device address
char* getDallasAddress(DeviceAddress deviceAddress)
{
// DebugTf("\r\n");
static char dest[10];
dest[0] = '\0';
for (uint8_t i = 0; i < 8; i++)
{
// zero pad the address if necessary
if (deviceAddress[i] < 16)
{
// Serial.print("0");
strcat(dest, "0");
}
// Serial.print(deviceAddress[i], HEX);
sprintf(dest+i, "%X", deviceAddress[i]);
// DebugTf("blah: %s\r\n", dest);
// DebugFlush();
}
return dest;
}
/***************************************************************************
*
* 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.
*
****************************************************************************
*/