Hello.
I try to write data what i receive over Serial1 to a file. The file is being created but
when i write with myFile.write or myFile.print it is always empty. When i enable
Serial1.write i see output. In this case it is tekst but the goal is to send a bmp file.
how can i write the binary what is send over Serial1 to a file ?
Regards,
Johan
#include <UTFT.h>
#include <Time.h>
#include <Timer.h>
#include <tinyFAT.h>
#include "OneWire.h"
#include "DallasTemperature.h"
#include <UTFT_SdRaw.h>
#include <SdFat.h>
#define SD_CHIP_SELECT 53 // SD chip select pin
#define SSID "wlan";
#define PASS "XXXXX" // My luggage has pthe same combination!
#define DEST_HOST "bla.pm";
#define DEST_IP "IP";
#define TIMEOUT 5000 // mS
#define CONTINUE false
#define HALT true
#define PORT "80"
//Display
UTFT myGLCD(CTE70, 38, 39, 40, 41);
UTFT_SdRaw myFiles(&myGLCD);
char fileName[] = "2468.txt";
// file system object
SdFat sd;
SdFile myFile;
// #define ECHO_COMMANDS // Un-comment to echo AT+ commands to serial monitor
// Print error message and loop stop.
void errorHalt(String msg)
{
Serial.println(msg);
Serial.println("HALT");
while(true){};
}
// Read characters from WiFi module and echo to serial until keyword occurs or timeout.
boolean echoFind(String keyword)
{
byte current_char = 0;
byte keyword_length = keyword.length();
// Fail if the target string has not been sent by deadline.
long deadline = millis() + TIMEOUT;
while(millis() < deadline)
{
if (Serial1.available())
{
char ch = Serial1.read();
Serial.write(ch);
if (ch == keyword[current_char])
if (++current_char == keyword_length)
{
Serial.println();
return true;
}
}
}
return false; // Timed out
}
// Read and echo all available module output.
// (Used when we're indifferent to "OK" vs. "no change" responses or to get around firmware bugs.)
void echoFlush()
{while(Serial1.available()) Serial.write(Serial1.read());}
// Echo module output until 3 newlines encountered.
// (Used when we're indifferent to "OK" vs. "no change" responses.)
void echoSkip()
{
echoFind("\n"); // Search for nl at end of command echo
echoFind("\n"); // Search for 2nd nl at end of response.
echoFind("\n"); // Search for 3rd nl at end of blank line.
}
// Send a command to the module and wait for acknowledgement string
// (or flush module output if no ack specified).
// Echoes all data received to the serial monitor.
boolean echoCommand(String cmd, String ack, boolean halt_on_fail)
{
Serial1.println(cmd);
#ifdef ECHO_COMMANDS
Serial.print("--"); Serial.println(cmd);
#endif
// If no ack response specified, skip all available module output.
if (ack == "")
echoSkip();
else
// Otherwise wait for ack.
if (!echoFind(ack)) // timed out waiting for ack string
if (halt_on_fail)
errorHalt(cmd+" failed");// Critical failure halt.
else
return false; // Let the caller handle it.
return true; // ack blank or ack found
}
// Connect to the specified wireless network.
boolean connectWiFi()
{
String cmd = "AT+CWJAP=\""; cmd += SSID; cmd += "\",\""; cmd += PASS; cmd += "\"";
if (echoCommand(cmd, "OK", CONTINUE)) // Join Access Point
{
Serial.println("Connected to WiFi.");
return true;
}
else
{
Serial.println("Connection to WiFi failed.");
return false;
}
}
// ******** SETUP ********
void setup()
{
Serial.begin(115200); // Communication with PC monitor via USB
Serial1.begin(115200); // Communication with ESP8266 via 5V/3.3V level shifter
Serial1.setTimeout(TIMEOUT);
Serial.println("ESP8266 Demo");
delay(5000);
Serial.println(F("Initialising SD card..."));
bool mysd = 0;
// see if the card is present and can be initialized:
while (!mysd)
{
if (!sd.begin(SD_CHIP_SELECT, SPI_FULL_SPEED)) {
Serial.println(F("Card failed, or not present"));
Serial.println(F("Retrying...."));
}
else
{
mysd = 1;
Serial.println(F("Card initialised."));
}
}
//Initialze display
myGLCD.InitLCD(PORTRAIT);
myGLCD.clrScr();
myGLCD.setColor(255, 255, 255);
delay(5000);
echoCommand("AT+RST", "ready", HALT); // Reset & test if the module is ready
Serial.println("Module is ready.");
echoCommand("AT+GMR", "OK", CONTINUE); // Retrieves the firmware ID (version number) of the module.
echoCommand("AT+CWMODE=1", "", HALT); // Station mode
echoCommand("AT+CIPMUX=1", "", HALT); // Allow multiple connections (we'll only use the first).
//connect to the wifi
boolean connection_established = false;
for(int i=0;i<5;i++)
{
if(connectWiFi())
{
connection_established = true;
break;
}
}
if (!connection_established) errorHalt("Connection failed");
delay(5000);
echoCommand("AT+CIFSR", "", HALT); // Echo IP address. (Firmware bug - should return "OK".)
// Establish TCP connection
delay(5000);
String cmd = "AT+CIPSTART=0,\"TCP\",\""; cmd += DEST_IP; cmd += "\",80";
if (!echoCommand(cmd, "OK", CONTINUE)) return;
// Get connection status
if (!echoCommand("AT+CIPSTATUS", "OK", CONTINUE)) return;
delay(2000);
// Build HTTP request.
cmd = "GET / HTTP/1.1\r\nHost: "; cmd += DEST_HOST; cmd += ":80\r\n\r\n";
// Ready the module to receive raw data
if (!echoCommand("AT+CIPSEND=0,"+String(cmd.length()), ">", CONTINUE))
{
echoCommand("AT+CIPCLOSE", "", CONTINUE);
Serial.println("Connection timeout.");
return;
}
// Send the raw HTTP request
echoCommand(cmd, "OK", CONTINUE); // GET
//Open File
myFile.open(fileName, O_RDWR | O_CREAT | O_AT_END);
// Loop forever echoing data received from destination server.
while(true)
while (Serial1.available()){
//Serial.write(Serial1.read());
myFile.write(Serial1.read());
delay(25);
}
myFile.close();
errorHalt("ONCE ONLY");
}
void loop() {
}