The SmartBox is a device which can display the actual time, the temperature and the light intensity. To make this, this device has an RTC clock ( ds1307 ), a DHT22 and a photoresistor. Values will be displayed on a i2c Oled Screen ( 128per64 ).
ConnectionsNote : i2c ( SDA, SCL ) can support several devices.
Code#include "U8glib.h"
#include <Wire.h>
#include <ds3231.h>
#include "DHT.h"
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
struct ts t;
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE);
void setup() {
dht.begin();
Wire.begin();
DS3231_init(DS3231_INTCN);
t.hour=22;
t.min=29;
t.sec=0;
t.mday=12;
t.mon=12;
t.year=2019;
DS3231_set(t);
if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {
u8g.setColorIndex(100);
}
else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {
u8g.setColorIndex(1);
}
else if ( u8g.getMode() == U8G_MODE_BW ) {
u8g.setColorIndex(3);
}
else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {
u8g.setHiColorByRGB(255,255,255);
}
}
void loop(){
DS3231_get(&t);
u8g.firstPage();
do {
draw();
} while( u8g.nextPage() );
delay(2000);
u8g.firstPage();
do {
draw2();
} while( u8g.nextPage() );
delay(2000);
u8g.firstPage();
do {
draw3();
} while( u8g.nextPage() );
delay(2000);
u8g.firstPage();
do {
draw4();
} while( u8g.nextPage() );
delay(2000);
}
void draw(){
int a = t.hour;
int b = t.min;
u8g.setFont(u8g_font_gdr25r);
char bb[2];
sprintf (bb, "%d", b);
u8g.drawStr(65, 40, bb);
char aa[2];
sprintf (aa, "%d", a);
u8g.drawStr(20, 40, aa);
u8g.drawStr(56,40 ,":");
}
void draw2(){
int c = t.mday;
int d = t.mon;
u8g.setFont(u8g_font_gdr25r);
char cc[2];
sprintf (cc, "%d", c);
u8g.drawStr(22, 40, cc);
char dd[2];
sprintf (dd, "%d", d);
u8g.drawStr(70, 40, dd);
u8g.drawStr(56, 40, "/");
}
void draw3(){
int h = dht.readHumidity();
int t = dht.readTemperature();
u8g.setFont(u8g_font_gdr25r);
char hh[2];
sprintf (hh, "%d", h);
u8g.drawStr(35, 61, hh);
char tt[2];
sprintf (tt, "%d", t);
u8g.drawStr(28, 30, tt);
u8g.drawStr(0, 30, "t:");
u8g.drawStr(0, 61, "h:");
u8g.drawStr(64, 30, "°C");
u8g.drawStr(71, 61, "%");
}
void draw4(){
u8g.setFont(u8g_font_gdr25r);
int val = analogRead(A0);
char vv[3];
sprintf (vv, "%d", val);
u8g.drawStr(72, 40, vv);
u8g.drawStr(0, 40, "Lux:");
}
Firstly, we define the Library U8glib ( for the oled screen ), Wire ( for SDA, SCL ), ds3231 ( for the RTC clock ) and DHT ( for the DHT22 ) :
#include "U8glib.h"
#include <Wire.h>
#include <ds3231.h>
#include "DHT.h"
Then, we define the digital pin 2 for the DHT and the type of DHT sensor that we'll use ( here a DHT22 ) :
#define DHTPIN 2
#define DHTTYPE DHT22
Secondly, we initialize every sensor and we start the data capturing :
void setup() {
dht.begin();
Wire.begin();
DS3231_init(DS3231_INTCN);
These lines are used to set the RTC Clock ( to synchronize the RTC clock with your timetable ) :
t.hour=22;
t.min=29;
t.sec=0;
t.mday=12;
t.mon=12;
t.year=2019;
DS3231_set(t);
These lines are used to set the screen color ( if you want to modify the code, you Don't have to change them ) :
if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {
u8g.setColorIndex(100);
}
else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {
u8g.setColorIndex(1);
}
else if ( u8g.getMode() == U8G_MODE_BW ) {
u8g.setColorIndex(3);
}
else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {
u8g.setHiColorByRGB(255,255,255);
}
}
In the void loop() we execute all peripherical programs ( there is 4 peripherical programs ) :
void loop(){
DS3231_get(&t);
u8g.firstPage();
do {
draw();
} while( u8g.nextPage() );
delay(2000);
u8g.firstPage();
do {
draw2();
} while( u8g.nextPage() );
delay(2000);
u8g.firstPage();
do {
draw3();
} while( u8g.nextPage() );
delay(2000);
u8g.firstPage();
do {
draw4();
} while( u8g.nextPage() );
delay(2000);
}
These programs display datas coming from the sensor :
void draw4(){
u8g.setFont(u8g_font_gdr25r);
int val = analogRead(A0);
char vv[3];
sprintf (vv, "%d", val);
u8g.drawStr(72, 40, vv);
u8g.drawStr(0, 40, "Lux:");
}
They read the values coming from the sensor :
int val = analogRead(A0);
And display them :
char vv[3];
sprintf (vv, "%d", val);
u8g.drawStr(72, 40, vv);
u8g.drawStr(0, 40, "Lux:");
This program described above is for the photoresistor, but there are some others programs.
For the RTC Clock : "draw" ( it displays the Hour/Min ) :
void draw(){
int a = t.hour;
int b = t.min;
u8g.setFont(u8g_font_gdr25r);
char bb[2];
sprintf (bb, "%d", b);
u8g.drawStr(65, 40, bb);
char aa[2];
sprintf (aa, "%d", a);
u8g.drawStr(20, 40, aa);
u8g.drawStr(56,40 ,":");
}
For the RTC Clock : "draw2" ( it displays the Day/Month ) :
void draw2(){
int c = t.mday;
int d = t.mon;
u8g.setFont(u8g_font_gdr25r);
char cc[2];
sprintf (cc, "%d", c);
u8g.drawStr(22, 40, cc);
char dd[2];
sprintf (dd, "%d", d);
u8g.drawStr(70, 40, dd);
u8g.drawStr(56, 40, "/");
}
For the DHT22 : "draw3" ( it displays the °C and the humidity % ) :
void draw3(){
int h = dht.readHumidity();
int t = dht.readTemperature();
u8g.setFont(u8g_font_gdr25r);
char hh[2];
sprintf (hh, "%d", h);
u8g.drawStr(35, 61, hh);
char tt[2];
sprintf (tt, "%d", t);
u8g.drawStr(28, 30, tt);
u8g.drawStr(0, 30, "t:");
u8g.drawStr(0, 61, "h:");
u8g.drawStr(64, 30, "°C");
u8g.drawStr(71, 61, "%");
}
For the Photoresistor : "draw4" ( it displays the light intensity values ).
void draw4(){
u8g.setFont(u8g_font_gdr25r);
int val = analogRead(A0);
char vv[3];
sprintf (vv, "%d", val);
u8g.drawStr(72, 40, vv);
u8g.drawStr(0, 40, "Lux:");
}
Final Results"Draw"
"Draw2"
"Draw3"
"Draw4"
Comments
Please log in or sign up to comment.