![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Alarm
Dependencies: mbed HTTPClient mbed-rtos 4DGL-uLCD-SE NTPClient EthernetInterface
alarm.h@1:2dcc0acbaa0b, 2020-12-17 (annotated)
- Committer:
- conormoloney
- Date:
- Thu Dec 17 18:49:31 2020 +0000
- Revision:
- 1:2dcc0acbaa0b
- Parent:
- 0:070d52cbaed0
project alarm with LCD
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
conormoloney | 0:070d52cbaed0 | 1 | |
conormoloney | 0:070d52cbaed0 | 2 | #include "mbed.h" |
conormoloney | 0:070d52cbaed0 | 3 | #include "EthernetInterface.h" |
conormoloney | 0:070d52cbaed0 | 4 | #include "NTPClient.h" |
conormoloney | 1:2dcc0acbaa0b | 5 | #include "uLCD_4DGL.h" |
conormoloney | 0:070d52cbaed0 | 6 | |
conormoloney | 0:070d52cbaed0 | 7 | #define VOLUME 1 |
conormoloney | 0:070d52cbaed0 | 8 | |
conormoloney | 1:2dcc0acbaa0b | 9 | //Serial Monitor |
conormoloney | 0:070d52cbaed0 | 10 | Serial pc(USBTX, USBRX); |
conormoloney | 0:070d52cbaed0 | 11 | |
conormoloney | 0:070d52cbaed0 | 12 | // Parameters |
conormoloney | 0:070d52cbaed0 | 13 | int port_number = 123; |
conormoloney | 0:070d52cbaed0 | 14 | |
conormoloney | 0:070d52cbaed0 | 15 | // Networking |
conormoloney | 0:070d52cbaed0 | 16 | EthernetInterface eth; |
conormoloney | 0:070d52cbaed0 | 17 | NTPClient ntpClient; |
conormoloney | 0:070d52cbaed0 | 18 | |
conormoloney | 0:070d52cbaed0 | 19 | //Pins |
conormoloney | 0:070d52cbaed0 | 20 | PwmOut pwm_pin(p21); |
conormoloney | 1:2dcc0acbaa0b | 21 | DigitalIn button(p12); |
conormoloney | 0:070d52cbaed0 | 22 | BusOut LEDS(LED1,LED2,LED3,LED4); |
conormoloney | 1:2dcc0acbaa0b | 23 | uLCD_4DGL uLCD(p9,p10,p11); |
conormoloney | 0:070d52cbaed0 | 24 | |
conormoloney | 0:070d52cbaed0 | 25 | // Plays a sound with the defined frequency, duration, and volume |
conormoloney | 0:070d52cbaed0 | 26 | void playNote(float frequency, float duration, float volume) { |
conormoloney | 0:070d52cbaed0 | 27 | pwm_pin.period(1.0/frequency); |
conormoloney | 0:070d52cbaed0 | 28 | pwm_pin = volume/2.0; |
conormoloney | 0:070d52cbaed0 | 29 | wait(duration); |
conormoloney | 0:070d52cbaed0 | 30 | pwm_pin = 0.0; |
conormoloney | 0:070d52cbaed0 | 31 | } |
conormoloney | 0:070d52cbaed0 | 32 | |
conormoloney | 0:070d52cbaed0 | 33 | void soundalarm() |
conormoloney | 0:070d52cbaed0 | 34 | { |
conormoloney | 0:070d52cbaed0 | 35 | time_t ctTime; |
conormoloney | 0:070d52cbaed0 | 36 | eth.init(); |
conormoloney | 0:070d52cbaed0 | 37 | eth.connect(); |
conormoloney | 0:070d52cbaed0 | 38 | |
conormoloney | 0:070d52cbaed0 | 39 | pc.printf("IP address is \n%s\n\n",eth.getIPAddress()); |
conormoloney | 0:070d52cbaed0 | 40 | wait(1); |
conormoloney | 0:070d52cbaed0 | 41 | |
conormoloney | 0:070d52cbaed0 | 42 | char* domainName="0.uk.pool.ntp.org"; |
conormoloney | 1:2dcc0acbaa0b | 43 | //Initilaise LCD |
conormoloney | 1:2dcc0acbaa0b | 44 | uLCD.baudrate(115200); |
conormoloney | 0:070d52cbaed0 | 45 | |
conormoloney | 0:070d52cbaed0 | 46 | // Read time from server |
conormoloney | 0:070d52cbaed0 | 47 | pc.printf("Reading time...\n\r"); |
conormoloney | 0:070d52cbaed0 | 48 | ntpClient.setTime(domainName,123,0x00005000); |
conormoloney | 1:2dcc0acbaa0b | 49 | //Buffers for holding time when the alarm begins and ends |
conormoloney | 1:2dcc0acbaa0b | 50 | char buffer1[80]; |
conormoloney | 1:2dcc0acbaa0b | 51 | char buffer2[80]; |
conormoloney | 0:070d52cbaed0 | 52 | wait(2); |
conormoloney | 0:070d52cbaed0 | 53 | eth.disconnect(); |
conormoloney | 0:070d52cbaed0 | 54 | ctTime = time(NULL); |
conormoloney | 1:2dcc0acbaa0b | 55 | strftime(buffer1, 80, " %a %b %d\n %T %p %z\n %Z\n", \ |
conormoloney | 0:070d52cbaed0 | 56 | localtime(&ctTime)); |
conormoloney | 0:070d52cbaed0 | 57 | // Loop |
conormoloney | 0:070d52cbaed0 | 58 | while((button==1)) |
conormoloney | 0:070d52cbaed0 | 59 | { |
conormoloney | 0:070d52cbaed0 | 60 | playNote(960,1.2,VOLUME); |
conormoloney | 0:070d52cbaed0 | 61 | LEDS=0x0F; |
conormoloney | 0:070d52cbaed0 | 62 | wait(0.1); |
conormoloney | 0:070d52cbaed0 | 63 | playNote(770,1.2,VOLUME); |
conormoloney | 0:070d52cbaed0 | 64 | LEDS=0; |
conormoloney | 0:070d52cbaed0 | 65 | } |
conormoloney | 1:2dcc0acbaa0b | 66 | uLCD.printf("Alarm Began On:\n"); |
conormoloney | 1:2dcc0acbaa0b | 67 | uLCD.printf(" UTC/GMT:\n%s", buffer1); |
conormoloney | 1:2dcc0acbaa0b | 68 | // pc.printf("Alarm stopped on\n"); |
conormoloney | 0:070d52cbaed0 | 69 | ctTime = time(NULL); |
conormoloney | 1:2dcc0acbaa0b | 70 | strftime(buffer2, 80, " %a %b %d\n %T %p %z\n %Z\n", \ |
conormoloney | 0:070d52cbaed0 | 71 | localtime(&ctTime)); |
conormoloney | 1:2dcc0acbaa0b | 72 | uLCD.printf("Alarm Stopped On:\n"); |
conormoloney | 1:2dcc0acbaa0b | 73 | uLCD.printf(" UTC/GMT:\n%s", buffer2); |
conormoloney | 1:2dcc0acbaa0b | 74 | wait(30); |
conormoloney | 1:2dcc0acbaa0b | 75 | uLCD.cls(); |
conormoloney | 0:070d52cbaed0 | 76 | } |
conormoloney | 0:070d52cbaed0 | 77 | |
conormoloney | 0:070d52cbaed0 | 78 | |
conormoloney | 0:070d52cbaed0 | 79 | |
conormoloney | 0:070d52cbaed0 | 80 | |
conormoloney | 0:070d52cbaed0 | 81 | |
conormoloney | 0:070d52cbaed0 | 82 |