MASTER
#include <RTClib.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "U8glib.h"
#include <TimeLib.h>
#include <DS1307RTC.h>
#include <DHT.h>;
#define DHTPIN 4
#define DHTTYPE DHT22
#define SLAVE_ADDR 1
#define ANSWERSIZE 10
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27,20,4);
U8GLIB_ST7920_128X64_4X u8g(10);
char* monthval[] = {"","JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"};
char* dotw[] = {"SUN","MON","TUE","WED","THU","FRI","SAT"};
char d;
String dht22hum;
String dht22temp;
void i2c_comm(){
Wire.requestFrom(SLAVE_ADDR,10);
String datain;
while (Wire.available()) {
d = Wire.read();
datain += d;
}
dht22hum = datain.substring(0,5);
dht22temp = datain.substring(5,10);
}
void timeprint(){
tmElements_t tm;
if (RTC.read(tm)) {
u8g.setFont(u8g_font_9x18);
u8g.drawStr(48,32,": :");
u8g.setPrintPos(30,32);
if(tm.Hour<10){u8g.print("0");}
u8g.print(tm.Hour); //hour
u8g.setPrintPos(57,32);
if(tm.Minute<10){u8g.print("0");}
u8g.print(tm.Minute); //min
u8g.setPrintPos(84,32);
if(tm.Second<10){u8g.print("0");}
u8g.print(tm.Second); //sec
u8g.setFont(u8g_font_6x12);
u8g.drawStr(45,45," ");
u8g.setPrintPos(33,45);
if(tm.Day<10){u8g.print("0");}
u8g.print(tm.Day); //day
// monthfunc();
u8g.setPrintPos(51,45);
// if(tm.Month<10){u8g.print("0");}
// u8g.print(tm.Month); //month
u8g.print(monthval[tm.Month]); //month
u8g.setPrintPos(75,45);
u8g.print(tmYearToCalendar(tm.Year)); //year
u8g.setFont(u8g_font_4x6);
u8g.setPrintPos(59,64);
u8g.print(dotw[tm.Wday]);
Serial.print("dotw : ");
Serial.println(tm.Wday);
}
}
void dht22() {
u8g.setFont(u8g_font_4x6);
u8g.drawStr(0,64,"Hu ");
u8g.setPrintPos(12,64);
u8g.print(dht22hum); //humidity
u8g.drawStr(32,64," %");
u8g.setFont(u8g_font_4x6);
u8g.drawStr(0,5,"Ta");
u8g.setPrintPos(12,5);
u8g.print("00.00"); //average temp
u8g.drawStr(32,5,"'c");
u8g.drawStr(0,11,"T0 ");
u8g.setPrintPos(12,11);
u8g.print(dht22temp); //dht22 temp
u8g.drawStr(32,11,"'c");
}
void draw() {
/*
u8g.setFont(u8g_font_4x6);
u8g.drawStr(0,5,"Ta");
u8g.drawStr(12,5,"00.00"); //average temp
u8g.drawStr(32,5,"'c");*/
u8g.drawStr(93,5,"Li");
u8g.drawStr(105,5,"00.00");//light intensity
u8g.drawStr(125,5,"%");
u8g.setFont(u8g_font_4x6);
u8g.drawStr(0,29,"JAPN");
u8g.drawStr(0,45,"CGAS");
u8g.drawStr(113,29,"RAIN");
u8g.drawStr(113,45,"MOTN");
u8g.setFont(u8g_font_4x6);
u8g.drawStr(93,64,"AQ "); //air quality
u8g.drawStr(105,64,"00.00");
u8g.drawStr(125,64,"%");
u8g.drawStr(59,5,"-O-");
}
void setup()
{
Wire.begin();
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.setCursor(3,0);
lcd.print("Hello, world!");
lcd.setCursor(2,1);
u8g.setColorIndex(1);
}
void loop(){
u8g.firstPage();
do {
draw();
timeprint();
dht22();
} while( u8g.nextPage() );
lcd.setCursor(0,2);
for(int i=1;i<8;i++){
lcd.print(" ");
lcd.write(i);}
}
SLAVE
#include <DHT.h>;
#include <Wire.h>
//Constants
#define DHTPIN 4
#define DHTTYPE DHT22
#define SLAVE_ADDR 1
#define ANSWERSIZE 5
DHT dht(DHTPIN, DHTTYPE);
volatile byte* FloatPtr;
float hum;
float temp;
char dhtdata[2]; // 0 is hum; 1 is temp
bool trig;
String hdht;
void setup()
{
Serial.begin(9600);
dht.begin();
Wire.begin(SLAVE_ADDR);
Wire.onRequest(reqDat);
}
void loop()
{
hum = dht.readHumidity();
temp= dht.readTemperature();
hdht = String(hum, 2);
Serial.print("Humidity: ");
Serial.print(String(hum, 2));
Serial.print(" %, Temp: ");
Serial.print(String(temp, 2));
Serial.println(" Celsius");
delay(100);
}
void reqDat() {
String hdht = String(hum, 2);
String tdht = String(temp, 2);
byte tresponse[ANSWERSIZE];
byte hresponse[ANSWERSIZE];
for (byte i=0;i<ANSWERSIZE;i++) {
hresponse[i] = byte(hdht.charAt(i));
}
Wire.write(hresponse,sizeof(hresponse));
for (byte i=0;i<ANSWERSIZE;i++) {
tresponse[i] = (byte)tdht.charAt(i);
}
Wire.write(tresponse,sizeof(tresponse));
}