V1
Dependencies: simple-mbed-client
Revision 1:bb3336b7e953, committed 2017-03-07
- Comitter:
- johnlynam2014
- Date:
- Tue Mar 07 12:57:42 2017 +0000
- Parent:
- 0:94dec0f696e0
- Commit message:
- Mbed_1st_Commit_V1
Changed in this revision
diff -r 94dec0f696e0 -r bb3336b7e953 function.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/function.h Tue Mar 07 12:57:42 2017 +0000 @@ -0,0 +1,126 @@ +#include "mbed.h" +#include "rtos.h" +#include "config.h" +#include "security.h" +#include "easy-connect.h" +#include "simple-mbed-client.h" +#include "hcsr04.h" + +// Permanent statuses (set by led/0/permanent_status) +#define STATUS_NONE 0 +#define STATUS_ON 1 +#define STATUS_OFF 2 + + + + +// Declare our peripherals here +DigitalOut red(LED_RED); +DigitalOut green(LED_GREEN); +DigitalOut blue(LED_BLUE); + + +//HCSR04 sensor(D12, D11); +//set D12 = trigger D11 = echo +DigitalOut trig = D12; +DigitalIn echo = D11; +Timer timer; +long duration,distance_cm; + +//return distance in cm +long distance(); +long echo_duration(); +long distanceDetect(); + + +void run() +{ + SimpleMbedClient client; + + SimpleResourceString name = client.define_resource("device/0/name", "jan", M2MBase::GET_PUT_ALLOWED, true);//User name +SimpleResourceString sensorName = client.define_resource("device/0/sensorName", "jan", M2MBase::GET_PUT_ALLOWED, true);//Module Name +SimpleResourceInt UltraSonicData = client.define_resource("device/0/USdata", 0, M2MBase::GET_ALLOWED);//UltraSonicData +//SimpleResourceInt pirCount = client.define_resource("pir/0/count", 0, M2MBase::GET_ALLOWED); + + + red=1; + green=1; + blue=1; + //printf("\r\nMAIN\n\r"); + sensorName = "Front Window 2"; + name = "John Lynam"; + distance(); + UltraSonicData = distance_cm; + //return distance_cm; + + +} + + +//dictance +long distance() +{ + + //printf("in distance function\r\n"); + duration = echo_duration(); + distance_cm = (duration/2)/29.1; + if(distance_cm < 20) + { + distanceDetect(); + } + else + { + + + } + printf("end distance function\r\n"); + return distance_cm; + +} + +long echo_duration() +{ + //printf("in echo duration function\r\n"); + timer.reset(); //reset timer + trig=1; // trigger low + wait_us(2); // wait + trig=0; // trigger high + wait_us(10); + trig=1; // trigger low + while(!echo); // start pulseIN + timer.start(); + while(echo); + timer.stop(); + //printf("end echo duration function\r\n"); + return timer.read_us(); + +} + +long distanceDetect() +{ + DigitalOut led(LED_RED); + DigitalOut led2(LED_BLUE); + //an action to demo distance change + led = !led; // toggle led + wait(0.2f); + led = !led2; // toggle led + wait(0.2f); + printf("Distance change detected: %d", distance_cm); + printf("\n\r"); + return distance_cm; + +} + + +// Registered callback for mbed Client +DigitalOut statusLed(LED1, 1); + +void registered() +{ + printf("Welcome John Lynam"); + statusLed = 0; +} + + + +
diff -r 94dec0f696e0 -r bb3336b7e953 hcsr04.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/hcsr04.cpp Tue Mar 07 12:57:42 2017 +0000 @@ -0,0 +1,30 @@ +#include "hcsr04.h" +#include "mbed.h" +/* +*HCSR04.cpp +*/ +HCSR04::HCSR04(PinName t, PinName e) : trig(t), echo(e) {} + long HCSR04::echo_duration() { + + timer.reset(); //reset timer + trig=0; // trigger low + wait_us(2); // wait + trig=1; // trigger high + wait_us(10); + trig=0; // trigger low + while(!echo); // start pulseIN + timer.start(); + while(echo); + timer.stop(); + return timer.read_us(); + +} + +//return distance in cm +long HCSR04::distance(){ + duration = echo_duration(); + distance_cm = (duration/2)/29.1; + return distance_cm; + +} +
diff -r 94dec0f696e0 -r bb3336b7e953 hcsr04.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/hcsr04.h Tue Mar 07 12:57:42 2017 +0000 @@ -0,0 +1,20 @@ +#ifndef hcsr04_H +#define hcsr04_H +#include "mbed.h" + + + +class HCSR04 { + public: + HCSR04(PinName t, PinName e); + long echo_duration(); + long distance(); + + private: + DigitalOut trig; + DigitalIn echo; + Timer timer; + long duration,distance_cm; +}; + +#endif
diff -r 94dec0f696e0 -r bb3336b7e953 main.cpp --- a/main.cpp Fri Feb 24 15:13:38 2017 +0000 +++ b/main.cpp Tue Mar 07 12:57:42 2017 +0000 @@ -1,87 +1,30 @@ #include "mbed.h" #include "rtos.h" -#include "config.h" +//#include "config.h" #include "security.h" #include "easy-connect.h" #include "simple-mbed-client.h" - -SimpleMbedClient client; - -// Declare our peripherals here -/*PwmOut red(LED_RED); -PwmOut green(LED_GREEN); -PwmOut blue(LED_BLUE);*/ -DigitalOut red(LED_RED); -DigitalOut green(LED_GREEN); -DigitalOut blue(LED_BLUE); - -InterruptIn pir(SW2); - -// We need a way to signal from an interrupt context -> main thread, use a Semaphore for it... -Semaphore updates(0); - -// YOUR CODE HERE -// Fwd declaration +#include "hcsr04.h" +#include "function.h" -// Variable that holds whether the light is on because the PIR sensor triggered (and timeout didn't happen yet) -bool ledOnBecauseOfPir = false; - -// Timeout based on led/0/timeout, disables the light after a set interval -Timeout pirTimeout; - -// Permanent statuses (set by led/0/permanent_status) -#define STATUS_NONE 0 -#define STATUS_ON 1 -#define STATUS_OFF 2 - - - -// Here are our resources: - -SimpleResourceString name = client.define_resource("device/0/name", "jan", M2MBase::GET_PUT_ALLOWED, true);//User name -SimpleResourceString sensorName = client.define_resource("device/0/sensorName", "jan", M2MBase::GET_PUT_ALLOWED, true);//Module Name -SimpleResourceInt UltraSonicData = client.define_resource("device/0/USdata", 0, M2MBase::GET_ALLOWED);//UltraSonicData -//SimpleResourceInt pirCount = client.define_resource("pir/0/count", 0, M2MBase::GET_ALLOWED); + SimpleMbedClient client; -// Registered callback for mbed Client -DigitalOut statusLed(LED1, 1); -void registered() { - printf("Welcome John Lynam"); - statusLed = 0; -} - -int main(int, char**) { - - sensorName = "Front Window 2"; - name = "John Lynam"; - UltraSonicData = 20; - /* - int i; - for( i = 0; i < 20; i++) +int main(int, char**) +{ + run(); + + NetworkInterface* network = easy_connect(true); + if (!network) { - wait(0.5); - i = UltraSonicData; - }*/ - - - red=1; - green=1; - blue=1; - + printf("Connect to internet failed... See serial output.\n"); + return 1; + } - NetworkInterface* network = easy_connect(true); - if (!network) { - printf("Connect to internet failed... See serial output.\n"); - return 1; - } - - struct MbedClientOptions options = client.get_default_options(); - options.DeviceType = "Wifi_Sonic_Test"; - if (!client.setup(options, network)) { - printf("Setting up mbed_client failed...\n"); - return 1; - } - - client.on_registered(®istered); - + struct MbedClientOptions options = client.get_default_options(); + options.DeviceType = "Wifi_Sonic_Test"; + if (!client.setup(options, network)) + { + printf("Setting up mbed_client failed...\n"); + return 1; + } } \ No newline at end of file
diff -r 94dec0f696e0 -r bb3336b7e953 mbed_app.json --- a/mbed_app.json Fri Feb 24 15:13:38 2017 +0000 +++ b/mbed_app.json Tue Mar 07 12:57:42 2017 +0000 @@ -13,7 +13,7 @@ "value": "D0" }, "esp8266-ssid": { - "value": "\"iPhone\"" + "value": "\"john\"" }, "esp8266-password": { "value": "\"qwerty12\""
diff -r 94dec0f696e0 -r bb3336b7e953 security.h --- a/security.h Fri Feb 24 15:13:38 2017 +0000 +++ b/security.h Tue Mar 07 12:57:42 2017 +0000 @@ -42,7 +42,7 @@ #define MBED_DOMAIN "5a6da97a-7f55-4e9e-aa2e-a0ef5a5cc1f3" -#define MBED_ENDPOINT_NAME "079a94b6-1f5a-48bc-aaec-94f0bc2ebe5d" +#define MBED_ENDPOINT_NAME "7a865bf0-f5eb-486a-93f7-8adeb29cdbd4" @@ -72,25 +72,25 @@ const uint8_t CERT[] = "-----BEGIN CERTIFICATE-----\r\n" -"MIIBzzCCAXOgAwIBAgIEFV92YTAMBggqhkjOPQQDAgUAMDkxCzAJBgNVBAYTAkZ\r\n" +"MIIBzzCCAXOgAwIBAgIEMF7OhTAMBggqhkjOPQQDAgUAMDkxCzAJBgNVBAYTAkZ\r\n" "JMQwwCgYDVQQKDANBUk0xHDAaBgNVBAMME21iZWQtY29ubmVjdG9yLTIwMTgwHh\r\n" -"cNMTcwMTI2MTIwNTQxWhcNMTgxMjMxMDYwMDAwWjCBoTFSMFAGA1UEAxNJNWE2Z\r\n" +"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CCqGSM49AwEHBG0wawIBAQQgzw+5xH0IIhMoD4ji\r\n" +"MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgsfpaV4V7UFFFsitr\r\n" -"ZZAGqQGNo1Hx7BGcPazAJs229V6hRANCAAS4p/z65OJ7fyNpxE6kOffVs9+g0c9i\r\n" +"6rA3vqqjzgzti8uUtNZxpDerLCChRANCAARpIToh6BAj1fS6Ck1EkmX58+pBTu2Q\r\n" -"2HVDBSjjBG9xkAuoDXZjTPYqX9IGeCSp2qvbpnSlCxOcSHbkKymy0i1r\r\n" +"7qev9tzafmbOogn/UJg9Zw0eao3hMTirzYMdFfWUQgJSBx47uUhv3NHZ\r\n" "-----END PRIVATE KEY-----\r\n";