Send text file from HTML page to arduino

Hi

For a project I’m doing, I’d like to send a text file from a PC to an arduino using wifi as a data carrier. I’m using the ESP-01 Module to create a software simulated access point to connect to.

In my previous wifi projects I’ve used a small HTML page to send data to my arduino via the ESP01. I built a very simple page containing a file uploader and a submit button to try and test this method for sending the file. However, I think that the serial buffer is overflowing faster than I can empty it because I can’t control the speed at which the file is sent. So the HTML might be a dead end.

Another possibility is using a C# PC program (which I have a small amount of experience with) to send the file using sockets, but I’m not sure how to use the sockets to send raw data. If anyone has any useful links or can provide explanations, it would be amazing.

Thanks already

(Arduino code for the HTML project)

byte connectionId;        // Link ID of current connection
long int timeout = 2000;  // Number of msec to wait for response of ESP-01 module

String header = "<form action=\"http://192.168.4.1/\" method=\"get\">";
String upload = "<input type=\"file\" name=\"upload\" value=\"upload\" id=\"upload\">";
String lbl = "<label for=\"fileupload\"> Select a file to upload</label>";
String submit = "<input type=\"submit\" value=\"Submit\"></form>";
String temp = "";

#define DEBUG true

void setup() {
  Serial.begin(9600);     // Start serial monitor
  Serial1.begin(115200);  // Start serial communication with ESP01 (baud rate = 115200)
  
  sendData("AT+RST\r\n",2000,DEBUG);          // Reset ESP01 module
  sendData("AT+GMR\r\n",2000,DEBUG);          // Check version info of the ESP01 module's firmware
  sendData("AT+CWMODE_CUR=2\r\n",1000,DEBUG); // Set the module to work in SoftAP mode
  sendData("AT+CWSAP_CUR=\"GIPTest\",\"Luc69420\",5,4\r\n",10000,DEBUG);  // Create SSID, WIFI password, channel 5 and security WPA & WPA2
  
  sendData("AT+CWDHCP_CUR=0,1\r\n",1000,DEBUG); // Enable DHCP in SoftAP and client mode
  sendData("AT+CIPMUX=1\r\n",1000,DEBUG);       // Configure for multiple connections
  sendData("AT+CIPSERVER=1,80\r\n",1000,DEBUG); // Turn on server on port 80, only works if CIPMUX=1
  
  sendData("AT+CIPAPMAC_CUR?\r\n",1000,DEBUG);  // Show MAC adress of ESP
  sendData("AT+CIPAP_CUR?\r\n",1000,DEBUG);     // Show current IP of ESP
  sendData("AT+CWLIF\r\n",1000,DEBUG);          // Show connected clients
}
 
void loop() {
  // A: Read incoming message from webbrowser (Cient) to ESP01 module and BBAVR (Server)
  if(Serial1.available()){    // If there is incoming data from webbrowser
    // 1. search for the link id number
    if(Serial1.find("+IPD,")){// If the incoming data from ESP01 has the instruction "IPD" - form incoming message: (+CIPMUX=1)+IPD,<link ID>,<len>[,<remote IP>,<remote port>]:<data>
      Serial.println("Request ok");                       // Display this on serial monitor
      Serial.println("Receiving data from web browser");  // Display this on serial monitor
      delay(300);                                         // Wait for the serial buffer to fill up (read all the serial data)
      connectionId = Serial1.parseInt();                  // Get the link id, parseInt converts characters to numbers. We need the link ID to be able to close the correct connection
      
      Serial1.flush();                  // Waits for the transmission of outgoing serial data to complete.

      Serial.println("");

      while(Serial1.available()){
        char ch = Serial1.read();
        Serial.print(ch);
      }
      Serial.println("");
     
      // B: Send new html webpage from BBAVR(Server) to the webbrowser (Client)
      Serial.println("Sending HTML");        // display this on serial monitor

      // Sending data to browser


      //send data from BBAVR & ESP01 (Server side) to the webbrowser (Client side)
      int contentLength = 0;
      
      contentLength = header.length() + upload.length() + lbl.length() + submit.length();  // we need to also send the data length that we calculate here
      
      //response http header
      espsend("HTTP/1.0 200 OK \r\nContent-Length:"+ (String)contentLength +"\r\nContent-Type: text/html; charset=utf-8\r\nConnection: close\r\n\r\n");
      
      espsend(header);
      espsend(upload);
      espsend(lbl);
      espsend(submit);

      //close connection
      String closeCommand = "AT+CIPCLOSE=";  //close the socket connection esp command 
      closeCommand+=connectionId; // append connection id
      closeCommand+="\r\n";
      sendData(closeCommand,1000,DEBUG); //timeout was standaard 3000
    }
  }
}

// FUNCTION : Send data from ESP01 to a client (webbrowser) 
void espsend(String d) {  
   String cipSend = " AT+CIPSEND="; // ESP01 Message format: "AT+CIPSEND=<link  ID>,<length>" 
   cipSend += connectionId;       
   cipSend += ",";
   cipSend +=d.length();            // Get the length of string d
   cipSend +="\r\n";                // Begin new line
   sendData(cipSend,500,DEBUG);     // Timeout was standard 1000
   sendData(d,500,DEBUG);           // Timeout was standard 1000
}

// FUNCTION : Send data from BBAVR to ESP01 and wait a certain time for a response 
void sendData(String command, const int timeout, boolean debug){
   // FIRST we send the command to the ESP01 module with these lines
   String response = "";        // Declare a string with the name response and empty it.
   Serial1.flush();             // Waits for the transmission of outgoing serial data to complete.  
   Serial1.print(command);      // Send the command to the ESP01 module
   long int timenow = millis(); // Get the number of msec since the start of the program and put it in var "timenow"        
   byte end = 0;
   
   // SECOND we wait for a response message of the ESP01 module
   while( (timenow+timeout) > millis()){// Stay in this loop for a number of msec set by 'timeout' value at the start of the program
      while(Serial1.available()){       // Loop as long as there is data in the serial receive buffer
        char c = Serial1.read();        // Read the next character.
        response+=c;                    // Add the last character to the response string
      }  
   }
    
   if(debug){                 // If debug is set to 1, the response is printed on the serial monitor
      Serial.print(response); // Displays the ESP response messages on the Serial monitor
      delay(100);
   }
}

Unless you are using an Arduino with two hardware serial ports, SoftwareSerial does not work well if at all at 115200.

In addition, do not use String; use char arrays.

Unless you are using an Arduino with two hardware serial ports, SoftwareSerial does not work well if at all at 115200.

I was going to use this project on an Arduino Mega anyways, so I suppose that this won’t be the problem? Except if I don’t understand how the different serial ports on an Arduino Mega work.

In addition, do not use String; use char arrays.

I’ll keep this in mind. I found some notes on the syntaxis of cstrings but I haven’t gotten to using them. Is there a lot of lost functionality when switching to char arrays?

The different hardware serial ports on a Mega all work the same.

It is better to use char arrays. However, depending on your code, you may not encounter problems using String. When you do, the code won't work.

.