Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: 4DGL-uLCD-SE EthernetInterface NTPClient mbed-rpc mbed-rtos mbed HTTPClient spxml
Diff: main.cpp
- Revision:
- 0:0a99e3fc2a46
- Child:
- 1:d7f65be2640d
diff -r 000000000000 -r 0a99e3fc2a46 main.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Sat Nov 15 19:36:44 2014 +0000
@@ -0,0 +1,216 @@
+// Includes
+#include "mbed.h"
+#include "NTPClient.h"
+#include "uLCD_4DGL.h"
+#include "EthernetInterface.h"
+#include <string>
+#include "mbed_rpc.h"
+#include "RPCCommand.h"
+#include "HTTPServer.h"
+#include "Formatter.h"
+#include "RequestHandler.h"
+#include "RPCType.h"
+#include "alarmContainer.h"
+
+// Defines
+#define SERVER_PORT 80 //HTTPSERVER
+
+// Global variables
+EthernetInterface eth;
+uLCD_4DGL uLCD(p28, p27, p29); // serial tx, serial rx, reset pin;
+NTPClient ntpClient;
+alarmModel _alarm;
+string dayOfWeek, month, dayNum, ampm;
+int hour, minute, sec;
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+DigitalOut led3(LED3);
+DigitalOut led4(LED4);
+
+// Function prototypes
+HTTPServer create_simple_server();
+HTTPServer create_interactive_server();
+void parse(char[], int*, char*);
+void thread_server(void const *args);
+void thread_display(void const *args);
+void thread_alarm(void const *args);
+
+int main() {
+
+ printf("Getting IP Address.. ");
+
+ uLCD.baudrate(2000000);
+ uLCD.cls();
+ uLCD.background_color(BLACK);
+ uLCD.textbackground_color(BLACK);
+ uLCD.locate(0,0);
+ uLCD.color(BLUE);
+ uLCD.text_height(2);
+ uLCD.text_width(2);
+ uLCD.printf("\n\nUniv Time\n Clock\n");
+ uLCD.text_height(1);
+ uLCD.text_width(1);
+ uLCD.printf(" Loading...\n");
+ uLCD.locate(0,0);
+
+ if(eth.init())
+ {
+ printf("Error while initializing the ethernet interface.\n");
+ return -1;
+ }
+ wait(5);
+ if(eth.connect())
+ {
+ printf("Error while starting the ethernet interface.\n");
+ return -1;
+ }
+
+ printf("IP Address is %s\n", eth.getIPAddress());
+
+ printf("Reading time..\n");
+ char* domainName="us.pool.ntp.org"; //SET TO DOMAIN NAME OF SERVER GETTING TIME FROM
+ //GETS THE TIME FROM THE SERVER
+ //setTime(DOMAIN_NAME,PORT_NUMBER,TIME_OUT)
+ //DOMAIN_NAME= domain name
+ //PORT NUMBER=port number (123 for NTP)
+ //TIME_OUT= timeout value for request
+ ntpClient.setTime(domainName,123,0x00005000);
+ printf("Time set.\n");
+
+ Thread t1(thread_server); //start thread_server
+ Thread t2(thread_display);
+ Thread t3(thread_alarm);
+
+ while(1){}
+}
+
+void thread_alarm(void const *args)
+{
+ while(1){
+ if(_alarm.alarmSet){
+ led1 = 1;
+ if(_alarm.hours == hour && _alarm.minutes == minute && _alarm.amPm == ampm){
+ led2 = 1;
+ led3 = 1;
+ led4 = 1;
+ }
+ }
+ Thread::wait(100);
+ }
+}
+
+void thread_display(void const *args){
+ time_t ctTime; //system time structure
+ uLCD.cls();
+ uLCD.locate(0,0);
+ uLCD.text_height(2);
+ uLCD.text_width(1);
+ char buffer[80]; //BUFFER TO HOLD FORMATTED TIME DATA
+ uLCD.textbackground_color(BLACK);
+
+ while (1) {
+
+ uLCD.locate(0,0);
+ uLCD.color(RED);
+ // loop and periodically update the LCD's time display
+ ctTime = time(NULL)-(3600*4); //TIME with offset for eastern time US
+
+ //FORMAT TIME FOR DISPLAY AND STORE FORMATTED RESULT IN BUFFER
+ strftime(buffer,80,"%a %b %d %T %p %z %Z",localtime(&ctTime));
+
+ int i=0;
+ char* chars_array = strtok(buffer, " :");
+ //printf("Chars array: \n");
+ while(chars_array)
+ {
+ switch(i){
+ case 0:
+ dayOfWeek = chars_array;
+ break;
+ case 1:
+ month = chars_array;
+ break;
+ case 2:
+ dayNum = chars_array;
+ break;
+ case 3:
+ //chars_array = chars_array - 1;
+ int hourTemp = atoi(chars_array);
+ hourTemp--; //Daylight savings
+ if(hourTemp > 12) hourTemp -= 12;
+ hour = hourTemp;
+ break;
+ case 4:
+ minute = atoi(chars_array);
+ break;
+ case 5:
+ sec = atoi(chars_array);
+ break;
+ case 6:
+ ampm = chars_array;
+ break;
+ }
+ i++;
+
+ chars_array = strtok(NULL, " :");
+ }
+
+ if(minute < 10 && sec < 10){ uLCD.printf(" %s, %s %s\n %d:0%d:0%d %s", dayOfWeek, month, dayNum, hour, minute, sec, ampm); }
+ else if(minute < 10){ uLCD.printf(" %s, %s %s\n %d:0%d:%d %s", dayOfWeek, month, dayNum, hour, minute, sec, ampm); }
+ else if (sec < 10){ uLCD.printf(" %s, %s %s\n %d:%d:0%d %s", dayOfWeek, month, dayNum, hour, minute, sec, ampm); }
+ else{ uLCD.printf(" %s, %s %s\n %d:%d:%d %s", dayOfWeek, month, dayNum, hour, minute, sec, ampm); }
+
+ if(_alarm.alarmSet){
+ uLCD.color(GREEN);
+ if(_alarm.minutes < 10){ uLCD.printf("\n\n Alarm:\n %d:0%d %s", _alarm.hours, _alarm.minutes, _alarm.amPm); }
+ else{ uLCD.printf("\n\n Alarm:\n %d:%d %s", _alarm.hours, _alarm.minutes, _alarm.amPm); }
+ }
+
+ Thread::wait(100);
+ }
+}
+
+void thread_server(void const *args)
+{
+ RPCType::instance().register_types();
+ HTTPServer srv = create_interactive_server();
+
+ if(!srv.init(SERVER_PORT))
+ {
+ eth.disconnect();
+ //return -1;
+ printf("Thread 1 error.\n");
+ }
+
+ srv.run();
+}
+
+HTTPServer create_simple_server()
+{
+ HTTPServer srv;
+ srv.add_request_handler("DELETE", new DeleteRequestHandler());
+ srv.add_request_handler("GET", new GetRequestHandler());
+ srv.add_request_handler("PUT", new PutRequestHandler());
+ return srv;
+}
+
+HTTPServer create_interactive_server()
+{
+ HTTPServer srv(new InteractiveHTMLFormatter());
+ srv.add_request_handler("GET", new ComplexRequestHandler());
+ return srv;
+}
+
+//SET FOR CSV FORMAT: NEEDS TO BE EDITED IF DIFFERENT FORMAT
+void parse(char buffer[], int *j, char *string) {
+//extracts next location string data item from buffer
+ int i=0;
+ for (i=0; i<=strlen(buffer); i++) { //TOTAL SIZE OF RETURNED DATA
+ if ((buffer[*j+i] == ',')||(buffer[*j+i] == '\0' )) { //IF comma or end of string
+ //comma is the string field delimiter
+ string[i]=0; //SETS END OF SRTRING TO 0
+ *j=*j+i+1; //UPDATES to 1 after comma seperated value
+ break;
+ } else string[i]=buffer[*j+i]; //Keep adding to the string
+ }
+}
\ No newline at end of file