5 Replies Latest reply on Oct 13, 2015 1:59 PM by PabloM_Intel

    REALLY WEIRD "fix" to File Read (fgets) in Edison

    crmakers

      I've been working on a system that reads data from the Internet "asynchronously" in Arduino IDE. My reference implementation is a home security prototype.

       

      The way the async calls work is making a system call to a nodejs script that reads data from the internet on a forked process and places the results (and progress updates) to a text file.

       

      So the system constantly checks on that file for its latest value.

       

      But for some reason it was reading garbage at times; at some point it was reading ONLY garbage, which made the approach unuseful.

       

      But! by accident, it started to work flawlesly, zero garbage, only meaningful reads to the file, even in very stressful conditions.

       

      I say by accident because the code that makes it work is quite unrelated to the file read process.

       

      The issue is documented here: https://github.com/janunezc/pvcloud/issues/24

       

      The code that makes it work is this (in red):

       

      void loop() {

       

       

        /* BEGIN: THE FOLLOWING CODE COULD NOT BE MOVED OUT TO A FUNCTION BECAUSE THE SYSTEM LOSES ITS ABILITY TO READASYNC PVCLOUD */

        String curMillis = String(millis()); //(!!!) Initializing curMillis to "0000" causes a lot of unrelated garbage on file read.

        String header = "TOMBO(" + CheckChar + ")" + curMillis; //Removing curMillis variable DOESN'T seem to cause garbage in file read.

        lcdOut(header,0);//(!!!) calling lcdOut(header) overloaded method causes unrelated garbage on file read!!!

        /* END: THE PRECEDING CODE COULD NOT BE MOVED OUT TO A FUNCTION BECAUSE THE SYSTEM LOSES ITS ABILITY TO READASYNC PVCLOUD */

       

       

        lcdCompositeOPMode();

       

        detectPushButton();

       

        asyncPVCloudUpdate();

       

       

        processOPMode();

       

       

        processSensorsLineChanges();

       

       

        processAlarmConditionChanges();

      }

       

      Even if I move that code to the end of loop, garbage comes back.

       

      The process that reads the file is in my so-called pvcloud_arduino_library in the same GIT repo (use development branch)

       

      https://github.com/janunezc/pvcloud/blob/development/pvcloud_arduino_library/pvcloud_lib.cpp

       

      String PVCloud::Check(String label){

        String fileName = asyncFilePath + "/out_pvcloud_";

       

       

        if (label != ""){

        fileName += label;

        }

        else {

        fileName += "any";

        }

        fileName += ".txt";

       

       

        FILE *filePointer;

        filePointer = fopen(fileName.buffer, "r");

        char fileContent[200];

       

       

      fgets(fileContent, 200, filePointer);

        String value = fileContent;

        value.trim();

       

       

        fclose(filePointer);

        return value;

      }