1- /* **********************************************************************************************************
2- mois Yun Beescale
3- Collect sensor data, send it via GSM to local SD, flatfile on webserver, and Hiveeyes' Kotori/InfluxDB.
4-
5- (c)2014-2017 Markus Euskirchen
6- https://www.euse.de/wp/blog/series/bienenwaage2/
7- https://github.com/bee-mois/beescale
8-
9- Changes
10- -------
11- 2014-04-04 Initial version
12- 2015-01-12 Added second scale
13- 2015-03-14 Modularized code into functions, code cleanup, thanks Alexander!
14- 2016-10-30 Change from Wifi to ethernet
15- 2016-11-15 Median instead average
16- 2017-02-26 Change from Ethernet-Shield to Yun-Shield
17- 2017-03-06 Added DHT22
18- 2017-03-09 Added switch, SD for local backup
19- 2017-03-12 Added TSL2591 digital light sensor
20-
21- GNU GPL v3 License
22- ------------------
23- This program is free software; you can redistribute it and/or modify
24- it under the terms of the GNU General Public License as published by
25- the Free Software Foundation; either version 3 of the License, or
26- (at your option) any later version.
27- This program is distributed in the hope that it will be useful,
28- but WITHOUT ANY WARRANTY; without even the implied warranty of
29- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30- GNU General Public License for more details.
31- You should have received a copy of the GNU General Public License
32- along with this program; if not, see:
33- <http://www.gnu.org/licenses/gpl-3.0.txt>,
34- or write to the Free Software Foundation,
35- Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
36- -------------------------------------------------------------------------
37-
38- Credits: Alexander Wilms for a first modularized/functionized loop.
39- Hiveeyes Developers for pushing me forward.
40-
41- Used libraries:
42- Bridge: Yun Standard
43- Console: Yun Standard
44- HttpClient: Yun Standard
45- FileIO: Yun Standard
46- ADS1231: http://forum.arduino.cc/index.php?action=dlattach;topic=131086.0;attach=67564
47- maybe better (?): https://github.com/rfjakob/barwin-arduino/tree/master/lib/ads1231
48- RunningMedian: https://github.com/RobTillaart/Arduino/tree/master/libraries/RunningMedian
49- SPI: Arduino Standard
50- digitalWriteFast: https://github.com/watterott/Arduino-Libs/tree/master/digitalWriteFast
51- OneWire: https://github.com/PaulStoffregen/OneWire
52- DallasTemperature: https://github.com/milesburton/Arduino-Temperature-Control-Library
53- DHT: https://github.com/adafruit/DHT-sensor-library
54- Wire: Arduino Standard
55- Adafruit_Sensor: https://github.com/adafruit/Adafruit_Sensor
56- Adafruit_TSL2591: https://github.com/adafruit/Adafruit_TSL2591_Library
57- **********************************************************************************************************/
1+ /* *
2+
3+ Mois Labs Beescale Yun - Firmware source code
4+ Collect sensor data, send it via GSM to local SD, flat-file on webserver, and to Hiveeyes' Kotori/InfluxDB/Grafana.
5+
6+ https://hiveeyes.org/docs/arduino/firmware/moislabs/beescale-yun/README.html
7+
8+ (c) 2011-2023 Markus Euskirchen
9+ License: Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License
10+ Lizenz: Creative Commons Namensnennung - Nicht-kommerziell - Weitergabe unter gleichen Bedingungen 4.0 International Lizenz
11+
12+ Source code repository and documentation:
13+ - https://github.com/bee-mois/beescale
14+ - https://github.com/hiveeyes/arduino/tree/main/moislabs/beescale-yun
15+ - https://www.euse.de/wp/blog/series/bienenwaage2/
16+
17+ Changelog:
18+ 2014-04-04 mois - Initial version
19+ 2015-01-12 mois - Added second scale
20+ 2015-03-14 Alexander Wilms -
21+ Modularized code into functions, adjusted loop order to avoid program hangs in case of
22+ client/network/transmit errors. Code cleanup.
23+ 2016-10-30 mois - Verbindung umgestellt auf Ethernet hinter OpenWRT-Router
24+ 2016-11-15 mois - Zeile 116: Median statt Average
25+ 2017-02-20 mois - zweite Wägezelle wieder eingebaut
26+ 2017-02-26 mois - für Yun: Bridgeverwendung aktiviert, Kontroll-LED eingebaut, GET Request
27+ 2017-02-27 mois - Serverantwort auf die Console leiten
28+ 2017-03-06 mois - DHT22 eingebaut
29+ 2017-03-09 mois - Schalter; SD-Backup eingebaut
30+ 2017-03-12 mois - TSL2591 eingebaut; überflüssige Kommentare und Optionen gelöscht.
31+ 2017-03-14 mois - TSL2591 optimiert: simple read!
32+ 2019-06-16 mois - added code to swich on/off USB-power by a relay on pinA0/D14
33+ 2020-10-05 mois - added code to check t2 and correct via relay if necessary.
34+ - commented out other relay stuff.
35+ 2022-03-15 mois - replaced DHT22 by SHT30.
36+ - therefore had to add an i2c multiplexer TCA9548A.
37+ - wrapped TSL2591 stuff in multiplexer code.
38+ - removed relay which had to reset one of the dht22.
39+ */
5840
5941// define individual values for used load cell type
6042// loadCellZeroOffset: write down the sensor value of the scale with no load and adjust it
@@ -77,10 +59,10 @@ long loadCellKgDivider01 = 3800000;
7759// initialize all needed variables
7860float temp1; // Temp outside box
7961float temp2; // Temp in box
80- float h1; // DHT Humidity Hive1
81- float t1; // DHT Temperature Hive1
82- float h2; // DHT Humidity Hive2
83- float t2; // DHT Temperature Hive2
62+ float h1; // SHT30 Humidity Hive1, connect sensor pins to I2C bus number 1 on multiplexer
63+ float t1; // SHT30 Temperature Hive1, -"-
64+ float h2; // SHT30 Humidity Hive2, connect sensor pins to I2C bus number 2 on multiplexer
65+ float t2; // SHT30 Temperature Hive2, -"-
8466float weight1;
8567long weightSensorValue;
8668float weightKg;
@@ -92,12 +74,13 @@ boolean weightKgTrusted01 = false;
9274static char cKg00[8 ];
9375static char cKg01[8 ];
9476String dataURL = " " ;
95- int switchPin = 11 ; // switch is connected to pin 11
96- uint16_t lux = 0 ;
77+ int switchPin = 11 ; // switch is connected to pin 11
78+ uint16_t lux; // connect sensor pins to I2C bus number 4 on multiplexer
79+ uint8_t bus;
9780
9881#include < ADS1231.h>
9982
100- ADS1231 loadCell; // create ADS1231 object
83+ ADS1231 loadCell; // create ADS1231 object
10184ADS1231 loadCell01;
10285
10386#include < RunningMedian.h>
@@ -117,23 +100,37 @@ OneWire OneWire(ONE_WIRE_BUS);
117100// Pass our OneWire reference to Dallas Temperature.
118101DallasTemperature sensors (&OneWire);
119102
120- #include " DHT.h"
121-
122- #define DHT1PIN 2
123- #define DHT2PIN 3
124- #define DHT1TYPE DHT22
125- #define DHT2TYPE DHT22
126- DHT dht1 (DHT1PIN, DHT1TYPE);
127- DHT dht2 (DHT2PIN, DHT2TYPE);
128-
129103#include < Wire.h>
130104#include < Adafruit_Sensor.h>
131105#include " Adafruit_TSL2591.h"
132106
133- Adafruit_TSL2591 tsl = Adafruit_TSL2591(2591 ); // pass in a number for the sensor identifier
107+ Adafruit_TSL2591 tsl = Adafruit_TSL2591(2591 ); // pins to I2C bus number 4
134108
135- // Forward declarations
136- String getTimeStamp ();
109+ #include " Adafruit_SHT31.h"
110+
111+ bool enableHeater = false ;
112+ Adafruit_SHT31 sht1 = Adafruit_SHT31(); // connect sensor pins to I2C bus number 1
113+ Adafruit_SHT31 sht2 = Adafruit_SHT31(); // connect sensor pins to I2C bus number 2
114+
115+ // Function to select I2C bus on multiplexer TCA9548A
116+ void tcaselect (uint8_t bus) {
117+ if (bus > 7 ) return ;
118+ Wire.beginTransmission (0x70 ); // TCA9548A multiplexer address
119+ Wire.write (1 << bus); // send byte to select bus
120+ Wire.endTransmission ();
121+ }
122+
123+ // Function to read temp&humidity from SHT31 sensors
124+ void readValues (Adafruit_SHT31 sht31, int bus) {
125+ tcaselect (bus);
126+ if (bus == 1 ) {
127+ h1 = sht31.readHumidity () + 0.4 ; /* +x% korrektur */
128+ t1 = sht31.readTemperature ();
129+ } else if (bus == 2 ) {
130+ h2 = sht31.readHumidity () - 0.4 ; /* -x% korrektur */
131+ t2 = sht31.readTemperature ();
132+ }
133+ }
137134
138135// Function to read out weight cell.
139136void getWeight (void ) {
@@ -161,9 +158,9 @@ void getWeight(void) {
161158 }
162159}
163160
164- void configureSensor ( void ) // Configures the gain and integration time for the TSL2591
165- {
166- tsl.setGain (TSL2591_GAIN_LOW); // 1x gain (bright light)
161+ // Function for TSL2591 to configure gain and integration time
162+ void configureSensor ( void ) {
163+ tsl.setGain (TSL2591_GAIN_LOW); // 1x gain (bright light)
167164 // tsl.setGain(TSL2591_GAIN_MED); // 25x gain
168165 // tsl.setGain(TSL2591_GAIN_HIGH); // 428x gain
169166 // tsl.setTiming(TSL2591_INTEGRATIONTIME_100MS); // shortest integration time (bright light)
@@ -172,26 +169,29 @@ void configureSensor(void) // Configures the gain and integration time for th
172169 // tsl.setTiming(TSL2591_INTEGRATIONTIME_400MS);
173170 // tsl.setTiming(TSL2591_INTEGRATIONTIME_500MS);
174171 // tsl.setTiming(TSL2591_INTEGRATIONTIME_600MS); // longest integration time (dim light)
175- //
176172 // 25 gain/200ms ok for winter indoor. spring sun too bright.
177173 // 1 gain/300ms ok for sunny spring morning
178174}
179175
180- void simpleRead (void ) {
181- // Simple data read example. Just read the infrared, fullspecrtrum diode
182- // or 'visible' (difference between the two) channels.
183- // This can take 100-600 milliseconds! Uncomment whichever of the following you want to read
184- lux = tsl.getLuminosity (TSL2591_VISIBLE);
185- // uint16_t x = tsl.getLuminosity(TSL2591_FULLSPECTRUM);
186- // uint16_t x = tsl.getLuminosity(TSL2591_INFRARED);
176+ // Function to read from TSL2591 brightness sensor
177+ void TSLRead (void ) {
178+ tcaselect (4 );
179+ uint32_t lum = tsl.getFullLuminosity ();
180+ uint16_t ir, full;
181+ ir = lum >> 16 ;
182+ full = lum & 0xFFFF ;
183+ lux = tsl.calculateLux (full, ir);
184+ // lux = tsl.getLuminosity(TSL2591_VISIBLE);
185+ // Serial.print("Visible: "); Serial.print(full - ir);
187186}
188187
189188// Function to send values via GET request.
190189void add_line () {
191190 HttpClient client;
192191 // convert the readings to a String to send it:
193192 dataURL = " http://www.euse.de/honig/beescale/add_line2.php?weight1=" ;
194- dataURL += cKg00;
193+ // dataURL += cKg00;
194+ dataURL += " 17.075" ; // fixwert wegen bloeder sonderzeichen
195195 dataURL += " &weight2=" ;
196196 dataURL += cKg01;
197197 dataURL += " &temp1=" ;
@@ -235,14 +235,15 @@ void add_line() {
235235 // send data
236236 client.get (dataURL);
237237 Console.println (" sending data..." );
238- // if there's incoming data from the net connection send it out the console.
238+ // if there's incoming data from the net connection, send it out the console.
239239 while (client.available ()) {
240240 char c = client.read ();
241241 Console.print (c);
242242 }
243243 Console.flush ();
244244}
245245
246+ // Function to write backup to SD
246247void add_line_sd () {
247248 // make a string that starts with a timestamp for assembling the data to log:
248249 String dataString;
@@ -251,7 +252,7 @@ void add_line_sd() {
251252 dataString += cKg00;
252253 dataString += " ," ;
253254 dataString += temp1;
254- dataString += " ,0," ;
255+ dataString += " ,0," ; // ??
255256 dataString += lux;
256257 dataString += " ," ;
257258 dataString += cKg01;
@@ -274,6 +275,7 @@ void add_line_sd() {
274275 // print to the serial port too:
275276 Console.println (" OK." );
276277// Console.print("OK. File Size: "); Console.println(getLogSize());
278+ Console.println (" " );
277279 }
278280 // if the file isn't open, pop up an error:
279281 else {
@@ -299,31 +301,17 @@ String getTimeStamp() {
299301 return result;
300302}
301303
302- /*
303- String getLogSize()
304- {
305- String res;
306- Process size;
307- size.runShellCommand("du -h /mnt/sda1/arduino/www/datalog.txt | cut -f1");
308- while (size.running());
309- while (size.available() > 0) {
310- char c = size.read();
311- if (c != '\n') {
312- res += c;
313- }
314- }
315- return res;
316- }
317- */
318-
319304void setup () {
305+ Console.println (" 00" );
320306// delay(1000);
307+ Wire.begin ();
321308 Serial.begin (9600 ); // init serial port and set baudrate
322309 Bridge.begin ();
323310 Console.begin ();
324311 FileSystem.begin ();
325312 // a second to initialize:
326313 delay (1000 );
314+ // while (!Console); // Wait for the Console port to connect
327315 // load cell / ADS1231 pin definition: SCL 7, Data 6, PowerDown 10
328316 loadCell.attach (7 , 6 , 10 );
329317 // second load cell / ADS1231 pin definition: SCL 8, Data 5, PowerDown 9
@@ -332,33 +320,34 @@ void setup() {
332320 // Switch LED: initialize digital pin LED_BUILTIN as an output.
333321 pinMode (LED_BUILTIN, OUTPUT);
334322 pinMode (switchPin, INPUT); // Switch: Set the switch pin as input
335- dht1.begin ();
336- dht2.begin ();
337- tsl.begin ();
338- configureSensor (); // digital light sensor
323+ // Init sensors on multiplexers I2C bus
324+ tcaselect (1 );
325+ sht1.begin (0x44 );
326+ tcaselect (2 );
327+ sht2.begin (0x44 );
328+ tcaselect (4 );
329+ tsl.begin (0x29 );
330+ configureSensor ();
339331}
340332
341333void loop () {
342334 while (digitalRead (switchPin) == HIGH) {
343- digitalWrite (LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
335+ digitalWrite (LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
344336 Console.println (" paused by switch..." );
345337 delay (5000 );
346338 }
347- digitalWrite (LED_BUILTIN, LOW); // turn the LED off (LOW is the voltage level)
348- // delay(1000);
339+ digitalWrite (LED_BUILTIN, LOW); // turn the LED off (LOW is the voltage level)
340+ // delay(1000);
349341 // read and prepare sensor data
350- sensors.requestTemperatures (); // get temperatures
351- temp2 = sensors.getTempCByIndex (0 ); // Temp in box
352- temp1 = sensors.getTempCByIndex (1 ); // Temp outside box
353- simpleRead (); // Luminosity by TSL2591
354- h1 = dht1.readHumidity ();
355- t1 = dht1.readTemperature ();
356- h2 = dht2.readHumidity ();
357- t2 = dht2.readTemperature ();
358- getWeight ();
359- add_line (); // transmit data
360- add_line_sd (); // backup to Yun's MicroSD
361- Console.println ();
362- // delay(21000); //run every 30 seconds (runtime without delay: 9 seconds)
363- delay (111000 ); // run every two minutes (runtime without delay: 9 seconds)
342+ readValues (sht1, 1 ); // get SHT31 temperatures/humidity
343+ readValues (sht2, 2 );
344+ sensors.requestTemperatures (); // get DS18B20 temperatures
345+ temp2 = sensors.getTempCByIndex (0 ); // get temperature from inside sensor node box
346+ temp1 = sensors.getTempCByIndex (1 ); // temperature outside box
347+ TSLRead (); // Luminosity by TSL2591
348+ getWeight (); // Load cell
349+ add_line (); // transmit data
350+ add_line_sd (); // backup to Yun's MicroSD
351+ // delay(21000); //run every 30 seconds (runtime without delay: 9 seconds)
352+ delay (111000 ); // run every two minutes (runtime without delay: 9 seconds)
364353}
0 commit comments