Level measurement using range finder and lora technology

Dependencies:   Cayenne-LPP SDBlockDevice

Committer:
wamae
Date:
Wed Jun 26 10:35:50 2019 +0000
Revision:
0:f930f0440fd5
better copy

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wamae 0:f930f0440fd5 1 #include "mbed.h"
wamae 0:f930f0440fd5 2 #include "hcsr04.h"
wamae 0:f930f0440fd5 3 #include "SDBlockDevice.h"
wamae 0:f930f0440fd5 4 #include "FATFileSystem.h"
wamae 0:f930f0440fd5 5 //#include "MQTTTimer.h"
wamae 0:f930f0440fd5 6 //#include "CayenneMQTTClient.h"
wamae 0:f930f0440fd5 7 //#include "MQTTNetworkIDW01M1.h"
wamae 0:f930f0440fd5 8 #include "lorawan_network.h"
wamae 0:f930f0440fd5 9 #include "CayenneLPP.h"
wamae 0:f930f0440fd5 10 #include "mbed_events.h"
wamae 0:f930f0440fd5 11 #include "mbed_trace.h"
wamae 0:f930f0440fd5 12 #include "lora_radio_helper.h"
wamae 0:f930f0440fd5 13 #include "SX1276_LoRaRadio.h"
wamae 0:f930f0440fd5 14 #include "LoRaWANInterface.h"
wamae 0:f930f0440fd5 15 //#include "stm32_standby.h"
wamae 0:f930f0440fd5 16
wamae 0:f930f0440fd5 17
wamae 0:f930f0440fd5 18
wamae 0:f930f0440fd5 19
wamae 0:f930f0440fd5 20 //#define STANDBY_TIME_S 1* 60
wamae 0:f930f0440fd5 21
wamae 0:f930f0440fd5 22 extern EventQueue ev_queue;
wamae 0:f930f0440fd5 23
wamae 0:f930f0440fd5 24 static uint32_t DEV_ADDR_1 = 0x260118FA;
wamae 0:f930f0440fd5 25 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:f930f0440fd5 26 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:f930f0440fd5 27 SDBlockDevice sd(PC_12, PC_11, PC_10, D10);// mosi, miso, sclk, cs
wamae 0:f930f0440fd5 28 FATFileSystem fs("sd");
wamae 0:f930f0440fd5 29 FlashIAP flash;
wamae 0:f930f0440fd5 30
wamae 0:f930f0440fd5 31 void apply_update(FILE *file, uint32_t address);
wamae 0:f930f0440fd5 32
wamae 0:f930f0440fd5 33 HCSR04 sensor(D4, D3);
wamae 0:f930f0440fd5 34 //Serial pc(USBTX, USBRX);
wamae 0:f930f0440fd5 35 //FILE *fp; // File pointer declear
wamae 0:f930f0440fd5 36
wamae 0:f930f0440fd5 37
wamae 0:f930f0440fd5 38 int dw,d,M,y,h,m,s; //VARIABLES FOR TIME AND YEAR
wamae 0:f930f0440fd5 39
wamae 0:f930f0440fd5 40
wamae 0:f930f0440fd5 41 //global variable
wamae 0:f930f0440fd5 42 long distance;
wamae 0:f930f0440fd5 43
wamae 0:f930f0440fd5 44 bool dist_updated = false;
wamae 0:f930f0440fd5 45 float volume;
wamae 0:f930f0440fd5 46
wamae 0:f930f0440fd5 47
wamae 0:f930f0440fd5 48
wamae 0:f930f0440fd5 49 void dist_measure(){
wamae 0:f930f0440fd5 50 sensor.start();
wamae 0:f930f0440fd5 51 wait_ms(100);
wamae 0:f930f0440fd5 52 distance=sensor.get_dist_cm();
wamae 0:f930f0440fd5 53 volume = 6*6*(2-(distance*0.01));
wamae 0:f930f0440fd5 54 dist_updated = true;
wamae 0:f930f0440fd5 55
wamae 0:f930f0440fd5 56 }
wamae 0:f930f0440fd5 57
wamae 0:f930f0440fd5 58 void check_for_updated_dist() {
wamae 0:f930f0440fd5 59
wamae 0:f930f0440fd5 60 if (dist_updated){
wamae 0:f930f0440fd5 61 dist_updated = false ;
wamae 0:f930f0440fd5 62 printf("Measure Distance = %ld ",distance);
wamae 0:f930f0440fd5 63 printf("Volume=%3.0f\n", volume);
wamae 0:f930f0440fd5 64
wamae 0:f930f0440fd5 65 CayenneLPP payload(50);
wamae 0:f930f0440fd5 66 float volume_value = volume;
wamae 0:f930f0440fd5 67 printf("Volume_value=%f\n", volume_value);
wamae 0:f930f0440fd5 68 payload.addAnalogInput(4, volume_value);
wamae 0:f930f0440fd5 69
wamae 0:f930f0440fd5 70 if (!lorawan_send(&payload)){
wamae 0:f930f0440fd5 71 // delete distance;
wamae 0:f930f0440fd5 72 // standby(STANDBY_TIME_S);
wamae 0:f930f0440fd5 73
wamae 0:f930f0440fd5 74 }
wamae 0:f930f0440fd5 75 }
wamae 0:f930f0440fd5 76 }
wamae 0:f930f0440fd5 77 /*void SetDateTime(int year = 2019, int mon = 1, int day = 1, int hour = 0, int min = 0, int sec = 0) {
wamae 0:f930f0440fd5 78 struct tm Clock;
wamae 0:f930f0440fd5 79 Clock.tm_year = year - 1900;
wamae 0:f930f0440fd5 80 Clock.tm_mon = mon - 1; // note: subtract 1
wamae 0:f930f0440fd5 81 Clock.tm_mday = day;
wamae 0:f930f0440fd5 82 Clock.tm_hour = hour;
wamae 0:f930f0440fd5 83 Clock.tm_min = min;
wamae 0:f930f0440fd5 84 Clock.tm_sec = sec;
wamae 0:f930f0440fd5 85 time_t epoch = mktime(&Clock);
wamae 0:f930f0440fd5 86 if (epoch == (time_t) -1) {
wamae 0:f930f0440fd5 87 error("Error in clock setting\n");
wamae 0:f930f0440fd5 88 // Stop here
wamae 0:f930f0440fd5 89 }
wamae 0:f930f0440fd5 90 set_time(epoch);
wamae 0:f930f0440fd5 91 }*/
wamae 0:f930f0440fd5 92
wamae 0:f930f0440fd5 93 static void lora_event_handler(lorawan_event_t event) {
wamae 0:f930f0440fd5 94 switch (event) {
wamae 0:f930f0440fd5 95 case CONNECTED:
wamae 0:f930f0440fd5 96 printf("[LNWK][INFO] Connection - Successful\n");
wamae 0:f930f0440fd5 97 break;
wamae 0:f930f0440fd5 98 case DISCONNECTED:
wamae 0:f930f0440fd5 99 ev_queue.break_dispatch();
wamae 0:f930f0440fd5 100 printf("[LNWK][INFO] Disconnected Successfully\n");
wamae 0:f930f0440fd5 101 break;
wamae 0:f930f0440fd5 102 case TX_DONE:
wamae 0:f930f0440fd5 103 printf("[LNWK][INFO] Message Sent to Network Server\n");
wamae 0:f930f0440fd5 104
wamae 0:f930f0440fd5 105 // delete distance;
wamae 0:f930f0440fd5 106 //standby(STANDBY_TIME_S);
wamae 0:f930f0440fd5 107
wamae 0:f930f0440fd5 108 break;
wamae 0:f930f0440fd5 109 case TX_TIMEOUT:
wamae 0:f930f0440fd5 110 case TX_ERROR:
wamae 0:f930f0440fd5 111 case TX_CRYPTO_ERROR:
wamae 0:f930f0440fd5 112 case TX_SCHEDULING_ERROR:
wamae 0:f930f0440fd5 113 printf("[LNWK][INFO] Transmission Error - EventCode = %d\n", event);
wamae 0:f930f0440fd5 114
wamae 0:f930f0440fd5 115
wamae 0:f930f0440fd5 116 //delete distance;
wamae 0:f930f0440fd5 117 // standby(STANDBY_TIME_S);
wamae 0:f930f0440fd5 118
wamae 0:f930f0440fd5 119 break;
wamae 0:f930f0440fd5 120 case RX_DONE:
wamae 0:f930f0440fd5 121 printf("[LNWK][INFO] Received message from Network Server\n");
wamae 0:f930f0440fd5 122 receive_message();
wamae 0:f930f0440fd5 123 break;
wamae 0:f930f0440fd5 124 case RX_TIMEOUT:
wamae 0:f930f0440fd5 125 case RX_ERROR:
wamae 0:f930f0440fd5 126 printf("[LNWK][INFO] Error in reception - Code = %d\n", event);
wamae 0:f930f0440fd5 127 break;
wamae 0:f930f0440fd5 128 case JOIN_FAILURE:
wamae 0:f930f0440fd5 129 printf("[LNWK][INFO] ABP Failed - Check Keys\n");
wamae 0:f930f0440fd5 130 break;
wamae 0:f930f0440fd5 131 default:
wamae 0:f930f0440fd5 132 MBED_ASSERT("Unknown Event");
wamae 0:f930f0440fd5 133 }
wamae 0:f930f0440fd5 134 }
wamae 0:f930f0440fd5 135
wamae 0:f930f0440fd5 136 void sd_write(){
wamae 0:f930f0440fd5 137
wamae 0:f930f0440fd5 138 //END OF CODE USED FOR OBTAINING TIME
wamae 0:f930f0440fd5 139 sensor.start();
wamae 0:f930f0440fd5 140 wait_ms(100);
wamae 0:f930f0440fd5 141 long distance=sensor.get_dist_cm();
wamae 0:f930f0440fd5 142 printf("distance is %dcm\n", distance);
wamae 0:f930f0440fd5 143 wait_ms(1000);
wamae 0:f930f0440fd5 144
wamae 0:f930f0440fd5 145 }
wamae 0:f930f0440fd5 146 int main() {
wamae 0:f930f0440fd5 147
wamae 0:f930f0440fd5 148 printf("=========================================\n");
wamae 0:f930f0440fd5 149 printf(" Water Level Monitoring System \n");
wamae 0:f930f0440fd5 150 printf("=========================================\n");
wamae 0:f930f0440fd5 151 char date[64]; //string for storing the current date
wamae 0:f930f0440fd5 152 char timeofday[64]; //string for storing the current time
wamae 0:f930f0440fd5 153
wamae 0:f930f0440fd5 154 struct tm t; //time structure for setting the time
wamae 0:f930f0440fd5 155
wamae 0:f930f0440fd5 156 //set time structure to 20 Sep 2016 00:31:01
wamae 0:f930f0440fd5 157 time_t rawtime;
wamae 0:f930f0440fd5 158 //time_t seconds; //time_t variable to read the number of seconds since January 1, 1970 for time calculations
wamae 0:f930f0440fd5 159 //set_time(rawtime);
wamae 0:f930f0440fd5 160 struct tm * timeinfo;
wamae 0:f930f0440fd5 161
wamae 0:f930f0440fd5 162 time ( &rawtime );
wamae 0:f930f0440fd5 163 timeinfo = localtime ( &rawtime );
wamae 0:f930f0440fd5 164
wamae 0:f930f0440fd5 165 printf ( "Current local time and date: %s", asctime (timeinfo) );
wamae 0:f930f0440fd5 166 t.tm_year = 2019 - 1900;
wamae 0:f930f0440fd5 167 t.tm_mon = 6 - 1;
wamae 0:f930f0440fd5 168 t.tm_mday = 19;
wamae 0:f930f0440fd5 169 t.tm_hour = 14;
wamae 0:f930f0440fd5 170 t.tm_min = 23;
wamae 0:f930f0440fd5 171 t.tm_sec = 8;
wamae 0:f930f0440fd5 172
wamae 0:f930f0440fd5 173
wamae 0:f930f0440fd5 174 //use the time structure and set the real-time clock (RTC) time
wamae 0:f930f0440fd5 175 set_time(mktime(&t));
wamae 0:f930f0440fd5 176 while (true)
wamae 0:f930f0440fd5 177 {
wamae 0:f930f0440fd5 178 lorawan_setup(DEV_ADDR_1, NWK_S_KEY_1, APP_S_KEY_1, lora_event_handler);
wamae 0:f930f0440fd5 179
wamae 0:f930f0440fd5 180 printf("Measuring Distance...\n");
wamae 0:f930f0440fd5 181
wamae 0:f930f0440fd5 182 //immediately measure the distance
wamae 0:f930f0440fd5 183 sensor.start();
wamae 0:f930f0440fd5 184 wait_ms(100);
wamae 0:f930f0440fd5 185 distance = sensor.get_dist_cm();
wamae 0:f930f0440fd5 186 printf("Measuring Dist =%ld...\n",distance);
wamae 0:f930f0440fd5 187
wamae 0:f930f0440fd5 188
wamae 0:f930f0440fd5 189 // Try to mount the filesystem.
wamae 0:f930f0440fd5 190 printf("Mounting the filesystem... ");
wamae 0:f930f0440fd5 191
wamae 0:f930f0440fd5 192 int err = fs.mount(&sd);
wamae 0:f930f0440fd5 193 printf("%s\r\n", (err ? "Failed :(\r\n" : "OK\r\n"));
wamae 0:f930f0440fd5 194 if (err)
wamae 0:f930f0440fd5 195 return err;
wamae 0:f930f0440fd5 196
wamae 0:f930f0440fd5 197 // Open a file.
wamae 0:f930f0440fd5 198 printf("Opening file '/sd/mylogger.txt'... ");
wamae 0:f930f0440fd5 199 FILE* fp = fopen("/sd/mylogger.txt", "a"); // File open for "a"ppend
wamae 0:f930f0440fd5 200 if (fp == NULL) { // Error!
wamae 0:f930f0440fd5 201 // printf("Unable to write the file\r\n");
wamae 0:f930f0440fd5 202
wamae 0:f930f0440fd5 203 }
wamae 0:f930f0440fd5 204 else {
wamae 0:f930f0440fd5 205 char buffer[64];
wamae 0:f930f0440fd5 206 strftime(buffer, 64, "%I:%M %p\n", localtime(&rawtime));
wamae 0:f930f0440fd5 207 // printf("Time as a custom formatted string = %s", buffer);
wamae 0:f930f0440fd5 208 time_t seconds = time(NULL); //read the new time value
wamae 0:f930f0440fd5 209 strftime(date, 64, "%d/%m/%y", localtime(&rawtime)); //create a date string from the time value
wamae 0:f930f0440fd5 210 strftime(timeofday, 64, "%H:%M:%S", localtime(&rawtime)); //create a time string from the time value
wamae 0:f930f0440fd5 211 fprintf(fp, "%s %s %dcm\r\n", date, timeofday, distance); //write the date string, time string, and analog integer value to the file
wamae 0:f930f0440fd5 212 //the /r/n combination puts the entries on different lines in the file
wamae 0:f930f0440fd5 213 //printf("%dcm ",distance); // Append data to SD card.
wamae 0:f930f0440fd5 214 //fprintf(fp, "%dcm\r\n",distance); // Serial monitor.
wamae 0:f930f0440fd5 215 }
wamae 0:f930f0440fd5 216 fclose(fp);
wamae 0:f930f0440fd5 217 // Cloce file.
wamae 0:f930f0440fd5 218 wait(20);
wamae 0:f930f0440fd5 219 printf("File successfully written!\r\n"); // Serial monitor.
wamae 0:f930f0440fd5 220 dist_measure();
wamae 0:f930f0440fd5 221 ev_queue.call_every(3000, &check_for_updated_dist);
wamae 0:f930f0440fd5 222 ev_queue.dispatch_forever();
wamae 0:f930f0440fd5 223
wamae 0:f930f0440fd5 224 printf ( "Current local time and date: %s", asctime (timeinfo) );
wamae 0:f930f0440fd5 225
wamae 0:f930f0440fd5 226 }
wamae 0:f930f0440fd5 227
wamae 0:f930f0440fd5 228 }