Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: Cayenne-LPP SDBlockDevice
main.cpp
00001 #include "mbed.h" 00002 #include "hcsr04.h" 00003 #include "SDBlockDevice.h" 00004 #include "FATFileSystem.h" 00005 //#include "MQTTTimer.h" 00006 //#include "CayenneMQTTClient.h" 00007 //#include "MQTTNetworkIDW01M1.h" 00008 #include "lorawan_network.h" 00009 #include "CayenneLPP.h" 00010 #include "mbed_events.h" 00011 #include "mbed_trace.h" 00012 #include "lora_radio_helper.h" 00013 #include "SX1276_LoRaRadio.h" 00014 #include "LoRaWANInterface.h" 00015 //#include "stm32_standby.h" 00016 00017 00018 00019 00020 //#define STANDBY_TIME_S 1* 60 00021 00022 extern EventQueue ev_queue; 00023 00024 static uint32_t DEV_ADDR_1 = 0x260118FA; 00025 static uint8_t NWK_S_KEY_1[] = { 0x5D, 0xD5, 0xC9, 0x84, 0xB9, 0xCE, 0x26, 0x05, 0xB7, 0x61, 0xBF, 0x9F, 0x3C, 0x2D, 0x4E, 0x14 }; 00026 static uint8_t APP_S_KEY_1[] = { 0xF0, 0xE4, 0xA1, 0xEB, 0x1F, 0xB5, 0x6E, 0xBA, 0x31, 0x69, 0x80, 0x9D, 0x85, 0x92, 0x68, 0xB4 }; 00027 SDBlockDevice sd(PC_12, PC_11, PC_10, D10);// mosi, miso, sclk, cs 00028 FATFileSystem fs("sd"); 00029 FlashIAP flash; 00030 00031 void apply_update(FILE *file, uint32_t address); 00032 00033 HCSR04 sensor(D4, D3); 00034 //Serial pc(USBTX, USBRX); 00035 //FILE *fp; // File pointer declear 00036 00037 00038 int dw,d,M,y,h,m,s; //VARIABLES FOR TIME AND YEAR 00039 00040 00041 //global variable 00042 long distance; 00043 00044 bool dist_updated = false; 00045 float volume; 00046 00047 00048 00049 void dist_measure(){ 00050 sensor.start(); 00051 wait_ms(100); 00052 distance=sensor.get_dist_cm(); 00053 volume = 6*6*(2-(distance*0.01)); 00054 dist_updated = true; 00055 00056 } 00057 00058 void check_for_updated_dist() { 00059 00060 if (dist_updated){ 00061 dist_updated = false ; 00062 printf("Measure Distance = %ld ",distance); 00063 printf("Volume=%3.0f\n", volume); 00064 00065 CayenneLPP payload(50); 00066 float volume_value = volume; 00067 printf("Volume_value=%f\n", volume_value); 00068 payload.addAnalogInput(4, volume_value); 00069 00070 if (!lorawan_send(&payload)){ 00071 // delete distance; 00072 // standby(STANDBY_TIME_S); 00073 00074 } 00075 } 00076 } 00077 /*void SetDateTime(int year = 2019, int mon = 1, int day = 1, int hour = 0, int min = 0, int sec = 0) { 00078 struct tm Clock; 00079 Clock.tm_year = year - 1900; 00080 Clock.tm_mon = mon - 1; // note: subtract 1 00081 Clock.tm_mday = day; 00082 Clock.tm_hour = hour; 00083 Clock.tm_min = min; 00084 Clock.tm_sec = sec; 00085 time_t epoch = mktime(&Clock); 00086 if (epoch == (time_t) -1) { 00087 error("Error in clock setting\n"); 00088 // Stop here 00089 } 00090 set_time(epoch); 00091 }*/ 00092 00093 static void lora_event_handler(lorawan_event_t event) { 00094 switch (event) { 00095 case CONNECTED: 00096 printf("[LNWK][INFO] Connection - Successful\n"); 00097 break; 00098 case DISCONNECTED: 00099 ev_queue.break_dispatch(); 00100 printf("[LNWK][INFO] Disconnected Successfully\n"); 00101 break; 00102 case TX_DONE: 00103 printf("[LNWK][INFO] Message Sent to Network Server\n"); 00104 00105 // delete distance; 00106 //standby(STANDBY_TIME_S); 00107 00108 break; 00109 case TX_TIMEOUT: 00110 case TX_ERROR: 00111 case TX_CRYPTO_ERROR: 00112 case TX_SCHEDULING_ERROR: 00113 printf("[LNWK][INFO] Transmission Error - EventCode = %d\n", event); 00114 00115 00116 //delete distance; 00117 // standby(STANDBY_TIME_S); 00118 00119 break; 00120 case RX_DONE: 00121 printf("[LNWK][INFO] Received message from Network Server\n"); 00122 receive_message(); 00123 break; 00124 case RX_TIMEOUT: 00125 case RX_ERROR: 00126 printf("[LNWK][INFO] Error in reception - Code = %d\n", event); 00127 break; 00128 case JOIN_FAILURE: 00129 printf("[LNWK][INFO] ABP Failed - Check Keys\n"); 00130 break; 00131 default: 00132 MBED_ASSERT("Unknown Event"); 00133 } 00134 } 00135 00136 void sd_write(){ 00137 00138 //END OF CODE USED FOR OBTAINING TIME 00139 sensor.start(); 00140 wait_ms(100); 00141 long distance=sensor.get_dist_cm(); 00142 printf("distance is %dcm\n", distance); 00143 wait_ms(1000); 00144 00145 } 00146 int main() { 00147 00148 printf("=========================================\n"); 00149 printf(" Water Level Monitoring System \n"); 00150 printf("=========================================\n"); 00151 char date[64]; //string for storing the current date 00152 char timeofday[64]; //string for storing the current time 00153 00154 struct tm t; //time structure for setting the time 00155 00156 //set time structure to 20 Sep 2016 00:31:01 00157 time_t rawtime; 00158 //time_t seconds; //time_t variable to read the number of seconds since January 1, 1970 for time calculations 00159 //set_time(rawtime); 00160 struct tm * timeinfo; 00161 00162 time ( &rawtime ); 00163 timeinfo = localtime ( &rawtime ); 00164 00165 printf ( "Current local time and date: %s", asctime (timeinfo) ); 00166 t.tm_year = 2019 - 1900; 00167 t.tm_mon = 6 - 1; 00168 t.tm_mday = 19; 00169 t.tm_hour = 14; 00170 t.tm_min = 23; 00171 t.tm_sec = 8; 00172 00173 00174 //use the time structure and set the real-time clock (RTC) time 00175 set_time(mktime(&t)); 00176 while (true) 00177 { 00178 lorawan_setup(DEV_ADDR_1, NWK_S_KEY_1, APP_S_KEY_1, lora_event_handler); 00179 00180 printf("Measuring Distance...\n"); 00181 00182 //immediately measure the distance 00183 sensor.start(); 00184 wait_ms(100); 00185 distance = sensor.get_dist_cm(); 00186 printf("Measuring Dist =%ld...\n",distance); 00187 00188 00189 // Try to mount the filesystem. 00190 printf("Mounting the filesystem... "); 00191 00192 int err = fs.mount(&sd); 00193 printf("%s\r\n", (err ? "Failed :(\r\n" : "OK\r\n")); 00194 if (err) 00195 return err; 00196 00197 // Open a file. 00198 printf("Opening file '/sd/mylogger.txt'... "); 00199 FILE* fp = fopen("/sd/mylogger.txt", "a"); // File open for "a"ppend 00200 if (fp == NULL) { // Error! 00201 // printf("Unable to write the file\r\n"); 00202 00203 } 00204 else { 00205 char buffer[64]; 00206 strftime(buffer, 64, "%I:%M %p\n", localtime(&rawtime)); 00207 // printf("Time as a custom formatted string = %s", buffer); 00208 time_t seconds = time(NULL); //read the new time value 00209 strftime(date, 64, "%d/%m/%y", localtime(&rawtime)); //create a date string from the time value 00210 strftime(timeofday, 64, "%H:%M:%S", localtime(&rawtime)); //create a time string from the time value 00211 fprintf(fp, "%s %s %dcm\r\n", date, timeofday, distance); //write the date string, time string, and analog integer value to the file 00212 //the /r/n combination puts the entries on different lines in the file 00213 //printf("%dcm ",distance); // Append data to SD card. 00214 //fprintf(fp, "%dcm\r\n",distance); // Serial monitor. 00215 } 00216 fclose(fp); 00217 // Cloce file. 00218 wait(20); 00219 printf("File successfully written!\r\n"); // Serial monitor. 00220 dist_measure(); 00221 ev_queue.call_every(3000, &check_for_updated_dist); 00222 ev_queue.dispatch_forever(); 00223 00224 printf ( "Current local time and date: %s", asctime (timeinfo) ); 00225 00226 } 00227 00228 }
Generated on Sat Jul 16 2022 05:24:58 by
1.7.2