How to measure water in a reservoir
Dependencies: Cayenne-MQTT-mbed Cayenne-LPP
main.cpp@0:7bfeb237e600, 2019-03-08 (annotated)
- Committer:
- wamae
- Date:
- Fri Mar 08 11:34:56 2019 +0000
- Revision:
- 0:7bfeb237e600
working code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wamae | 0:7bfeb237e600 | 1 | #include "MQTTTimer.h" |
wamae | 0:7bfeb237e600 | 2 | #include "CayenneMQTTClient.h" |
wamae | 0:7bfeb237e600 | 3 | #include "MQTTNetworkIDW01M1.h" |
wamae | 0:7bfeb237e600 | 4 | #include "mbed.h" |
wamae | 0:7bfeb237e600 | 5 | #include "lorawan_network.h" |
wamae | 0:7bfeb237e600 | 6 | #include "CayenneLPP.h" |
wamae | 0:7bfeb237e600 | 7 | #include "mbed_events.h" |
wamae | 0:7bfeb237e600 | 8 | #include "mbed_trace.h" |
wamae | 0:7bfeb237e600 | 9 | #include "lora_radio_helper.h" |
wamae | 0:7bfeb237e600 | 10 | #include "SX1276_LoRaRadio.h" |
wamae | 0:7bfeb237e600 | 11 | #include "LoRaWANInterface.h" |
wamae | 0:7bfeb237e600 | 12 | #include "hcsr04.h" |
wamae | 0:7bfeb237e600 | 13 | #include "standby.h" |
wamae | 0:7bfeb237e600 | 14 | |
wamae | 0:7bfeb237e600 | 15 | #define STANDBY_TIME_S 1* 60 |
wamae | 0:7bfeb237e600 | 16 | |
wamae | 0:7bfeb237e600 | 17 | extern EventQueue ev_queue; |
wamae | 0:7bfeb237e600 | 18 | |
wamae | 0:7bfeb237e600 | 19 | static uint32_t DEV_ADDR_1 = 0x260118FA; |
wamae | 0:7bfeb237e600 | 20 | static uint8_t NWK_S_KEY_1[] = { 0x5D, 0xD5, 0xC9, 0x84, 0xB9, 0xCE, 0x26, 0x05, 0xB7, 0x61, 0xBF, 0x9F, 0x3C, 0x2D, 0x4E, 0x14 }; |
wamae | 0:7bfeb237e600 | 21 | static uint8_t APP_S_KEY_1[] = { 0xF0, 0xE4, 0xA1, 0xEB, 0x1F, 0xB5, 0x6E, 0xBA, 0x31, 0x69, 0x80, 0x9D, 0x85, 0x92, 0x68, 0xB4 }; |
wamae | 0:7bfeb237e600 | 22 | |
wamae | 0:7bfeb237e600 | 23 | //static uint32_t DEV_ADDR_2 = 0x2601114C; |
wamae | 0:7bfeb237e600 | 24 | //static uint8_t NWK_S_KEY_2[] = { 0xDB, 0xC0, 0xD7, 0x32, 0xF8, 0x41, 0xE7, 0xF5, 0xB9, 0xCB, 0x54, 0x6F, 0x49, 0xD5, 0x48, 0x25 }; |
wamae | 0:7bfeb237e600 | 25 | //static uint8_t APP_S_KEY_2[] = { 0x64, 0x6C, 0xF0, 0xA4, 0x9C, 0x6F, 0x30, 0xF4, 0x0C, 0x80, 0x3E, 0x4A, 0x50, 0xC3, 0x77, 0x53 }; |
wamae | 0:7bfeb237e600 | 26 | HCSR04 sensor(D4, D3); |
wamae | 0:7bfeb237e600 | 27 | //DigitalOut led3(LED3); |
wamae | 0:7bfeb237e600 | 28 | |
wamae | 0:7bfeb237e600 | 29 | // WiFi network info. |
wamae | 0:7bfeb237e600 | 30 | //char* ssid = "Jeff_HomeAP_2EX"; |
wamae | 0:7bfeb237e600 | 31 | //char* wifiPassword = "iots2480"; |
wamae | 0:7bfeb237e600 | 32 | |
wamae | 0:7bfeb237e600 | 33 | // Cayenne authentication info. This should be obtained from the Cayenne Dashboard. |
wamae | 0:7bfeb237e600 | 34 | //char* username = "0cb35a60-7fbc-11e8-890a-c1153a0b021e"; |
wamae | 0:7bfeb237e600 | 35 | //char* password = "8f7cb3b94334a5a0a5cbe11b068f4630b6c8d478"; |
wamae | 0:7bfeb237e600 | 36 | //char* clientID = "d8504b50-b0ff-11e8-85ea-f10189fd2756"; |
wamae | 0:7bfeb237e600 | 37 | |
wamae | 0:7bfeb237e600 | 38 | |
wamae | 0:7bfeb237e600 | 39 | //global variable |
wamae | 0:7bfeb237e600 | 40 | long distance; |
wamae | 0:7bfeb237e600 | 41 | bool dist_updated = false; |
wamae | 0:7bfeb237e600 | 42 | float volume; |
wamae | 0:7bfeb237e600 | 43 | |
wamae | 0:7bfeb237e600 | 44 | //void dust_sensor_cb(int lpo, float ratio, float concentration) { |
wamae | 0:7bfeb237e600 | 45 | // dust_concentration = concentration; |
wamae | 0:7bfeb237e600 | 46 | // dust_updated = true; |
wamae | 0:7bfeb237e600 | 47 | //} |
wamae | 0:7bfeb237e600 | 48 | |
wamae | 0:7bfeb237e600 | 49 | void dist_measure(){ |
wamae | 0:7bfeb237e600 | 50 | sensor.start(); |
wamae | 0:7bfeb237e600 | 51 | wait_ms(100); |
wamae | 0:7bfeb237e600 | 52 | distance=sensor.get_dist_cm(); |
wamae | 0:7bfeb237e600 | 53 | volume = 6*6*(2-(distance*0.01)); |
wamae | 0:7bfeb237e600 | 54 | dist_updated = true; |
wamae | 0:7bfeb237e600 | 55 | |
wamae | 0:7bfeb237e600 | 56 | } |
wamae | 0:7bfeb237e600 | 57 | |
wamae | 0:7bfeb237e600 | 58 | void check_for_updated_dist() { |
wamae | 0:7bfeb237e600 | 59 | if (dist_updated){ |
wamae | 0:7bfeb237e600 | 60 | dist_updated = false ; |
wamae | 0:7bfeb237e600 | 61 | printf("Measure Distance = %ld ",distance); |
wamae | 0:7bfeb237e600 | 62 | printf("Volume=%3.0f\n", volume); |
wamae | 0:7bfeb237e600 | 63 | |
wamae | 0:7bfeb237e600 | 64 | CayenneLPP payload(50); |
wamae | 0:7bfeb237e600 | 65 | float volume_value = volume; |
wamae | 0:7bfeb237e600 | 66 | printf("Volume_value=%f\n", volume_value); |
wamae | 0:7bfeb237e600 | 67 | payload.addAnalogInput(4, volume_value); |
wamae | 0:7bfeb237e600 | 68 | |
wamae | 0:7bfeb237e600 | 69 | if (!lorawan_send(&payload)){ |
wamae | 0:7bfeb237e600 | 70 | // delete distance; |
wamae | 0:7bfeb237e600 | 71 | standby(STANDBY_TIME_S); |
wamae | 0:7bfeb237e600 | 72 | } |
wamae | 0:7bfeb237e600 | 73 | } |
wamae | 0:7bfeb237e600 | 74 | } |
wamae | 0:7bfeb237e600 | 75 | |
wamae | 0:7bfeb237e600 | 76 | static void lora_event_handler(lorawan_event_t event) { |
wamae | 0:7bfeb237e600 | 77 | switch (event) { |
wamae | 0:7bfeb237e600 | 78 | case CONNECTED: |
wamae | 0:7bfeb237e600 | 79 | printf("[LNWK][INFO] Connection - Successful\n"); |
wamae | 0:7bfeb237e600 | 80 | break; |
wamae | 0:7bfeb237e600 | 81 | case DISCONNECTED: |
wamae | 0:7bfeb237e600 | 82 | ev_queue.break_dispatch(); |
wamae | 0:7bfeb237e600 | 83 | printf("[LNWK][INFO] Disconnected Successfully\n"); |
wamae | 0:7bfeb237e600 | 84 | break; |
wamae | 0:7bfeb237e600 | 85 | case TX_DONE: |
wamae | 0:7bfeb237e600 | 86 | printf("[LNWK][INFO] Message Sent to Network Server\n"); |
wamae | 0:7bfeb237e600 | 87 | |
wamae | 0:7bfeb237e600 | 88 | // delete distance; |
wamae | 0:7bfeb237e600 | 89 | standby(STANDBY_TIME_S); |
wamae | 0:7bfeb237e600 | 90 | break; |
wamae | 0:7bfeb237e600 | 91 | case TX_TIMEOUT: |
wamae | 0:7bfeb237e600 | 92 | case TX_ERROR: |
wamae | 0:7bfeb237e600 | 93 | case TX_CRYPTO_ERROR: |
wamae | 0:7bfeb237e600 | 94 | case TX_SCHEDULING_ERROR: |
wamae | 0:7bfeb237e600 | 95 | printf("[LNWK][INFO] Transmission Error - EventCode = %d\n", event); |
wamae | 0:7bfeb237e600 | 96 | |
wamae | 0:7bfeb237e600 | 97 | |
wamae | 0:7bfeb237e600 | 98 | //delete distance; |
wamae | 0:7bfeb237e600 | 99 | standby(STANDBY_TIME_S); |
wamae | 0:7bfeb237e600 | 100 | break; |
wamae | 0:7bfeb237e600 | 101 | case RX_DONE: |
wamae | 0:7bfeb237e600 | 102 | printf("[LNWK][INFO] Received message from Network Server\n"); |
wamae | 0:7bfeb237e600 | 103 | receive_message(); |
wamae | 0:7bfeb237e600 | 104 | break; |
wamae | 0:7bfeb237e600 | 105 | case RX_TIMEOUT: |
wamae | 0:7bfeb237e600 | 106 | case RX_ERROR: |
wamae | 0:7bfeb237e600 | 107 | printf("[LNWK][INFO] Error in reception - Code = %d\n", event); |
wamae | 0:7bfeb237e600 | 108 | break; |
wamae | 0:7bfeb237e600 | 109 | case JOIN_FAILURE: |
wamae | 0:7bfeb237e600 | 110 | printf("[LNWK][INFO] ABP Failed - Check Keys\n"); |
wamae | 0:7bfeb237e600 | 111 | break; |
wamae | 0:7bfeb237e600 | 112 | default: |
wamae | 0:7bfeb237e600 | 113 | MBED_ASSERT("Unknown Event"); |
wamae | 0:7bfeb237e600 | 114 | } |
wamae | 0:7bfeb237e600 | 115 | } |
wamae | 0:7bfeb237e600 | 116 | |
wamae | 0:7bfeb237e600 | 117 | |
wamae | 0:7bfeb237e600 | 118 | int main() { |
wamae | 0:7bfeb237e600 | 119 | set_time(0); |
wamae | 0:7bfeb237e600 | 120 | |
wamae | 0:7bfeb237e600 | 121 | printf("=========================================\n"); |
wamae | 0:7bfeb237e600 | 122 | printf(" Water Level Monitoring System \n"); |
wamae | 0:7bfeb237e600 | 123 | printf("=========================================\n"); |
wamae | 0:7bfeb237e600 | 124 | |
wamae | 0:7bfeb237e600 | 125 | lorawan_setup(DEV_ADDR_1, NWK_S_KEY_1, APP_S_KEY_1, lora_event_handler); |
wamae | 0:7bfeb237e600 | 126 | |
wamae | 0:7bfeb237e600 | 127 | printf("Measuring Distance...\n"); |
wamae | 0:7bfeb237e600 | 128 | |
wamae | 0:7bfeb237e600 | 129 | //immediately measure the distance |
wamae | 0:7bfeb237e600 | 130 | sensor.start(); |
wamae | 0:7bfeb237e600 | 131 | wait_ms(100); |
wamae | 0:7bfeb237e600 | 132 | distance = sensor.get_dist_cm(); |
wamae | 0:7bfeb237e600 | 133 | printf("Measuring Dist =%ld...\n",distance); |
wamae | 0:7bfeb237e600 | 134 | dist_measure(); |
wamae | 0:7bfeb237e600 | 135 | ev_queue.call_every(3000, &check_for_updated_dist); |
wamae | 0:7bfeb237e600 | 136 | |
wamae | 0:7bfeb237e600 | 137 | |
wamae | 0:7bfeb237e600 | 138 | ev_queue.dispatch_forever(); |
wamae | 0:7bfeb237e600 | 139 | } |
wamae | 0:7bfeb237e600 | 140 | |
wamae | 0:7bfeb237e600 | 141 | |
wamae | 0:7bfeb237e600 | 142 |