I’m trying to write a program to read a file off of a photon board and send it to a node.js server on a computer, which then saves it to a file.
Here’s my photon code:
#include <SdFat.h>
#include "SdFat.h"
#include <string.h>
#define SD_CS_PIN SS
UDP Udp;
SdFat SD;
SdFile myFile;
IPAddress nodeServer(172,20,10,2);
int port = 33333;
char filename[20] = "sensor0000.TXT";
int counter;
char data[100];
void setup() {
if (!SD.begin(SD_CS_PIN)) {
Serial.println("initialization failed!");
}
for (int i = 1; i < 10000; i++) {
filename[6] = '0' + i/1000;
filename[7] = '0' + (i/100) % 10;
filename[8] = '0' + (i/10) % 10;
filename[9] = '0' + i % 10;
//Particle.publish("trying filename", filename);
if (! SD.exists(filename)) {
i = i - 1;
filename[6] = '0' + i/1000;
filename[7] = '0' + (i/100) % 10;
filename[8] = '0' + (i/10) % 10 ;
filename[9] = '0' + i % 10;
Particle.publish("using filename", filename);
break;
}
}
if (!myFile.open(filename, O_READ)) {
return;
}
Udp.begin(port);
}
void loop() {
counter = 0;
while ((data[counter] = myFile.read()) >= 0) {
Particle.process();
counter++;
if (counter >= 100) {
break;
}
}
Particle.publish("sending packet", data);
Udp.sendPacket(data, sizeof(data), nodeServer, port);
}
Node.js code:
var PORT = 33333;
var HOST = '172.20.10.2';
const fs = require('fs');
const dgram = require('dgram');
const server = dgram.createSocket('udp4');
const stream = fs.createWriteStream('./test.txt');
server.on('listening', function() {
var address = server.address();
console.log('UDP Server listening on ' + address.address + ':' + address.port);
});
server.on('message', function(message, remote) {
stream.write(message);
});
server.bind(PORT, HOST);
This works for most of the file, but for some reason the start and end of the file (first and last 20 lines or so) are completely transformed into a long string of � characters. The file also doesn’t appear to end, when I tested it with a smaller file it kept sending packets of � after all of the file data I wrote had been sent.
The only issue I could think of would be that the start of the file has words while the rest is just numbers, but some of the numbers near the beginning and end are corrupted also and the end doesn’t have words or characters besides numbers, so that doesn’t really work.
Thanks in advance for any help!