📜 ⬆️ ⬇️

Another weather station, step-by-step video instruction

Recently, the topic of weather stations has become increasingly popular. Probably creating your own weather station is a good practice in the development of arduinka. I want to make a small contribution to this good thing.
I want to present you a video lesson on working with sensors of temperature, humidity, pressure, and outputting this data to the display. Perhaps this information will help someone start their way into the world of microcontrollers.

The station was going on this scheme.


Well, here's a lesson


Project created during the lesson process
')
Created sketch
#include <Wire.h> #include <LiquidCrystal_I2C.h> #include "DHT.h" #include <OneWire.h> #include "RTClib.h" #include <BMP085.h> BMP085 _bmp085 = BMP085(); long _bmp085P = 0; long _bmp085T = 0; long _bmp085A = 0; byte _d18x2x1Addr[8] = {0x28, 0xFF, 0x11, 0x94, 0x3C, 0x4, 0x0, 0x48}; RTC_DS1307 _RTC1; DateTime _tRTC1; LiquidCrystal_I2C _lcd1(0x3F, 20, 4); int _dispTempLength1 = 0; boolean _isNeedClearDisp1; DHT _dht1(2, DHT22); OneWire _ow3(3); String _gtv1; String _gtv2; String _gtv3; String _gtv4; String _gtv5; String _gtv6; bool _gtv7 = 0; int _gtv8 = 0; bool _gtv9 = 0; unsigned long _d18x2x1Tti = 0UL; float _d18x2x1O = 0.00; unsigned long _bmp0851Tti = 0UL; unsigned long _dht1Tti = 0UL; float _dht1t = 0.00; float _dht1h = 0.00; bool _count1I = 0; int _count1P = 0; bool _trgs1 = 0; bool _D1B1 = 0; bool _tim1I = 0; bool _tim1O = 0; unsigned long _tim1P = 0UL; int _disp1oldLength = 0; int _disp2oldLength = 0; String _mux1; int _disp3oldLength = 0; String _mux2; int _disp4oldLength = 0; String _mux3; int _disp5oldLength = 0; bool _bounseInput5S = 0; bool _bounseInput5O = 0; unsigned long _bounseInput5P = 0UL; bool _bounseInput4S = 0; bool _bounseInput4O = 0; unsigned long _bounseInput4P = 0UL; void setup() { Wire.begin(); _bmp085.init(MODE_ULTRA_HIGHRES, 300, true); _RTC1.begin(); pinMode(5, INPUT); digitalWrite(5, HIGH); pinMode(4, INPUT); digitalWrite(4, HIGH); _lcd1.init(); _lcd1.noBacklight(); _bounseInput5O = digitalRead(5); _bounseInput4O = digitalRead(4); _dht1.begin(); } void loop() { _tRTC1 = _RTC1.now(); if (_isTimer(_dht1Tti, 8000)) { _dht1Tti = millis(); float tempDht2; tempDht2 = _dht1.readTemperature(); if (!(isnan(tempDht2))) { _dht1t = tempDht2; } tempDht2 = _dht1.readHumidity(); if (!(isnan(tempDht2))) { _dht1h = tempDht2; } } if (_isNeedClearDisp1) { _lcd1.clear(); _isNeedClearDisp1 = 0; } if (_isTimer(_bmp0851Tti, 6000)) { _bmp0851Tti = millis(); _bmp085.getAltitude(&_bmp085A); _bmp085.getPressure(&_bmp085P); _bmp085.getTemperature(&_bmp085T); } bool _bounceTmp5 = (digitalRead (5)); if (_bounseInput5S) { if (millis() >= (_bounseInput5P + 40)) { _bounseInput5O = _bounceTmp5; _bounseInput5S = 0; } } else { if (_bounceTmp5 != _bounseInput5O ) { _bounseInput5S = 1; _bounseInput5P = millis(); } } bool _bounceTmp4 = (digitalRead (4)); if (_bounseInput4S) { if (millis() >= (_bounseInput4P + 40)) { _bounseInput4O = _bounceTmp4; _bounseInput4S = 0; } } else { if (_bounceTmp4 != _bounseInput4O ) { _bounseInput4S = 1; _bounseInput4P = millis(); } } if (_isTimer(_d18x2x1Tti, 5000)) { _d18x2x1Tti = millis(); _d18x2x1O = _readDS18_ow3(_d18x2x1Addr, 0); } _gtv1 = ((String("T-")) + (( _floatToStringWitRaz((_d18x2x1O), 2))) + (String("C"))); _gtv2 = ((String("T-")) + (( _floatToStringWitRaz((_bmp085T) / (10.00), 1))) + (String("C"))); _gtv3 = ((String("P-")) + ((String((_bmp085P) / (1000)))) + (String("kPa"))); _gtv4 = ((String("A")) + ((String(_bmp085A))) + (String("cm"))); _gtv5 = ((String("T-")) + (( _floatToStringWitRaz(_dht1t, 2))) + (String("C"))); _gtv6 = ((String("H-")) + (( _floatToStringWitRaz(_dht1h, 2))) + (String("%"))); if (!(_bounseInput5O)) { if (! _count1I) { _count1P = _count1P + 1; _count1I = 1; } } else { _count1I = 0; } if (_count1P < 0 ) _count1P = 0; if (_gtv7) _count1P = 0; _gtv7 = _count1P >= 3; _gtv8 = _count1P; if ( (!(_bounseInput4O)) || (!(_bounseInput5O)) ) _trgs1 = 1; if (_gtv9) _trgs1 = 0; if ( (_trgs1) && (_bounseInput5O) ) { if (_tim1I) { if ( _isTimer(_tim1P, 20000)) _tim1O = 1; } else { _tim1I = 1; _tim1P = millis(); } } else { _tim1O = 0; _tim1I = 0; } if (_trgs1) { if (! _D1B1) { _lcd1.backlight(); _D1B1 = 1; } } else { if (_D1B1) { _lcd1.noBacklight(); _D1B1 = 0; } } _gtv9 = _tim1O; if (1) { _dispTempLength1 = (((((String((_tRTC1.hour())))) + (String(":")) + ((String((_tRTC1.minute())))) + (String(":")) + ((String((_tRTC1.second()))))))).length(); if (_disp1oldLength > _dispTempLength1) { _isNeedClearDisp1 = 1; } _disp1oldLength = _dispTempLength1; _lcd1.setCursor(0, 0); _lcd1.print(((((String((_tRTC1.hour())))) + (String(":")) + ((String((_tRTC1.minute())))) + (String(":")) + ((String((_tRTC1.second()))))))); } else { if (_disp1oldLength > 0) { _isNeedClearDisp1 = 1; _disp1oldLength = 0; } } if (1) { _dispTempLength1 = (((((String((_tRTC1.day())))) + (String("-")) + ((String((_tRTC1.month())))) + (String("-")) + ((String((_tRTC1.year()))))))).length(); if (_disp2oldLength > _dispTempLength1) { _isNeedClearDisp1 = 1; } _disp2oldLength = _dispTempLength1; _lcd1.setCursor(9, 0); _lcd1.print(((((String((_tRTC1.day())))) + (String("-")) + ((String((_tRTC1.month())))) + (String("-")) + ((String((_tRTC1.year()))))))); } else { if (_disp2oldLength > 0) { _isNeedClearDisp1 = 1; _disp2oldLength = 0; } } if ((_gtv8) == 0) { _mux1 = String("DS18B2"); } if ((_gtv8) == 1) { _mux1 = String("Bmp-085"); } if ((_gtv8) == 2) { _mux1 = String("DHT-22"); } if (1) { _dispTempLength1 = ((_mux1)).length(); if (_disp3oldLength > _dispTempLength1) { _isNeedClearDisp1 = 1; } _disp3oldLength = _dispTempLength1; _lcd1.setCursor(int((20 - _dispTempLength1) / 2), 1); _lcd1.print((_mux1)); } else { if (_disp3oldLength > 0) { _isNeedClearDisp1 = 1; _disp3oldLength = 0; } } if ((_gtv8) == 0) { _mux2 = _gtv1; } if ((_gtv8) == 1) { _mux2 = _gtv2; } if ((_gtv8) == 2) { _mux2 = _gtv5; } if (1) { _dispTempLength1 = ((_mux2)).length(); if (_disp4oldLength > _dispTempLength1) { _isNeedClearDisp1 = 1; } _disp4oldLength = _dispTempLength1; _lcd1.setCursor(0, 2); _lcd1.print((_mux2)); } else { if (_disp4oldLength > 0) { _isNeedClearDisp1 = 1; _disp4oldLength = 0; } } if ((_gtv8) == 0) { _mux3 = String("-----"); } if ((_gtv8) == 1) { _mux3 = ((_gtv3) + (String("/")) + (_gtv4)); } if ((_gtv8) == 2) { _mux3 = _gtv6; } if (1) { _dispTempLength1 = ((_mux3)).length(); if (_disp5oldLength > _dispTempLength1) { _isNeedClearDisp1 = 1; } _disp5oldLength = _dispTempLength1; _lcd1.setCursor(0, 3); _lcd1.print((_mux3)); } else { if (_disp5oldLength > 0) { _isNeedClearDisp1 = 1; _disp5oldLength = 0; } } } bool _isTimer(unsigned long startTime, unsigned long period ) { unsigned long endTime; endTime = startTime + period; return (millis() >= endTime); } String _floatToStringWitRaz(float value, int raz) { float tv; int ti = int(value); String ts = String(ti); if (raz == 0) { return ts; } ts += "."; float tf = abs(value - ti); for (int i = 1; i <= raz; i++ ) { tv = tf * 10; ti = int(tv); ts += String(ti); tf = (tv - ti); } return ts; } float _convertDS18x2xData(byte type_s, byte data[12]) { int16_t raw = (data[1] << 8) | data[0]; if (type_s) { raw = raw << 3; if (data[7] == 0x10) { raw = (raw & 0xFFF0) + 12 - data[6]; } } else { byte cfg = (data[4] & 0x60); if (cfg == 0x00) raw = raw & ~7; else if (cfg == 0x20) raw = raw & ~3; else if (cfg == 0x40) raw = raw & ~1; } return (float)raw / 16.0; } float _readDS18_ow3(byte addr[8], byte type_s) { byte data[12]; byte i; _ow3.reset(); _ow3.select(addr); _ow3.write(0xBE); for ( i = 0; i < 9; i++) { data[i] = _ow3.read(); } _ow3.reset(); _ow3.select(addr); _ow3.write(0x44, 1); return _convertDS18x2xData(type_s, data); } 



Ps. In the next video tutorial, we plan to connect an SD card reader for logging sensor readings.

Source: https://habr.com/ru/post/241913/


All Articles