# data[].attributes.body
/* Embedded Assignment Sem 2, 2018/2019

- Fikry
- Khalish
- Amirul
- Nakib
- Syakir

*/
#include "mbed.h"
#include "TextLCD.h"            // library for LCD Display

TextLCD lcd(p21, p22, p17, p18, p19, p20); // rs, e, d0, d1, d2, d3 pin out for LCD

DigitalIn press(p14);            // Joy stick button
BusIn Up(p15);                   // Joy stick
BusIn Down(p12);                 // Joy stick
BusIn Left(p13);                 // Joy stick
BusIn Right(p16);                // Joy stick

int y[4]={00,00,00,00};
char day[7][40] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};

//=================================== function for time display =================================

// function for time
void time(int hour, int min, int sec){

for(int m=0;m<7;m++){

for(int k=1;k<=12;k++){
if(hour != 1){
k = hour;
}

for(int j=0;j<60;j++){
if(min != 0){
j = min;
}

for(int i=0;i<60;i++){
if(sec != 0){
i = sec;
}

lcd.cls();
lcd.locate(0,0);                                // locate time
lcd.printf(" %d: %d: %d: ", k, j, i);      // print time

//lcd.locate(0,1);                                // locate day
lcd.printf("%s", day[m]);                        // print day

i++;
sec = 0;

wait(0.2);
} // end for loop i (sec)

min = 0;
} // end for loop j (min)

hour = 1;
} // end for loop k (hour)
} // end for loop m (days)
}

//==================================== main function ============================================

int main() {
// start program
lcd.printf("Press button to set time");

while(1){
// wait for input from user - joy stick button
if(press==true){
int x=0;
int dayN = 0;

lcd.cls();
lcd.locate(0,0);
lcd.printf("%d:%d:%d",y[0],y[1],y[2]);      // locate time

wait(0.5);

while(1){
lcd.locate(0,0);
lcd.printf("%d:%d:%d",y[0],y[1],y[2]);          // locate time
lcd.locate(0,1);                                 // locate text

// choosing the array
if(x==0){
lcd.printf("                 ");
lcd.locate(0,1);
lcd.printf("Hour");
}
if(x==1){
lcd.printf("                 ");
lcd.locate(0,1);
lcd.printf("Minute");
}
if(x==2){
lcd.printf("                 ");
lcd.locate(0,1);
lcd.printf("Sec");
}
if(x==3){
lcd.printf("                 ");
lcd.locate(0,1);
lcd.printf("DONE ? - Press!! ");
}

//=================== Operations by Joystic ==========================

//=================== press up
if (Up){
y[x]++;
wait(0.5);
} // end if (up)

//=================== press down
if (Down){
y[x]--;
wait(0.5);
} // end if (down)

//=================== press left
if (Left){
if(x==0){
x=3;
wait(0.5);
} // end if

else{
x--;
wait(0.5);
} // end else
} // end if (left)

//=================== press right
if (Right){
if(x==3){
x=0;
wait(0.5);
} // end if

else{
x++;
wait(0.5);
} // end else
} // end if (right)

//=================== press joystic
if(x==3){
if(press==true){
time(y[0], y[1], y[2]);
}
}

} // end while loop
} // end if (user press)

/*else{
time();
}*/

} // end while loop
} // end main

# data[].attributes.body
/* Embedded Assignment Sem 2, 2018/2019

- Fikry
- Khalish
- Amirul
- Nakib
- Syakir

*/
#include "mbed.h"
#include "TextLCD.h"            // library for LCD Display

TextLCD lcd(p21, p22, p17, p18, p19, p20); // rs, e, d0, d1, d2, d3 pin out for LCD

DigitalIn press(p14);            // Joy stick button
BusIn Up(p15);                   // Joy stick
BusIn Down(p12);                 // Joy stick
BusIn Left(p13);                 // Joy stick
BusIn Right(p16);                // Joy stick

PwmOut speaker(p26);             // Speaker

int y[3]={00,00,00};
int alarm[3]={00,00,00};

char day[7][40] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};

char menu[2][40] = {"1. Set Timer", "2. Set Alarm"};

//=================================== buzzer ====================================================

void playAlarm(){

lcd.cls();
lcd.locate(0,0);
lcd.printf("-- ALARM !!!! --");

lcd.locate(0,1);
lcd.printf("Press Anything");

while(1){
for (float i=2000.0; i<10000.0; i+=500) {
speaker.period(1.0/i);
speaker=0.5;
wait(0.1);
}

if(press){
for (float i=2000.0; i<10000.0; i+=500) {
speaker.period(1.0/i);
speaker=1.0;
wait(0.1);
}
return;
}
}
}

//=================================== function for time display =================================

// function for time
void time(int hour, int min, int sec){

for(int m=0;m<7;m++){

for(int k=1;k<=12;k++){
if(hour != 1){
k = hour;
}

for(int j=0;j<60;j++){
if(min != 0){
j = min;
}

for(int i=0;i<60;i++){
if(sec != 0){
i = sec;
}

lcd.cls();
lcd.locate(2,0);                                // locate time
lcd.printf(" %d : %d : %d ", k, j, i);      // print time

lcd.locate(0,1);                                // locate day
//          lcd.printf("%s", day[m]);                        // print day

if(alarm[0]==0 && alarm[1]==0 && alarm[2]==0){
lcd.printf("No Alarm Set");
}

else{
lcd.printf("*Alarm: %d:%d:%d", alarm[0],alarm[1],alarm[2]);
}

if(k==alarm[0] && j==alarm[1] && i==alarm[2]){
playAlarm();
}

if(Left){
flag = 1;
return;
}

i++;
sec = 0;

wait(0.2);
} // end for loop i (sec)

min = 0;
} // end for loop j (min)

hour = 1;
} // end for loop k (hour)
} // end for loop m (days)
}

//==================================== set time function ============================================

void setTime(){

lcd.cls();
lcd.locate(0,0);
lcd.printf("-- Set Time --");
wait(1);

int x=0;

lcd.cls();

y[0] = 0;
y[1] = 0;
y[2] = 0;

while(1){
lcd.locate(2,0);
lcd.printf(" %d : %d : %d ",y[0],y[1],y[2]);          // locate time

lcd.locate(0,1);                             // locate text

// choosing the array
if(x==0){
lcd.printf("                 ");
lcd.locate(0,1);
lcd.printf("Hour");
wait(0.05);
}
if(x==1){
lcd.printf("                 ");
lcd.locate(0,1);
lcd.printf("Minute");
wait(0.05);
}
if(x==2){
lcd.printf("                 ");
lcd.locate(0,1);
lcd.printf("Second");
wait(0.05);
}
if(x==3){
lcd.printf("                 ");
lcd.locate(0,1);
lcd.printf("Done? - Press!! ");
wait(0.05);
}

//=================== Operations by Joystic ==========================

//=================== press up
if (Up){
// limit for hour
if(x==0){
if(y[0]>11){
y[0]=0;
wait(0.5);
}
}
// limit for minute
if(x==1){
if(y[1]>58){
y[1]=0-1;
wait(0.5);
}
}
// limit for seconds
if(x==2){
if(y[2]>58){
y[2]=0-1;
wait(0.5);
}
}

if(y[0]!=12||y[1]!=60||y[2]!=60){
y[x]++;
wait(0.5);
}
} // end if (up)

//=================== press down
if (Down){
// limit for hour
if(x==0){
if(y[0]<2){
y[0]=13;
wait(0.5);
}
}
// limit for minute
if(x==1){
if(y[1]<1){
y[1]=60;
wait(0.5);
}
}
// limit for seconds
if(x==2){
if(y[2]<1){
y[2]=60;
wait(0.5);
}
}

if(y[0]!=0 || y[1]!=0 || y[2]!=0){
y[x]--;
wait(0.5);
}
} // end if (down)

//=================== press left
if (Left){
if(x==0){
x=3;
wait(0.5);
} // end if

else{
x--;
wait(0.5);
} // end else
} // end if (left)

//=================== press right
if (Right){
if(x==3){
x=0;
wait(0.5);
} // end if

else{
x++;
wait(0.5);
} // end else
} // end if (right)

//=================== press joystic
if(x==3){
if(press==true){
time(y[0], y[1], y[2]);
}
}
} // end while loop
}

//==================================== set alarm function ============================================

void setAlarm(){
int x=0;

lcd.cls();

while(1){
lcd.locate(2,0);
lcd.printf(" %d : %d : %d ",y[0],y[1],y[2]);          // locate time

lcd.locate(0,1);                                // locate text

// select array
if(x==0){
lcd.printf("                 ");
lcd.locate(0,1);
lcd.printf("Hour");
wait(0.05);
}
if(x==1){
lcd.printf("                 ");
lcd.locate(0,1);
lcd.printf("Minute");
wait(0.05);
}
if(x==2){
lcd.printf("                 ");
lcd.locate(0,1);
lcd.printf("Second");
wait(0.05);
}
if(x==3){
lcd.printf("                 ");
lcd.locate(0,1);
lcd.printf("Done? - Press!! ");
wait(0.05);
}

//=================== Operations by Joystic ==========================

//=================== press up
if (Up){
// limit for hour
if(x==0){
if(y[0]>11){
y[0]=0;
wait(0.5);}
}
// limit for minute
if(x==1){
if(y[1]>58){
y[1]=0-1;
wait(0.5);
}
}
// limit for seconds
if(x==2){
if(y[2]>58){
y[2]=0-1;
wait(0.5);
}
}

if(y[0]!=12||y[1]!=60||y[2]!=60){
y[x]++;
wait(0.5);
}
} // end if (up)

//=================== press down
if (Down){
// limit for hour
if(x==0){
if(y[0]<2){
y[0]=13;
wait(0.5);
}
}

// limit for minute
if(x==1){
if(y[1]<1){
y[1]=60;
wait(0.5);
}
}

// limit for seconds
if(x==2){
if(y[2]<1){
y[2]=60;
wait(0.5);
}
}

if(y[0]!=0 || y[1]!=0 || y[2]!=0){
y[x]--;
wait(0.5);
}
} // end if (down)

//=================== press left
if (Left){
if(x==0){
x=3;
wait(0.5);
} // end if

else{
x--;
wait(0.5);
} // end else
} // end if (left)

//=================== press right
if (Right){
if(x==3){
x=0;
wait(0.5);
} // end if

else{
x++;
wait(0.5);
} // end else
} // end if (right)

//=================== press joystic
if(x==3){
if(press==true){
for(int z=0;z<3;z++){
alarm[z] = y[z];        // set array fot alarm
}
setTime();
}
}
}
}

//==================================== main function ============================================

int main() {
// start program
lcd.printf("-- Welcome --");
wait(1);

// main
lcd.cls();
lcd.locate(0,0);
lcd.printf("Main Menu");
wait(1);

while(1){
// wait for input from user - joy stick button
int x;

//=================== press up
if (Up){
x=0;
//       lcd.cls();
lcd.locate(0,1);
lcd.printf("%s", menu[x]);
} // end if (up)

//=================== press down
if (Down){
x=1;
//      lcd.cls();
lcd.locate(0,1);
lcd.printf("%s", menu[x]);
} // end if (down)

//=================== press
if(press){
if(x==0){
setTime();
}

if(x==1){
setAlarm();
}
}
} // end while loop
} // end main

# data[].attributes.body
/* Embedded Assignment Sem 2, 2018/2019

- Fikry
- Khalish
- Amirul
- Nakib
- Syakir

*/
#include "mbed.h"
#include "TextLCD.h"            // library for LCD Display

TextLCD lcd(p21, p22, p17, p18, p19, p20); // rs, e, d0, d1, d2, d3 pin out for LCD

DigitalIn press(p14);            // Joy stick button
BusIn Up(p15);                   // Joy stick
BusIn Down(p12);                 // Joy stick
BusIn Left(p13);                 // Joy stick
BusIn Right(p16);                // Joy stick

int y[4]={00,00,00,00};
char day[7][40] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};

char menu[2][40] = {"Set Time", "Set Alarm"};

//=================================== function for time display =================================

// function for time
void time(int hour, int min, int sec){

for(int m=0;m<7;m++){

for(int k=1;k<=12;k++){
if(hour != 1){
k = hour;
}

for(int j=0;j<60;j++){
if(min != 0){
j = min;
}

for(int i=0;i<60;i++){
if(sec != 0){
i = sec;
}

lcd.cls();
lcd.locate(0,0);                                // locate time
lcd.printf("%d:%d:%d", k, j, i);      // print time

lcd.locate(0,1);                                // locate day
lcd.printf("%s", day[m]);                        // print day

i++;
sec = 0;

wait(0.2);
} // end for loop i (sec)

min = 0;
} // end for loop j (min)

hour = 1;
} // end for loop k (hour)
} // end for loop m (days)
}

//==================================== set time function ============================================

void setTime(){
int x=0;

lcd.cls();
lcd.locate(0,0);
lcd.printf("%d:%d:%d",y[0],y[1],y[2]);      // locate time

wait(0.5);

while(1){
lcd.locate(0,0);
lcd.printf("%d:%d:%d",y[0],y[1],y[2]);          // locate time
lcd.locate(0,1);                             // locate text

// choosing the array
if(x==0){
lcd.printf("                 ");
lcd.locate(0,1);
lcd.printf("Hour");
wait(0.05);
}
if(x==1){
lcd.printf("                 ");
lcd.locate(0,1);
lcd.printf("Minute");
wait(0.05);
}
if(x==2){
lcd.printf("                 ");
lcd.locate(0,1);
lcd.printf("Second");
wait(0.05);
}
if(x==3){
lcd.printf("                 ");
lcd.locate(0,1);
lcd.printf("DONE? - Press!! ");
wait(0.05);
}

//=================== Operations by Joystic ==========================

//=================== press up
if (Up){
// limit for hour
if(x==0){
if(y[0]>11){
y[0]=0;
wait(0.5);}
}
// limit for minute
if(x==1){
if(y[1]>58){
y[1]=0-1;
wait(0.5);}
}
// limit for seconds
if(x==2){
if(y[2]>58){
y[2]=0-1;
wait(0.5);}
}

if(y[0]!=12||y[1]!=60||y[2]!=60){
y[x]++;
wait(0.5);}
} // end if (up)

//=================== press down
if (Down){
// limit for hour
if(x==0){
if(y[0]<2){
y[0]=13;
wait(0.5);}
}
// limit for minute
if(x==1){
if(y[1]<1){
y[1]=60;
wait(0.5);}
}
// limit for seconds
if(x==2){
if(y[2]<1){
y[2]=60;
wait(0.5);}
}

if(y[0]!=0 || y[1]!=0 || y[2]!=0){
y[x]--;
wait(0.5);
}
} // end if (down)

//=================== press left
if (Left){
if(x==0){
x=3;
wait(0.5);
} // end if

else{
x--;
wait(0.5);
} // end else
} // end if (left)

//=================== press right
if (Right){
if(x==3){
x=0;
wait(0.5);
} // end if

else{
x++;
wait(0.5);
} // end else
} // end if (right)

//=================== press joystic
if(x==3){
if(press==true){
time(y[0], y[1], y[2]);
}
}

} // end while loop
}

//==================================== set alarm function ============================================

void setAlarm(){
lcd.cls();
lcd.locate(0,0);
lcd.printf("----SET ALARM HERE--------");
wait(0.5);
}

//==================================== main function ============================================

int main() {
// start program
lcd.printf("Welcome");
wait(1);

// main
lcd.locate(0,0);
lcd.printf("Main Menu");
wait(1);

lcd.locate(1,0);

while(1){
// wait for input from user - joy stick button
int x;

//=================== press up
if (Up){
x=0;
lcd.cls();
lcd.locate(0,0);
lcd.printf("%s", menu[x]);
} // end if (up)

//=================== press down
if (Down){
x=1;
lcd.cls();
lcd.locate(0,0);
lcd.printf("%s", menu[x]);
} // end if (down)

if(press){
if(x==0){
setTime();
}

if(x==1){
setAlarm();
}
}

} // end while loop
} // end main

# data[].attributes.body
/* Arduino+esp8266 thingSpeak example
* Example name = "Write temperature and humidity to Thingspeak channel"
* Created by Ilias Lamprou
* Updated Oct 30 2016
*
* Download latest Virtuino android app from the link:https://play.google.com/store/apps/details?id=com.virtuino_automations.virtuino&hl
* Video tutorial link: https://youtu.be/4XEe0HY0j6k
* Contact address for questions or comments: iliaslampr@gmail.com
*/

// Code to use SoftwareSerial
#include <SoftwareSerial.h>
SoftwareSerial espSerial =  SoftwareSerial(2,3);      // arduino RX pin=2  arduino TX pin=3    connect the arduino RX pin to esp8266 module TX pin   -  connect the arduino TX pin to esp8266 module RX pin

#include <DHT.h>        //Attention: For new  DHT11 version  library you will need the Adafruit_Sensor library
//Download from here: https://github.com/adafruit/Adafruit_Sensor

#define DHTPIN 5                // Connect the signal pin of DHT22 sensor to digital pin 5
#define ldrPin A0   // analog output for LDR

#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

String apiKey = "SR1PXN9VTNFJ4YS0";     // replace with your channel's thingspeak WRITE API key

String ssid="Bruno";    // Wifi network SSID
String password ="abcde12345";  // Wifi network password

boolean DEBUG=true;

//======================================================================== showResponce
void showResponse(int waitTime){
long t=millis();
char c;
while (t+waitTime>millis()){
if (espSerial.available()){
c=espSerial.read();
if (DEBUG) Serial.print(c);
}
}

}

//========================================================================
boolean thingSpeakWrite(float value1, float value2, float value3){
String cmd = "AT+CIPSTART=\"TCP\",\"";                  // TCP connection
cmd += "184.106.153.149";                               // api.thingspeak.com
cmd += "\",80";
espSerial.println(cmd);
if (DEBUG) Serial.println(cmd);
if(espSerial.find("Error")){
if (DEBUG) Serial.println("AT+CIPSTART error");
return false;
}

String getStr = "GET /update?api_key=";   // prepare GET string
getStr += apiKey;

getStr +="&field1=";
getStr += String(value1);
getStr +="&field2=";
getStr += String(value2);

getStr += "&field3=";
getStr += String(value3);

// getStr +="&field4=";
// getStr += String(value4);
// ...
getStr += "\r\n\r\n";

// send data length
cmd = "AT+CIPSEND=";
cmd += String(getStr.length());
espSerial.println(cmd);
if (DEBUG)  Serial.println(cmd);

delay(100);
if(espSerial.find(">")){
espSerial.print(getStr);
if (DEBUG)  Serial.print(getStr);
}
else{
espSerial.println("AT+CIPCLOSE");
// alert user
if (DEBUG)   Serial.println("AT+CIPCLOSE");
return false;
}
return true;
}
//================================================================================ setup

//int ldrPin = 7;

void setup() {
DEBUG=true;           // enable debug serial

pinMode(ldrPin, INPUT);

Serial.begin(9600);

dht.begin();          // Start DHT sensor

espSerial.begin(9600);  // enable software serial
// Your esp8266 module's speed is probably at 115200.
// For this reason the first time set the speed to 115200 or to your esp8266 configured speed
// and upload. Then change to 9600 and upload again

//espSerial.println("AT+RST");         // Enable this line to reset the module;
//showResponse(1000);

//espSerial.println("AT+UART_CUR=9600,8,1,0,0");    // Enable this line to set esp8266 serial speed to 9600 bps
//showResponse(1000);

// start wifi conenction
Serial.println("Start");

espSerial.println("AT+CWMODE=1");   // set esp8266 as client
showResponse(5000);

espSerial.println("AT+CWJAP=\""+ssid+"\",\""+password+"\"");  // set your home router SSID and password
showResponse(5000);

if (DEBUG)  Serial.println("Setup completed");
}

// ====================================================================== loop
void loop() {

// Read sensor values

int ldrStatus = analogRead(ldrPin);

if (ldrStatus <= 520) {                               // ldr status bright>520, dark<=520
Serial.print("Its DARK, Turn on the LED : ");
Serial.println(ldrStatus);
}
else {
Serial.print("Its BRIGHT, Turn off the LED : ");
Serial.println(ldrStatus);
}

float f = ldrStatus;

float t = dht.readTemperature();
float h = dht.readHumidity();
if (isnan(t) || isnan(h)) {
if (DEBUG) Serial.println("Failed to read from DHT");
}
else {
if (DEBUG)  Serial.println("Temp="+String(t)+" *C");
if (DEBUG) Serial.println("Humidity="+String(h)+" %");
thingSpeakWrite(t,h,f);                                      // Write values to thingspeak
}

// thingspeak needs 15 sec delay between updates,
delay(20000);
}

# data[].attributes.body
/* Embedded Assignment Sem 2, 2018/2019

- Fikry
- Khalish
- Amirul
- Nakib
- Syakir

*/
#include "mbed.h"
#include "TextLCD.h"            // library for LCD Display

TextLCD lcd(p21, p22, p17, p18, p19, p20); // rs, e, d0, d1, d2, d3 pin out for LCD

DigitalIn press(p14);            // Joy stick button
BusIn Up(p15);                   // Joy stick
BusIn Down(p12);                 // Joy stick
BusIn Left(p13);                 // Joy stick
BusIn Right(p16);                // Joy stick

int y[3]={00,00,00};
char day[7][40] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};

// function for time
void time(){
int i = 0;
for(int m=0;m<7;m++){
for(int k=1;k<=12;k++){
for(int j=0;j<60;j++){
y[2] = 0; // reset seconds into 0

for(i=0;i<60;i++){
y[0] = k;
y[1] = j;

lcd.cls();
lcd.locate(0,0);                                // locate time
lcd.printf("%d : %d : %d",y[0],y[1],y[2]);      // print time

lcd.locate(0,1);                                // locate day
lcd.printf("%s",day[m]);                        // print day

y[2]++;

wait(1);
} // end for loop i (sec)
} // end for loop j (min)
} // end for loop k (hour)
} // end for loop m (days)
}

// main function
int main() {
// start program
lcd.printf("Press button to set time");

while(1){
// wait for input from user - joy stick button
if(press==true){
int x=0;

lcd.cls();
lcd.locate(0,0);
lcd.printf("%d:%d:%d",y[0],y[1],y[2]);      // locate time

wait(0.5);

while(1){
lcd.locate(0,0);
lcd.printf("%d:%d:%d",y[0],y[1],y[2]);      // locate time

lcd.locate(0,1);                            // locate text

// Operations by Joystic
if (Up){
lcd.printf("                 ");
lcd.locate(0,1);
lcd.printf("UP");

y[x]++;
wait(0.5);
} // end if (up)

if (Down){
lcd.printf("                 ");
lcd.locate(0,1);
lcd.printf("Down");

y[x]--;
wait(0.5);
} // end if (down)

if (Left){
if(x==0){
lcd.printf("                 ");
lcd.locate(0,1);
lcd.printf("Left");

x=2;
wait(0.5);
} // end if

else{
lcd.printf("                 ");
lcd.locate(0,1);
lcd.printf("Left");

x--;
wait(0.5);
} // end else
} // end if (left)

if (Right){
if(x==2){
lcd.printf("                 ");
lcd.locate(0,1);
lcd.printf("Right");

x=0;
wait(0.5);
} // end if

else{
lcd.printf("                 ");
lcd.locate(0,1);
lcd.printf("Right");

x++;
wait(0.5);
} // end else
} // end if (right)
} // end while loop
} // end if (user press)

else{
time();
}
} // end while loop
} // end main