My components: Arduino Uno , ethernet shield w5100 and 16 gb sd card
I am trying to get the binary file sent by the client from the arduino server side and print it to the sd card.
I haven't too much arduino experience , ı wrote sending part my c++ client socket side, but I do not know how I am gonna get and write to sd card to my arduino server side.. Is it possible to do that?
can someone help me?
Thank you.
my cpp socket client side ( sending binary file )
char *buffer; //buffer to store file contents
long size, end; //file size
ifstream file("ques.exe", ios::in | ios::binary | ios::ate);
size = file.tellg(); //retrieve get pointer position
file.seekg(0, ios::end); //position get pointer at the begining of the file
end = file.tellg();
buffer = new char[end-size]; //initialize the buffer
file.read(buffer, (end-size)); //read file to buffer
file.close(); //close file
//and sending part
if (string(buf, 0, bytesReceived)=="test")
{
int iResult = send(sock, buffer,(end-size)+1, 0);
if (iResult != SOCKET_ERROR) {
cout << "Success" << endl;
}
}
see the ChatServer example for socket server example.
the server will return a EthernetClient. this implements Stream class as Serial so you will read the bytes with one of the read functions (byte by byte or over buffer). The SD class File implements Stream too so you will write the data with one of the write functions (byte by byte or over buffer).
with buffer
long read = 0;
byte buff[64];
while (client.connected() && read < contentLength) {
while (client.available()) {
int l = client.read(buff, sizeof(buff));
file.write(buff, l);
read += l;
}
}
file.close();
I tried but I couldn't integrate it into my own code.
Could you tell me , how can ?
I will explain more,
when client send "2" to my arduino server , arduino server will send "test" then my client when gets a "test" it will send binary file and arduino will write to sd card. Is ıt works?
It feels like there is something wrong.
there is my arduino server code:
#include <SD.h>
#include <SPI.h>
#include <Ethernet.h>
#include <ArduinoJson.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, x, x, x);
IPAddress gateway( 10, 0, 0, 1 );
IPAddress subnet(255, 255, 0, 0);
EthernetServer server(5002);
String inData; //char inData;
void setup() {
// initialize the ethernet device
Ethernet.begin(mac, ip, gateway, subnet);
// start listening for clients
server.begin();
// Open serial communications and wait for port to open:
Serial.begin(9600);
Serial.println(Ethernet.localIP());
}
void loop() {
EthernetClient client = server.available();
// when the client sends the first byte, say hello:
if (client.connected()) {
while (client.available())
{
char thisChar = client.read();
inData += thisChar;
// echo the bytes to the server as well:
Serial.write(thisChar);
}
}
if (inData == "1") {
StaticJsonDocument<250> doc;
char json[] ="{\"Model\":\"Arduino UNO R3 - USB Chip CH340\",\"Microcontroller\":\"ATmega328\",\"Flash Memory\":\" 32 KB (ATmega328) 0.5 KB bootloader\",\"SRAM\":\"2 KB (ATmega328)\",\"EEPROM\":\"1 KB (ATmega328)\"}";
DeserializationError error = deserializeJson(doc, json);
// Test if parsing succeeds.
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.c_str());
return;
}
else
{
serializeJsonPretty(doc, client);
inData="";
}
}
if(inData="2")
{
client.print("test");
}
else if(inData!="1" && inData!="2")
{
client.print("Any different request?");
inData="";
}
}