/*
Edison Internet Calendar
This is the Arduino code part of the "Intel Edison Proxmity Talking Calendar"
2015 Ambrose Clarke
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
*/
#include //For the LCD
#include //For the LCD
#include // The SD library is used to read a temporary file created by python
//For the Sonar Detector
#define trigPin 7
#define echoPin A0
#define ledStillWorking 13
#define onTime 15000 //screen on for 15 secs
//future Expansion - how much time of resident inactivity, during daylight, before you want an warning email sent to you
#define inactivityConcern 4*60 //minutes
LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display
long tiScreenOff = 0;
int tiUpdateTime = 0; //internal counter for screen update
//Clear screen and Display two lines of text
void Screen(String sLine1, String sLine2)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(sLine1);
lcd.setCursor(0, 1);
lcd.print(sLine2);
LightOn();
}
//retrieve data from linux efficiently without writing to sd card
String GetTime()
{
FILE *fpipe; char abBuf[20] = {0};
if ( (fpipe = (FILE*)popen("date '+%a %d %b %I:%M'", "r")) ) {
fgets( abBuf, 17, fpipe);
pclose(fpipe);
}
String ti = String(abBuf);
return ti;
}
//Update Time on Line 1 - dont enable backlight
void UpdateTime()
{
//Screen("---","Retrieving...");
String ti = GetTime();
lcd.setCursor(0, 0);
lcd.print(ti);
}
//Update Line 2, the event line - and turn on light
void UpdateInfo(String sLine2)
{
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(sLine2);
LightOn();
}
//Turn on backlight for 15 seconds
void LightOn()
{
lcd.setBacklight(true);
tiScreenOff = millis() + onTime;
}
//Initialise the LCD
void setup_LCD()
{
lcd.init(); // initialize the lcd
lcd.init();
// Print a welcome message to the LCD.
Screen("Ambrose 2015", "Edison Calendar");
}
//Initialise the arduino app
void setup() {
Serial.begin (115200);
setup_LCD();
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(ledStillWorking, OUTPUT);
Serial.println("Start..");
UpdateTime();
LightOn();
delay(1000); //sonar settle time
}
//return the distance in cm
float getDistance()
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
float duration = pulseIn(echoPin, HIGH);
float distance = (duration / 2) / 29.1;
return distance;
}
//The Arduino Loop
void loop() {
//Blink LED13 to Show we Arduino code is running
digitalWrite(ledStillWorking, !digitalRead(ledStillWorking));
float distance = getDistance();
if (distance >= 200 || distance <= 0) {
Serial.println("Out of range");
}
else {
Serial.print(distance);
Serial.println(" cm");
if (distance < 100)
{ //Within 1m
//Double check! better to err on not having going off randomly
distance = getDistance();
if(distance > 0 && distance < 100)
{
UpdateInfo( "Reading Calendar");
UpdateTime(); //will be a pause - so make sure clock is correct
//This line useful for debugging - writes Python outtoput to serial console
//system("/usr/bin/python /home/root/pyc.py &> /dev/ttyGS0");
{ //retrieve data from python without writing to sd card
FILE *fpipe; char abBuf[20] = {0};
if ( (fpipe = (FILE*)popen("/usr/bin/python /home/root/pyc.py", "r")) ) {
fgets( abBuf, 17, fpipe);
pclose(fpipe);
}
String info = String(abBuf);
UpdateInfo( info );
}
}
}
}
//Turn off display after 15 seconds
if (tiScreenOff > 0 && tiScreenOff < millis())
{
lcd.setBacklight(false);
tiScreenOff = 0;
}
//Every 30 seconds will update display (without turning on light)
if (++tiUpdateTime >= 60)
{
tiUpdateTime= 0;
UpdateTime();
}
delay(500);
}