In continuarea articolului precedent vom adauga la montajul statiei meteo 2 senzori DHT22 pentru monitorizarea interior/exterior, 3 senzori DS18B20 pentru supravegherea celor 3 zone distincte si un modul RTC cu DS3231.
Pentru a folosi acesti senzori vom utiliza codurile din articolele publicate anterior pe site, linkurile fiind putin mai sus.
Vom folosi un Arduino Mega iar senzorii DHT22 sunt conectati la pinii D2 si D3, senzorii DS18B20 sunt conectati toti la pinul D22 iar modulul RTC se coneteaza la pinii SDA si SCL ai Arduino.
Sketch-ul actualizat arata asa:
/* www.electronicstore.ro
* Sketch statie meteo cu 2 senzori DHT22 si 3 senzori DS18B20
* impreuna cu modul de ceas RTC si afisare pe display grafic 128x64
* ver 1.0
* */
#include "DHT.h"
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 22
#define TEMPERATURE_PRECISION 9
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress senzor1, senzor2, senzor3;
#define DHTPIN1 2
#define DHTTYPE1 DHT22
DHT dht1(DHTPIN1, DHTTYPE1);
#define DHTPIN2 3
#define DHTTYPE2 DHT22
DHT dht2(DHTPIN2, DHTTYPE2);
#include <Wire.h>
#include <TimeLib.h>
#include <DS1307RTC.h>
#include "U8glib.h"
U8GLIB_ST7920_128X64 u8g(52, 51, 50, U8G_PIN_NONE);
#define icon_width 16
#define icon_height 16
static unsigned char clock_bits[] = {
0x00, 0x00, 0xc0, 0x01, 0xb0, 0x06, 0x8c, 0x18, 0x82, 0x20, 0x82, 0x20,
0x81, 0x40, 0x81, 0x7f, 0x01, 0x40, 0x02, 0x20, 0x02, 0x20, 0x0c, 0x18,
0x30, 0x06, 0xc0, 0x01, 0x00, 0x00, 0x00, 0x00 };
static unsigned char clock2_bits[] = {
0xff, 0xff, 0x3f, 0xfe, 0x4f, 0xf9, 0x73, 0xe7, 0x7d, 0xdf, 0x7d, 0xdf,
0x7e, 0xbf, 0x7e, 0x80, 0xfe, 0xbf, 0xfd, 0xdf, 0xfd, 0xdf, 0xf3, 0xe7,
0xcf, 0xf9, 0x3f, 0xfe, 0xff, 0xff, 0xff, 0xff };
static unsigned char home_bits[] = {
0x00, 0x00, 0x80, 0x01, 0x40, 0x02, 0x20, 0x04, 0x10, 0x08, 0x08, 0x10,
0x04, 0x20, 0xfc, 0x3f, 0x04, 0x20, 0x74, 0x2f, 0x74, 0x2f, 0x74, 0x20,
0x74, 0x20, 0xfc, 0x3f, 0x00, 0x00, 0x00, 0x00 };
static unsigned char home2_bits[] = {
0xff, 0xff, 0x7f, 0xfe, 0xbf, 0xfd, 0xdf, 0xfb, 0xef, 0xf7, 0xf7, 0xef,
0xfb, 0xdf, 0x03, 0xc0, 0xfb, 0xdf, 0x8b, 0xd0, 0x8b, 0xd0, 0x8b, 0xdf,
0x8b, 0xdf, 0x03, 0xc0, 0xff, 0xff, 0xff, 0xff };
static unsigned char out_bits[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x10, 0x01, 0x08, 0x02,
0x08, 0x02, 0x08, 0x02, 0x10, 0x01, 0x10, 0x01, 0xe0, 0x00, 0xe0, 0x00,
0xe0, 0x00, 0xe0, 0x38, 0xe3, 0xfe, 0xff, 0xff };
static unsigned char out2_bits[] = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xef, 0xfe, 0xf7, 0xfd,
0xf7, 0xfd, 0xf7, 0xfd, 0xef, 0xfe, 0xef, 0xfe, 0x1f, 0xff, 0x1f, 0xff,
0x1f, 0xff, 0x1f, 0xc7, 0x1c, 0x01, 0x00, 0x00 };
static unsigned char unu_bits[] = {
0x00, 0x00, 0xc0, 0x01, 0xe0, 0x01, 0xf0, 0x01, 0xb8, 0x01, 0x98, 0x01,
0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01,
0x80, 0x01, 0xe0, 0x07, 0xe0, 0x07, 0x00, 0x00 };
static unsigned char unu2_bits[] = {
0xff, 0xff, 0x3f, 0xfe, 0x1f, 0xfe, 0x0f, 0xfe, 0x47, 0xfe, 0x67, 0xfe,
0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe,
0x7f, 0xfe, 0x1f, 0xf8, 0x1f, 0xf8, 0xff, 0xff };
static unsigned char doi_bits[] = {
0x00, 0x00, 0xc0, 0x0f, 0xe0, 0x1f, 0x30, 0x18, 0x30, 0x18, 0x00, 0x18,
0x00, 0x1c, 0x00, 0x0e, 0x00, 0x07, 0x80, 0x03, 0xc0, 0x01, 0xe0, 0x00,
0x60, 0x00, 0xe0, 0x1f, 0xe0, 0x1f, 0x00, 0x00 };
static unsigned char doi2_bits[] = {
0xff, 0xff, 0x3f, 0xf0, 0x1f, 0xe0, 0xcf, 0xe7, 0xcf, 0xe7, 0xff, 0xe7,
0xff, 0xe3, 0xff, 0xf1, 0xff, 0xf8, 0x7f, 0xfc, 0x3f, 0xfe, 0x1f, 0xff,
0x9f, 0xff, 0x1f, 0xe0, 0x1f, 0xe0, 0xff, 0xff };
static unsigned char trei_bits[] = {
0x00, 0x00, 0x80, 0x07, 0xc0, 0x0f, 0x60, 0x18, 0x20, 0x30, 0x00, 0x30,
0x00, 0x18, 0x00, 0x0e, 0x80, 0x0f, 0x00, 0x18, 0x00, 0x30, 0x20, 0x30,
0x60, 0x18, 0xc0, 0x0f, 0x80, 0x07, 0x00, 0x00 };
static unsigned char trei2_bits[] = {
0xff, 0xff, 0x7f, 0xf8, 0x3f, 0xf0, 0x9f, 0xe7, 0xdf, 0xcf, 0xff, 0xcf,
0xff, 0xe7, 0xff, 0xf1, 0x7f, 0xf0, 0xff, 0xe7, 0xff, 0xcf, 0xdf, 0xcf,
0x9f, 0xe7, 0x3f, 0xf0, 0x7f, 0xf8, 0xff, 0xff };
int temp1 = 0;
int temp2 = 0;
int hum1 = 0;
int hum2 = 0;
int ora = 10;
int minut = 25;
int sec = 22;
int zi = 12;
int luna = 3;
int an = 2020;
float zona1, zona2, zona3;
void ecran1(void) {
u8g.setFont(u8g_font_unifont);
u8g.drawStr( 15, 18, "www.");
u8g.drawStr( 03, 38, "electronicstore");
u8g.drawStr( 80, 58, ".ro");
}
void ecran2(void) {
u8g.setFont(u8g_font_unifont);
u8g.drawStr( 27, 18, "Ceas si");
u8g.drawStr( 17, 38, "statie meteo");
u8g.drawStr( 47, 58, "grafica");
}
int arata1(int ora, int minut, int sec, int zi, int luna, int an) {
enum {BufSize=20};
char buf[BufSize];
snprintf (buf, BufSize, "%d:%d:%d", ora, minut, sec);
enum {BufSize2=20};
char buf2[BufSize2];
snprintf (buf2, BufSize2, "%d/%d/%d", zi, luna, an);
u8g.setFont(u8g_font_7x13);
u8g.drawFrame(0,0,128,20);
u8g.drawXBM( 2, 2, icon_width, icon_height, clock2_bits);
u8g.drawXBM( 19, 2, icon_width, icon_height, home_bits);
u8g.drawXBM( 36, 2, icon_width, icon_height, out_bits);
u8g.drawXBM( 53, 2, icon_width, icon_height, unu_bits);
u8g.drawXBM( 70, 2, icon_width, icon_height, doi_bits);
u8g.drawXBM( 87, 2, icon_width, icon_height, trei_bits);
u8g.drawFrame(0,22,128,42);
u8g.drawStr( 7, 38, buf);
u8g.drawStr( 7, 58, buf2);
}
int arata2(int temp1, int hum1) {
enum {BufSize=20};
char buf[BufSize];
snprintf (buf, BufSize, "Temp: %d%cC", temp1, 0xB0);
enum {BufSize2=20};
char buf2[BufSize2];
snprintf (buf2, BufSize2, "Hum: %d%%", hum1);
u8g.setFont(u8g_font_7x13);
u8g.drawFrame(0,0,128,20);
u8g.drawXBM( 2, 2, icon_width, icon_height, clock_bits);
u8g.drawXBM( 19, 2, icon_width, icon_height, home2_bits);
u8g.drawXBM( 36, 2, icon_width, icon_height, out_bits);
u8g.drawXBM( 53, 2, icon_width, icon_height, unu_bits);
u8g.drawXBM( 70, 2, icon_width, icon_height, doi_bits);
u8g.drawXBM( 87, 2, icon_width, icon_height, trei_bits);
u8g.drawFrame(0,22,128,42);
u8g.drawStr( 7, 38, buf);
u8g.drawStr( 10, 58, buf2);
}
int arata3(int temp2, int hum2) {
enum {BufSize=20};
char buf[BufSize];
snprintf (buf, BufSize, "Temp: %d%cC", temp2, 0xB0);
enum {BufSize2=20};
char buf2[BufSize2];
snprintf (buf2, BufSize2, "Hum: %d%%", hum2);
u8g.setFont(u8g_font_7x13);
u8g.drawFrame(0,0,128,20);
u8g.drawXBM( 2, 2, icon_width, icon_height, clock_bits);
u8g.drawXBM( 19, 2, icon_width, icon_height, home_bits);
u8g.drawXBM( 36, 2, icon_width, icon_height, out2_bits);
u8g.drawXBM( 53, 2, icon_width, icon_height, unu_bits);
u8g.drawXBM( 70, 2, icon_width, icon_height, doi_bits);
u8g.drawXBM( 87, 2, icon_width, icon_height, trei_bits);
u8g.drawFrame(0,22,128,42);
u8g.drawStr( 7, 38, buf);
u8g.drawStr( 10, 58, buf2);
}
int arata4(int zona1) {
enum {BufSize=20};
char buf[20];
snprintf (buf, BufSize, "Temp: %d%cC", zona1, 0xB0);
u8g.setFont(u8g_font_7x13);
u8g.drawFrame(0,0,128,20);
u8g.drawXBM( 2, 2, icon_width, icon_height, clock_bits);
u8g.drawXBM( 19, 2, icon_width, icon_height, home_bits);
u8g.drawXBM( 36, 2, icon_width, icon_height, out_bits);
u8g.drawXBM( 53, 2, icon_width, icon_height, unu2_bits);
u8g.drawXBM( 70, 2, icon_width, icon_height, doi_bits);
u8g.drawXBM( 87, 2, icon_width, icon_height, trei_bits);
u8g.drawFrame(0,22,128,42);
u8g.drawStr( 7, 38, "Zona 1...");
u8g.drawStr( 7, 58, buf);
}
int arata5(int zona2) {
enum {BufSize=20};
char buf[BufSize];
snprintf (buf, BufSize, "Temp: %d%cC", zona2, 0xB0);
u8g.setFont(u8g_font_7x13);
u8g.drawFrame(0,0,128,20);
u8g.drawXBM( 2, 2, icon_width, icon_height, clock_bits);
u8g.drawXBM( 19, 2, icon_width, icon_height, home_bits);
u8g.drawXBM( 36, 2, icon_width, icon_height, out_bits);
u8g.drawXBM( 53, 2, icon_width, icon_height, unu_bits);
u8g.drawXBM( 70, 2, icon_width, icon_height, doi2_bits);
u8g.drawXBM( 87, 2, icon_width, icon_height, trei_bits);
u8g.drawFrame(0,22,128,42);
u8g.drawStr( 7, 38, "Zona 2...");
u8g.drawStr( 7, 58, buf);
}
int arata6(int zona3) {
enum {BufSize=20};
char buf[BufSize];
snprintf (buf, BufSize, "Temp: %d%cC", zona3, 0xB0);
u8g.setFont(u8g_font_7x13);
u8g.drawFrame(0,0,128,20);
u8g.drawXBM( 2, 2, icon_width, icon_height, clock_bits);
u8g.drawXBM( 19, 2, icon_width, icon_height, home_bits);
u8g.drawXBM( 36, 2, icon_width, icon_height, out_bits);
u8g.drawXBM( 53, 2, icon_width, icon_height, unu_bits);
u8g.drawXBM( 70, 2, icon_width, icon_height, doi_bits);
u8g.drawXBM( 87, 2, icon_width, icon_height, trei2_bits);
u8g.drawFrame(0,22,128,42);
u8g.drawStr( 7, 38, "Zona 3...");
u8g.drawStr( 7, 58, buf);
}
void clearLCD(){
u8g.firstPage();
do {
} while( u8g.nextPage() );
}
void setup(void) {
Serial.begin(9600);
Serial.println("Testare 3 senzori DS18B20 impreuna cu Arduino");
dht1.begin();
dht2.begin();
sensors.begin();
u8g.setColorIndex(1);
u8g.firstPage();
do {
u8g.drawFrame(0,0,128,64);
ecran1();
} while( u8g.nextPage() );
delay(2000);
clearLCD();
u8g.firstPage();
do {
u8g.drawFrame(0,0,128,64);
u8g.drawFrame(2,2,124,60);
ecran2();
} while( u8g.nextPage() );
delay(2000);
clearLCD();
}
void loop(void) {
hum1 = dht1.readHumidity();
temp1 = dht1.readTemperature();
hum2 = dht2.readHumidity();
temp2 = dht2.readTemperature();
Serial.print("Citire temperatura...");
sensors.requestTemperatures();
Serial.println("OK");
tmElements_t tm;
RTC.read(tm);
u8g.firstPage();
do {
ora = tm.Hour;
minut = tm.Minute;
sec = tm.Second;
zi = tm.Day;
luna = tm.Month;
an = tmYearToCalendar(tm.Year);
arata1(ora, minut, sec, zi, luna, an);
} while( u8g.nextPage() );
delay(2000);
clearLCD();
u8g.firstPage();
do {
arata2(temp1, hum1);
} while( u8g.nextPage() );
delay(2000);
clearLCD();
u8g.firstPage();
do {
arata3(temp2, hum2);
} while( u8g.nextPage() );
delay(2000);
clearLCD();
u8g.firstPage();
do {
if (sensors.getAddress(senzor1, 0)) {
float zona1 = sensors.getTempC(senzor1);
arata4(zona1);
}
} while( u8g.nextPage() );
delay(2000);
clearLCD();
u8g.firstPage();
do {
if (sensors.getAddress(senzor2, 1)) {
float zona2 = sensors.getTempC(senzor2);
arata5(zona2);
}
} while( u8g.nextPage() );
delay(2000);
clearLCD();
u8g.firstPage();
do {
if (sensors.getAddress(senzor3, 2)) {
float zona3 = sensors.getTempC(senzor3);
arata6(zona3);
}
} while( u8g.nextPage() );
delay(2000);
clearLCD();
}
Un filmulet cu statia meteo in functiune este mai jos:
Va urma...