#include "spiffs.h" #include "Common.h" #include "fetch_epochTime.h" struct flash_Storage { uint8_t rfidData[12]; uint8_t dateTime[13]; }; struct flash_Storage flash[400]; void spiff_RFIDDataStorage() { size_t total = 0, used = 0; char time_buf[32]; double dateTime_recvd = 0; long long convert_dateTime_longLong = 0; ret = esp_spiffs_info(NULL, &total, &used); if (ret != ESP_OK) { ESP_LOGE(SPIFFS_TAG, "Failed to get SPIFFS partition information (%s)", esp_err_to_name(ret)); } else { ESP_LOGI(SPIFFS_TAG, "Partition size: total: %d, used: %d", total, used); } strcpy((char *)flash[g_byRfidStoredInFlash_Counter].rfidData,(char *)g_byRfid_data_buff_copy); printf("flash[%d] = %s\n", g_byRfidStoredInFlash_Counter,flash[g_byRfidStoredInFlash_Counter].rfidData); dateTime_recvd = fetch_EpochTime_Ms(); convert_dateTime_longLong = floor(dateTime_recvd); sprintf(time_buf, "%lld", convert_dateTime_longLong); ESP_LOGI(SPIFFS_TAG, "The current date/time in India is: %s",time_buf); strcpy((char *)flash[g_byRfidStoredInFlash_Counter].dateTime,time_buf); ESP_LOGI(SPIFFS_TAG, "Opening file"); FILE* f = fopen("/spiffs/foo.txt", "a"); if (f == NULL) { ESP_LOGE(SPIFFS_TAG, "Failed to open file for writing"); return; } else printf("File opened\n"); fwrite(&flash[g_byRfidStoredInFlash_Counter], sizeof(struct flash_Storage), 1, f); printf("Size of flash buffer = %d\n",sizeof(struct flash_Storage)); //fprintf(f, (char *)flash[g_byRfidStoredInFlash_Counter]); g_byRfidStoredInFlash_Counter++; g_byRfidStoredInFlash_CounterCopy = g_byRfidStoredInFlash_Counter; err = nvs_set_i16(routerConfigHandler, "RfidNoInternet", g_byRfidStoredInFlash_CounterCopy); if(err != ESP_OK) { printf("\nError in nvs_set_i16 RfidNoInternet! (%04X)\n", err); return; } //printf("Commiting\n"); err = nvs_commit(routerConfigHandler); if(err != ESP_OK){ printf("\nError in commit! (%04X)\n", err); return; } //printf("No of rfid stored when no internet = %d\n", g_byRfidStoredInFlash_CounterCopy); fclose(f); ESP_LOGI(SPIFFS_TAG, "File written"); } void spiff_RFIDDataSendOnInternet() { cJSON *grp_id = NULL; cJSON *dev_id = NULL; cJSON *rfidTag = NULL; cJSON *rfidTags = NULL; unsigned int file_size = 0; size_t bytes_read = 0; char *rfid_dataInFlash = NULL; //char parsed_singleRfidPacketFromFlash[25]; char attendanceArrayURL[50]; bzero(attendanceArrayURL, sizeof(attendanceArrayURL)); form_AttendanceDataArrayURL(attendanceArrayURL); printf("Attendance Array URL 2 = %s\n", attendanceArrayURL); FILE* fp = fopen("/spiffs/foo.txt", "r"); if (fp == NULL) { ESP_LOGE(SPIFFS_TAG, "Failed to open file for reading"); return; } else{ fseek(fp, 0, SEEK_END); file_size = ftell(fp); fseek(fp, 0, SEEK_SET); //ESP_LOGI("SPIFFS", "File size: %d Bytes", file_size); } rfid_dataInFlash = (char *)malloc(file_size); if (rfid_dataInFlash == NULL) { ESP_LOGE("SPIFFS", "Failed to allocate memory"); return; } else{ //printf("ESP32 Flash Read\n"); bytes_read = fread(rfid_dataInFlash, 1,file_size,fp); rfid_dataInFlash[bytes_read] = '\0'; } //ESP_LOGI("SPIFFS", "Data sent: %d Bytes", bytes_read); //printf("File Data = %s\n", rfid_dataInFlash); ESP_LOGI(SPIFFS_TAG, "Read from file: '%s'", rfid_dataInFlash); err = nvs_get_i16(routerConfigHandler, "RfidNoInternet", &g_byRfidStoredInFlash_CounterCopy); if(err != ESP_OK) { printf("\nError in nvs_get_i16 to get RfidNoInternet ! (%04X)\n", err); } else { printf("Count1 = %d\n", g_byRfidStoredInFlash_CounterCopy); err = nvs_get_i16(routerConfigHandler, "RfidSent", &g_byRfidStoredInFlash_SentToServerCounter); if(err != ESP_OK) { printf("\nError in nvs_get_i16 to get RfidSent ! (%04X)\n", err); g_byRfidStoredInFlash_SentToServerCounter = 0; } esp_http_client_config_t config = { .url = attendanceArrayURL, .event_handler = _http_event_handler, .method = 1, }; //printf("Attendance Array URL = %s\n", config.url); esp_http_client_handle_t client = esp_http_client_init(&config); cJSON *flashRfid_json = cJSON_CreateObject(); grp_id = cJSON_CreateString(g_byDeviceIDBuff_ReceivedFromServer); cJSON_AddItemToObject(flashRfid_json, "device_group_id", grp_id); dev_id = cJSON_CreateString(g_byFormattedMacID_Buff); cJSON_AddItemToObject(flashRfid_json, "device_id", dev_id); rfidTag = cJSON_AddArrayToObject(flashRfid_json,"rfiddata"); printf("RfidSentToServerCounter in flash = %d\n", g_byRfidStoredInFlash_SentToServerCounter); for(int16_t rfid_dataCounter = g_byRfidStoredInFlash_SentToServerCounter * 25; rfid_dataCounter < (g_byRfidStoredInFlash_CounterCopy * 25); ) { printf("Hello India\n"); printf("%d\n", (g_byRfidStoredInFlash_CounterCopy * 25)); char parsed_singleRfidPacketFromFlash[27]; char parsed_dateFromFlash[14]; char parsed_rfidFromFlash[13]; printf("rfid_dataCounter= %d\n", rfid_dataCounter); bzero(parsed_singleRfidPacketFromFlash, sizeof(parsed_singleRfidPacketFromFlash)); bzero(parsed_rfidFromFlash, sizeof(parsed_rfidFromFlash)); bzero(parsed_dateFromFlash, sizeof(parsed_dateFromFlash)); memcpy(parsed_singleRfidPacketFromFlash, &rfid_dataInFlash[rfid_dataCounter], 25); printf("NEW = %s\n", parsed_singleRfidPacketFromFlash); int j = snprintf(parsed_rfidFromFlash, 13, "%s\n", parsed_singleRfidPacketFromFlash); printf("parsed_rfidFromFlash = %s\n", parsed_rfidFromFlash); int k = snprintf(parsed_dateFromFlash, 14, "%s\n", (parsed_singleRfidPacketFromFlash+12)); printf("parsed_dateFromFlash = %s\n", parsed_dateFromFlash); rfidTags = cJSON_CreateObject();; cJSON_AddItemToObject(rfidTags, "rfid", cJSON_CreateString(parsed_rfidFromFlash)); cJSON_AddItemToObject(rfidTags, "timestamp", cJSON_CreateString(parsed_dateFromFlash)); cJSON_AddItemToArray(rfidTag, rfidTags); rfid_dataCounter += 25; printf("New rfid_dataCounter = %d\n", rfid_dataCounter); g_byRfidStoredInFlash_SentToServerCounter++; printf("g_byRfidStoredInFlash_SentToServerCounter = %d\n", g_byRfidStoredInFlash_SentToServerCounter); err = nvs_set_i16(routerConfigHandler, "RfidSent", g_byRfidStoredInFlash_SentToServerCounter); if(err != ESP_OK) { printf("\nError in nvs_set_i16 RfidSent! (%04X)\n", err); return; } err = nvs_commit(routerConfigHandler); if(err != ESP_OK){ printf("\nError in commit! (%04X)\n", err); return; } } char *post_data = cJSON_Print(flashRfid_json); printf("FLASHRFID DATA = %s\n", post_data); g_byLastAttendanceArraySentFailedBuff = cJSON_Print(flashRfid_json); esp_http_client_set_post_field(client, post_data, strlen(post_data)); //esp_http_client_set_method(client, HTTP_METHOD_POST); esp_http_client_set_header(client, "Authorization", access_token); esp_http_client_set_header(client, "Content-Type", "application/json"); apiFailedState = LAST_FAILED_ATTENDANCE_ARRAY; err = esp_http_client_perform(client); if (err == ESP_OK){ ESP_LOGI(SPIFFS_TAG, "SPIFF Data POST Status = %d, content_length = %d", esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else{ ESP_LOGE(SPIFFS_TAG, "SPIFF Data POST request failed: %s", esp_err_to_name(err)); apiFailedState = LAST_FAILED_ATTENDANCE_ARRAY; repeat_ServerApiCalls_onFail(); } esp_http_client_close(client); esp_http_client_cleanup(client); cJSON_Delete(flashRfid_json); free(post_data); free(g_byLastAttendanceArraySentFailedBuff); } if((g_byRfidStoredInFlash_CounterCopy-g_byRfidStoredInFlash_SentToServerCounter) == 0){ printf("all data sent\n"); if (remove("/spiffs/foo.txt") == 0) { printf("Test Point 3\n"); g_byRfidStoredInFlash_CounterCopy = 0; g_byRfidStoredInFlash_Counter = 0; g_byRfidStoredInFlash_SentToServerCounter = 0; err = nvs_set_i16(routerConfigHandler, "RfidNoInternet", g_byRfidStoredInFlash_CounterCopy); if(err != ESP_OK) { printf("\nError in nvs_set_i16 RfidNoInternet! (%04X)\n", err); return; } err = nvs_set_i16(routerConfigHandler, "RfidSent", g_byRfidStoredInFlash_SentToServerCounter); if(err != ESP_OK) { printf("\nError in nvs_set_i16 RfidSent! (%04X)\n", err); return; } //printf("Commiting\n"); err = nvs_commit(routerConfigHandler); if(err != ESP_OK){ printf("\nError in commit! (%04X)\n", err); return; } printf("Deleted successfully"); } else printf("Unable to delete the file"); } fclose(fp); }