Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
6 years, 4 months ago.
How do i send data to the cloud and store in the SD card in real time
This happens when i inserted a time function to be storing the data to the SD card . Right now the tera term only shows that it sends only once. Here is part of my code
static void lora_event_handler(lorawan_event_t event) { switch (event) { case CONNECTED: printf("[LNWK][INFO] Connection - Successful\n"); break; case DISCONNECTED: ev_queue.break_dispatch(); printf("[LNWK][INFO] Disconnected Successfully\n"); break; case TX_DONE: printf("[LNWK][INFO] Message Sent to Network Server\n");
delete distance; wait(50); break; case TX_TIMEOUT: case TX_ERROR: case TX_CRYPTO_ERROR: case TX_SCHEDULING_ERROR: printf("[LNWK][INFO] Transmission Error - EventCode = %d\n", event);
delete distance; wait(50); break; case RX_DONE: printf("[LNWK][INFO] Received message from Network Server\n"); receive_message(); break; case RX_TIMEOUT: case RX_ERROR: printf("[LNWK][INFO] Error in reception - Code = %d\n", event); break; case JOIN_FAILURE: printf("[LNWK][INFO] ABP Failed - Check Keys\n"); break; default: MBED_ASSERT("Unknown Event"); } }
void sd_write(){
END OF CODE USED FOR OBTAINING TIME sensor.start(); wait_ms(100); long distance=sensor.get_dist_cm(); printf("distance is %dcm\n", distance); wait_ms(1000);
} int main() {
set_time(seconds); set_time(mktime(&t));
printf("=========================================\n"); printf(" Water Level Monitoring System \n"); printf("=========================================\n"); char date[64]; string for storing the current date char timeofday[64]; string for storing the current time
time_t seconds; time_t variable to read the number of seconds since January 1, 1970 for time calculations time_t now;
struct tm t; time structure for setting the time
set time structure to 20 Sep 2016 00:31:01 time_t rawtime; struct tm * timeinfo;
time ( &rawtime ); timeinfo = localtime ( &rawtime );
printf ( "Current local time and date: %s", asctime (timeinfo) ); t.tm_year = 2019 - 1900; t.tm_mon = 6 - 1; t.tm_mday = 10; t.tm_hour = 11; t.tm_min = 23; t.tm_sec = 8;
use the time structure and set the real-time clock (RTC) time set_time(mktime(&t));
lorawan_setup(DEV_ADDR_1, NWK_S_KEY_1, APP_S_KEY_1, lora_event_handler);
printf("Measuring Distance...\n");
immediately measure the distance sensor.start(); wait_ms(100); distance = sensor.get_dist_cm(); printf("Measuring Dist =%ld...\n",distance);
Try to mount the filesystem. printf("Mounting the filesystem... ");
int err = fs.mount(&sd); printf("%s\r\n", (err ? "Failed :(\r\n" : "OK\r\n")); if (err) return err;
Open a file. printf("Opening file '/sd/mylogger.txt'... "); FILE* fp = fopen("/sd/mylogger.txt", "a"); File open for "a"ppend if (fp == NULL) { Error! printf("Unable to write the file\r\n");
} else { char buffer[64]; strftime(buffer, 64, "%I:%M %p\n", localtime(&rawtime)); printf("Time as a custom formatted string = %s", buffer); time_t seconds = time(NULL); read the new time value strftime(date, 64, "%d/%m/%y", localtime(&rawtime)); create a date string from the time value strftime(timeofday, 64, "%H:%M:%S", localtime(&rawtime)); create a time string from the time value fprintf(fp, "%s %s %dcm\r\n", date, timeofday, distance); write the date string, time string, and analog integer value to the file the /r/n combination puts the entries on different lines in the file printf("%dcm ",distance); Append data to SD card. fprintf(fp, "%dcm\r\n",distance); Serial monitor. } fclose(fp); Cloce file. printf("File successfully written!\r\n"); Serial monitor. dist_measure(); ev_queue.call_every(1000, &check_for_updated_dist); ev_queue.dispatch_forever();
}