Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Example sketch required
#1
Hi

Can you please provide an example sketch that utilises some of the disk and file system commands?

Thanks

Alan
Reply
#2
Hi Alan,

there are a lots of functionalities connected with the file system. Here we report just a simple example that creates a file with three text lines and installs a TCP server that permits to download the file with a common browser:


Code:
  ....
  delay(5000);                        // Let uPanel start
  Serial.begin(57600);                    // Initialise the serial port
 
  Serial.println("");                    // Discharge pending commands
  Serial.println("$DISK:AUTOFORMAT");            // Format the disk if necessary
  Serial.println("$FILE0:OPEN:data.txt:w");        // Create new file "data.txt" for writing
  
  for(int n=0; n<3; n++)                // Manage 3 lines
  {
     String TextLine = "Line "+String(n);         // Prepare a string to write to file
     Serial.println("$FILE0:WAN:"+TextLine);            // Write the string and terminate with \n
  }
  
  Serial.println("$FILE0:CLOSE");            // Close the file
  Serial.println("$DISK:LS");                // Ask the disk's directory
  Serial.println("$TCP0:LAFH::5555");            // Start a WEB server (all files, no password) on port 5555
The output from the miuPanel module will be:

Code:
$OK-AFORMAT
$FILE0:OPEN: 0 bytes
$FILE0:WR: 7 bytes
$FILE0:WR: 7 bytes
$FILE0:WR: 7 bytes
$FILE0:CLOSED
$DISK-LS
$LS:       21 data.txt
$OK-LS
$OK-TCP0-Listen

For downloading this file, open a browser and type the following line in the URL field, where xxx.xxx.xxx.xxx is the IP address of the module:
http://xxx.xxx.xxx.xxx:5555/data.txt
We hope this will help you.
Reply
#3
Thank you for the example code. I've compiled and uploaded it to the Arduino. When the code runs I can see the commands being written to the serial monitor but I do not receive any responses from the miuPanel module (Serial.read() doesn't receive data). I'm confident that the wiring is correct because I can successfully run other miuPanel examples from the website and observe 2-way communication between the Arduino and the miuPanel module. I seem to be having trouble specifically with the $FILE and $DISK commands. Any help to resolve this would be appreciated. Thanks - Alan.
Reply
#4
Hello,

is your module running firmware version 3? Disk and network functions have been introduced in the version 003A. If your module still runs the version 2, these commands do not produce any output. Also please note, that in the example the serial has been set to 57600, if your module is set to 115200 you have to change either the sketch or the module configuration.

You can check the firmware version and baud rate in the configuration form through the App.
Reply
#5
I've upgraded the firmware to version 3 and can run your code fragment. I now get responses to the $DISK and $FILE commands. However, I'm seeing some strange responses. Each time I run the code I get a slightly different set of responses. I get multiple (sometimes 5, sometimes an endless number) of $FILE0:CLOSED responses for a single $FILE0:CLOSE command. I also get $ERR responses ($ERR-FILE-ID and $ERR-TCP-Socket). Also, when I access the URL from a browser I get an "Internal File Error" message.

Do you have a complete working sketch that you can post that runs the commands in the fragment you posted earlier but that in addition reads and displays the responses?

Thanks, Alan.
Reply
#6
Make sure you have not inserted the instructions above in the loop function, otherwise they will be executed multiple times. In case, move the code into the setup function or add a "while(1);" instruction at the and of the loop function.

Which Arduino board are you using? UNO?

miuPanel Team
Reply
#7
(04-19-2016, 01:08 PM)SCF Electronics Wrote: Make sure you have not inserted the instructions above in the loop function, otherwise they will be executed multiple times. In case, move the code into the setup function or add a "while(1);" instruction at the and of the loop function.

Which Arduino board are you using? UNO?

miuPanel Team


Hello

All the command calls are in the setup function. I'm using an Arduino Uno purchased 2015. In the hope that it helps, I've pasted in below the complete sketch that I built around your supplied code fragment.

Thanks, Alan



// **********************************************************************************
// Attempt to capture and display synchronous response from each $DISK/$FILE command.
// **********************************************************************************

#define MAX_WAIT_TIME 10000 // milliseconds.

void setup() {
  // Setup code exactly as supplied by miuPanel forum example apart from interleaved PrintResponse() calls.
 
  // put your setup code here, to run once:
  delay(5000);                        // Let uPanel start
  Serial.begin(57600);                    // Initialise the serial port
  Serial.println("");                    // Discharge pending commands

  Serial.println("$DISK:AUTOFORMAT");            // Format the disk if necessary
  PrintResponse();
 
  Serial.println("$FILE0:O:data.txt:w");        // Create new file "data.txt" for writing
  PrintResponse();
 
  for(int n=0; n<3; n++)                // Manage 3 lines
  {
     String TextLine = "Line "+String(n);         // Prepare a string to write to file

     Serial.println("$FILE0:WAN:"+TextLine);            // Write the string and terminate with \n
     PrintResponse();
  }
 
  Serial.println("$FILE0:CLOSE");            // Close the file
  PrintResponse();

  Serial.println("$DISK:LS");                // Ask the disk's directory
  PrintResponse();

  Serial.println("$TCP0:LAFH::5555");            // Start a WEB server (all files, no password) on port 5555
  PrintResponse();
}

void PrintResponse() {
  // It is assumed that each response ends in a newline but that a response to a given command can contain multiple lines.
 
  bool bGotResponse = false;
  String Response = "";
  int c;

  unsigned long startTime = millis();
  while (!bGotResponse && ((millis()-startTime) < MAX_WAIT_TIME)) {
    while (Serial.available()) {
      c = Serial.read();
      Response += (char) c;
      bGotResponse = true;
    }
    delay (100);
  }
  if (bGotResponse) {
    Serial.println(Response);
    Serial.println("**********************************");
  }
}

void loop() {
  // Print out anything we missed earlier.
  if (Serial.available()) {
    char c = Serial.read();
    Serial.print((char) c);
  }
  delay (100);
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)