Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <time.h>
- #include <sys/time.h>
- #include <Ticker.h>
- #include <FS.h>
- #include <GoogleDrive.h>
- #include "soc/rtc_wdt.h"
- // Timezone definition to get properly time from NTP server
- #define MYTZ "CET-1CEST,M3.5.0,M10.5.0/3"
- #include <SPIFFS.h>
- const char* fsName = "SPIFFS";
- #define fileSystem SPIFFS
- /*
- #include <FFat.h>
- const char* fsName = "FFat";
- #define fileSystem FFat
- */
- // WiFi setup
- const char* ssid = "xxxxxxxxxxx"; // SSID WiFi network
- const char* password = "xxxxxxxxxxx"; // Password WiFi network
- /* https://developers.google.com/identity/protocols/OAuth2ForDevices#creatingcred */
- const char* CLIENT_ID = "xxxxxxxxxx-6d2s2ne5v1eoapxxxxxxxxxxxxgp5gk7.apps.googleusercontent.com";
- const char* CLIENT_SECRET = "JqfXMxxxxxxxBr_4unmi4jEmS";
- const char* SCOPES = "https://www.googleapis.com/auth/drive.file email profile";
- struct tm timeinfo;
- const char * getUpdatedtime(const uint32_t timeout);
- bool fsOK = false;
- #define FORMAT_IF_FAILED true // Format filesystem if needed
- const char *hostname = "espfs";
- #include "espWebserver.h"
- GoogleDriveAPI myDrive(&fileSystem, NULL );
- // With Google Drive, we need the id of folder and files in order to access and work with
- // In this example, all files managed from the device will be stored in folder APP_FOLDERNAME
- const char* APP_FOLDERNAME = "AppData"; // Google Drive online folder name
- bool startUpload = false;
- void setup(){
- Serial.begin(115200);
- String taskMessage = "Setup running on core ";
- taskMessage = taskMessage + xPortGetCoreID();
- Serial.println(taskMessage);
- TaskHandle_t TaskUpload;
- xTaskCreatePinnedToCore(
- coreTask, /* Function to implement the task */
- "coreTask", /* Name of the task */
- 20000, /* Stack size in words */
- (void*)&startUpload, /* Task input parameter */
- 0, /* Priority of the task */
- &TaskUpload, /* Task handle. */
- 0); /* Core where the task should run */
- Serial.println("Task created...");
- // WiFi INIT
- startWiFi();
- // FILESYSTEM INIT
- startFilesystem();
- // Sync time with NTP. Blocking, but with timeout (0 == no timeout)
- const char* nowTime = getUpdatedtime(5000);
- Serial.printf("Time: %s\n", nowTime);
- // Add custom handler for webserver
- server.on("/sendToDrive", HTTP_GET, [](){
- server.send(200, "text/plain", "Start uploading file to Drive");
- startUpload = true;
- });
- // WEB SERVER INIT
- startWebServer();
- // Print some information about heap memory before use init the library
- printHeapStats();
- // Begin, after wait authorize if necessary (with 60s timeout)
- if (myDrive.begin(CLIENT_ID, CLIENT_SECRET, SCOPES)){
- uint32_t timeout = millis();
- while (myDrive.getState() != GoogleDriveAPI::GOT_TOKEN){
- // Notify user about pending authorization required https//www.google.come/device with user_code
- if(myDrive.getState() == GoogleDriveAPI::REQUEST_AUTH){
- Serial.println(F("\nApplication need to be authorized!"));
- Serial.print(F("Open with a browser the address < https//www.google.come/device > and insert this confirmation code "));
- Serial.println(myDrive.getUserCode());
- }
- // Wait for user authorization on https//www.google.come/device
- delay(10000);
- if(millis() - timeout > 60000) break;
- }
- // Check if the "app folder" is present in your Drive
- if (myDrive.getState() == GoogleDriveAPI::GOT_TOKEN){
- String appFolderId = myDrive.searchFile(APP_FOLDERNAME);
- if (appFolderId.length() < 30 ) {
- Serial.println("Folder APP not present. Now it will be created.");
- myDrive.createFolder(APP_FOLDERNAME, "root");
- }
- else {
- // Save the folder id for easy retrieve when needed
- myDrive.setAppFolderId(appFolderId);
- Serial.printf("App folder id: %s\n", appFolderId.c_str());
- }
- }
- }
- }
- void loop(){
- server.handleClient();
- // Print information about memory state
- static uint32_t printTime;
- if(millis() - printTime > 5000){
- printTime = millis();
- printHeapStats();
- }
- }
- // This task will run on core 0, while loop() default task is number 1
- // Once the bool global var startUpload == true, upload will start without interfer to other things
- void coreTask( void * pvParam ){
- String taskMessage = "Task running on core ";
- taskMessage = taskMessage + xPortGetCoreID();
- while(true){
- if(startUpload){
- // Disable temporarily watchdog for core 0
- disableCore0WDT();
- uploadToDrive();
- enableCore0WDT();
- startUpload = false;
- }
- vTaskDelay(10);
- }
- vTaskDelete(NULL);
- }
- void uploadToDrive(){
- Serial.println(F("\n\nUploading a new file to Drive \"/audio.wav\""));
- uint32_t startTime = millis();
- // If uploadedId is a valid Google ID, upload was succesfull
- String uploadedId = myDrive.uploadFile("/audio.wav", myDrive.getAppFolderId(), false);
- if(uploadedId.length()){
- String text = F("File uploaded to Drive with id ");
- text += uploadedId;
- Serial.println(text);
- }
- else{
- server.send(200, "text/plain", "Error");
- Serial.print(F("\nError. File not uploaded correctly."));
- }
- Serial.printf("Upload time %d ms\n", millis() - startTime);
- }
- void startFilesystem(){
- // FILESYSTEM INIT
- //fileSystem.format();
- fsOK = fileSystem.begin(FORMAT_IF_FAILED);
- if (fsOK){
- File root = fileSystem.open("/", "r");
- File file = root.openNextFile();
- while (file){
- const char* fileName = file.name();
- size_t fileSize = file.size();
- Serial.printf("FS File: %s, size: %lu\n", fileName, (long unsigned)fileSize);
- file = root.openNextFile();
- }
- Serial.println();
- }
- }
- void startWiFi(){
- Serial.printf("Connecting to %s\n", ssid);
- if (String(WiFi.SSID()) != String(ssid)){
- WiFi.mode(WIFI_STA);
- WiFi.begin(ssid, password);
- }
- while (WiFi.status() != WL_CONNECTED){
- delay(500);
- Serial.print(".");
- }
- Serial.print("\nConnected! IP address: ");
- Serial.println(WiFi.localIP());
- WiFi.setHostname(hostname);
- MDNS.begin(hostname);
- Serial.printf("Open http://%s.local/edit to edit or upload files\n", hostname);
- // Set timezone and NTP servers
- configTzTime(MYTZ, "time.google.com", "time.windows.com", "pool.ntp.org");
- }
- const char * getUpdatedtime(const uint32_t timeout) {
- #define BUF_SIZE 40
- uint32_t start = millis();
- Serial.print("Sync time...");
- do {
- time_t now = time(nullptr);
- timeinfo = *localtime(&now);
- delay(1);
- } while(millis() - start < timeout && timeinfo.tm_year <= (1970 - 1900));
- Serial.println(" done.");
- char * buffer = new char[BUF_SIZE];
- buffer[BUF_SIZE] = '\0';
- strftime (buffer, 40, "%A, %d/%m/%Y %H:%M:%S", &timeinfo);
- return (const char*) buffer;
- }
- void printHeapStats(){
- Serial.printf("\nTotal free: %6d - Max block: %6d\n", heap_caps_get_free_size(0), heap_caps_get_largest_free_block(0));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement