Geolocation through Ethernet

Dependencies:   mbed HTTPClient MMA8452Q mbed-rtos 4DGL-uLCD-SE NTPClient SDFileSystem1 EthernetInterface

Committer:
stephenb
Date:
Fri Dec 18 19:36:51 2020 +0000
Revision:
3:65349fe42061
First Final Version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
stephenb 3:65349fe42061 1 #include "mbed.h"
stephenb 3:65349fe42061 2 #include "EthernetInterface.h"
stephenb 3:65349fe42061 3 #include "NTPClient.h"
stephenb 3:65349fe42061 4 #include "uLCD_4DGL.h"
stephenb 3:65349fe42061 5
stephenb 3:65349fe42061 6 #define VOLUME 1
stephenb 3:65349fe42061 7 // Parameters
stephenb 3:65349fe42061 8 int port_number = 123;
stephenb 3:65349fe42061 9
stephenb 3:65349fe42061 10 // Networking
stephenb 3:65349fe42061 11 EthernetInterface eth;
stephenb 3:65349fe42061 12 NTPClient ntpClient;
stephenb 3:65349fe42061 13
stephenb 3:65349fe42061 14 //Pins
stephenb 3:65349fe42061 15 PwmOut pwm_pin(p21);
stephenb 3:65349fe42061 16 DigitalIn button(p12);
stephenb 3:65349fe42061 17 BusOut LEDS(LED1,LED2,LED3,LED4);
stephenb 3:65349fe42061 18 uLCD_4DGL uLCD(p9,p10,p11);
stephenb 3:65349fe42061 19
stephenb 3:65349fe42061 20 // Plays a sound with the defined frequency, duration, and volume
stephenb 3:65349fe42061 21 void playNote(float frequency, float duration, float volume) {
stephenb 3:65349fe42061 22 pwm_pin.period(1.0/frequency);
stephenb 3:65349fe42061 23 pwm_pin = volume/2.0;
stephenb 3:65349fe42061 24 wait(duration);
stephenb 3:65349fe42061 25 pwm_pin = 0.0;
stephenb 3:65349fe42061 26 }
stephenb 3:65349fe42061 27
stephenb 3:65349fe42061 28 void soundalarm()
stephenb 3:65349fe42061 29 {
stephenb 3:65349fe42061 30 time_t ctTime;
stephenb 3:65349fe42061 31 eth.init();
stephenb 3:65349fe42061 32 eth.connect();
stephenb 3:65349fe42061 33
stephenb 3:65349fe42061 34 char* domainName="0.uk.pool.ntp.org";
stephenb 3:65349fe42061 35 //Initilaise LCD
stephenb 3:65349fe42061 36 uLCD.baudrate(115200);
stephenb 3:65349fe42061 37
stephenb 3:65349fe42061 38 // Read time from server
stephenb 3:65349fe42061 39 ntpClient.setTime(domainName,123,0x00005000);
stephenb 3:65349fe42061 40 //Buffers for holding time when the alarm begins and ends
stephenb 3:65349fe42061 41 char buffer1[80];
stephenb 3:65349fe42061 42 char buffer2[80];
stephenb 3:65349fe42061 43 wait(2);
stephenb 3:65349fe42061 44 eth.disconnect();
stephenb 3:65349fe42061 45 ctTime = time(NULL);
stephenb 3:65349fe42061 46 strftime(buffer1, 80, " %a %b %d\n %T %p %z\n %Z\n", \
stephenb 3:65349fe42061 47 localtime(&ctTime));
stephenb 3:65349fe42061 48 // Loop
stephenb 3:65349fe42061 49 while((button==1))
stephenb 3:65349fe42061 50 {
stephenb 3:65349fe42061 51 playNote(960,1.2,VOLUME);
stephenb 3:65349fe42061 52 LEDS=0x0F;
stephenb 3:65349fe42061 53 wait(0.1);
stephenb 3:65349fe42061 54 playNote(770,1.2,VOLUME);
stephenb 3:65349fe42061 55 LEDS=0;
stephenb 3:65349fe42061 56 }
stephenb 3:65349fe42061 57 uLCD.locate(0,12);
stephenb 3:65349fe42061 58 uLCD.printf("Alarm Began On:\n");
stephenb 3:65349fe42061 59 uLCD.printf(" UTC/GMT:\n%s", buffer1);
stephenb 3:65349fe42061 60 ctTime = time(NULL);
stephenb 3:65349fe42061 61 strftime(buffer2, 80, " %a %b %d\n %T %p %z\n %Z\n", \
stephenb 3:65349fe42061 62 localtime(&ctTime));
stephenb 3:65349fe42061 63 uLCD.printf("Alarm Stopped On:\n");
stephenb 3:65349fe42061 64 uLCD.printf(" UTC/GMT:\n%s", buffer2);
stephenb 3:65349fe42061 65 wait(30);
stephenb 3:65349fe42061 66 uLCD.cls();
stephenb 3:65349fe42061 67 }
stephenb 3:65349fe42061 68
stephenb 3:65349fe42061 69
stephenb 3:65349fe42061 70
stephenb 3:65349fe42061 71
stephenb 3:65349fe42061 72
stephenb 3:65349fe42061 73