#include <Wire.
h>
#include <RTClib.h>
#include <LiquidCrystal.h>
RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {
"Sun",
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat"
};
// temperature sensor
const int sensorPin = A0;
float sensorValue;
float voltageOut;
float temperatureC;
// esp8266
String stmt = "";
// set up alarm variables
int a_year = 0;
int a_month = 0;
int a_day = 0;
int a_hour = 0;
int a_minute = 0;
int counter_at_alarm;
// set up local weather variables
float l_temp;
int l_humidity;
float l_pressure;
float l_wind_speed;
// another devices
const int buzzerPin = 6;
const int buttonPin = 8;
int buttonState = 0;
int oldButtonState = 0;
int counter = 0;
int tempPin = 0;
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() {
Serial.begin(9600); /* start serial for debug */
Wire.begin(8); /* join i2c bus with address 8 */
Wire.onReceive(receiveEvent); /* register receive event */
Wire.onRequest(requestEvent); /* register request event */
// DS1307
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
// other devices
pinMode(buzzerPin, OUTPUT);
pinMode(buttonPin, INPUT);
lcd.begin(16, 2);
void loop() {
DateTime now = rtc.now();
// handle request from esp8266
handleReq(stmt);
buttonState = digitalRead(buttonPin);
if (buttonState != oldButtonState) {
oldButtonState = buttonState;
counter++;
delay(50);
} else if (counter % 3 == 0) {
lcd.setCursor(0,0);
lcd.print(now.year());
lcd.print('/');
lcd.print(now.month());
lcd.print('/');
lcd.print(now.day());
lcd.print(" (");
lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);
lcd.print(") ");
lcd.setCursor(0,1);
lcd.print(now.hour());
lcd.print(':');
lcd.print(now.minute());
lcd.print(':');
lcd.print(now.second());
} else if (counter % 3 == 1) {
lcd.setCursor(0,0);
lcd.print("Room Temp: ");
lcd.setCursor(0,1);
lcd.print(getTemp());
lcd.print(" *C");
} else if (counter % 3 == 2) {
lcd.setCursor(0,0);
lcd.print("T: ");
lcd.print(l_temp);
lcd.print(" ");
lcd.print("H: ");
lcd.print(l_humidity);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print("P: ");
lcd.print(l_pressure);
lcd.print(" ");
lcd.print("WS: ");
lcd.print(l_wind_speed);
// check alarm
if (now.year() == a_year && now.month() == a_month && now.day() == a_day && now.hour() ==
a_hour && now.minute() == a_minute) {
if (now.second() < 3) {
counter_at_alarm = counter;
if (counter_at_alarm == counter) {
tone(buzzerPin,220,125);
delay(125);
delay(1000);
lcd.clear();
// function that executes whenever data is received from master
void receiveEvent(int howMany) {
while (Wire.available() > 0) {
char c = Wire.read();
stmt += c;
// function that executes whenever data is requested from master
void requestEvent() {
void handleReq(String &stmt) {
if (stmt.length() != 0) {
Serial.println(stmt);
// Find the positions of spaces
int firstSpace = stmt.indexOf(' ');
// Extract substrings
String action = stmt.substring(0, firstSpace);
if (action == "GET_WEATHER") {
int secondSpace = stmt.indexOf(' ', firstSpace + 1);
int thirdSpace = stmt.indexOf(' ', secondSpace + 1);
int fourthSpace = stmt.indexOf(' ', thirdSpace + 1);
l_temp = stmt.substring(firstSpace + 1, secondSpace).toFloat();
l_humidity = stmt.substring(secondSpace + 1, thirdSpace).toInt();
l_pressure = stmt.substring(thirdSpace + 1, fourthSpace).toFloat();
l_wind_speed = stmt.substring(fourthSpace + 1).toFloat();
} else {
String date_time = stmt.substring(firstSpace + 1);
// Extract year
int year = date_time.substring(0, 4).toInt();
// Extract month
int month = date_time.substring(5, 7).toInt();
// Extract day
int day = date_time.substring(8, 10).toInt();
// Extract hour
int hour = date_time.substring(11, 13).toInt();
// Extract minute
int minute = date_time.substring(14, 16).toInt();
if (action == "SET_ALARM") {
a_year = year;
a_month = month;
a_day = day;
a_hour = hour;
a_minute = minute;
} else if (action == "SET_TIME") {
rtc.adjust(DateTime(year, month, day, hour, minute, 0));
} else if (action == "SET_C_TIME") {
int second = date_time.substring(17, 19).toInt();
rtc.adjust(DateTime(year, month, day, hour, minute, second));
stmt = "";
float getTemp() {
int val;
val = analogRead(tempPin);
float mv = (val/1023.0)*5000;
float cel = mv/10;
return cel;