Send spiffs content via udp
Posted: Sun Jul 22, 2018 8:01 am
Following on from my Pi sleep/lock problem I have followed the advice of another user to cut out the pi from my process.
I have had a few problems but am getting the code written slowly and have a question about sending the contents of the spiff file to my PC. I might look at the MQTT? alternative protocol later but want to stick with udp at the moment.
The code I have for reading the content of the spiffs file and displaying it is below and works but how do I then make the file contents into something that can be transmitted wirelessly to my network?
Example line from the spiffs file: 1532206976, 29.1, 37.0,1008.4
My original code just sending one line of data which worked:
New code that reads the previously saved data from spiffs and could be any length up to 1 mB
Need to format the Serial.print into something I can transmit in udp.write() ?
I have had a few problems but am getting the code written slowly and have a question about sending the contents of the spiff file to my PC. I might look at the MQTT? alternative protocol later but want to stick with udp at the moment.
The code I have for reading the content of the spiffs file and displaying it is below and works but how do I then make the file contents into something that can be transmitted wirelessly to my network?
Example line from the spiffs file: 1532206976, 29.1, 37.0,1008.4
My original code just sending one line of data which worked:
Code: Select all
int packetSize = udp.parsePacket();
if (packetSize)
{
// receive incoming UDP packets
Serial.printf("Received %d bytes from %s, port %d\n", packetSize, udp.remoteIP().toString().c_str(), udp.remotePort());
int len = udp.read(incomingPacket, 255);
if (len > 0)
{
incomingPacket[len] = 0;
}
Serial.printf("UDP packet contents: %s\n", incomingPacket);
getWeather(); // Function to get the temperature and humidity
// send back a reply, to the IP address and port we got the packet from
udp.beginPacket(udp.remoteIP(), udp.remotePort());
udp.write(outputData);
udp.endPacket();
// Function pasted here for reference
void getWeather() {
h = bme.readHumidity();
t = bme.readTemperature();
p = (bme.readPressure() / 100.0F);
dtostrf(t, 5, 1, temperatureFString);
dtostrf(h, 5, 1, humidityString);
dtostrf(p, 6, 1, pressureString);
sprintf(outputData, "%s,%s,%s", temperatureFString, humidityString, pressureString);
delay(100);
Serial.println(t);
}
New code that reads the previously saved data from spiffs and could be any length up to 1 mB
Need to format the Serial.print into something I can transmit in udp.write() ?
Code: Select all
int i;
int packetSize = udp.parsePacket();
//if (packetSize)
if ((packetSize) && (download == 0)) // Make sure the data is only transmitted the once in this loop
{
// receive incoming UDP packets
Serial.printf("Received %d bytes from %s, port %d\n", packetSize, udp.remoteIP().toString().c_str(), udp.remotePort());
int len = udp.read(incomingPacket, 255);
if (len > 0)
{
incomingPacket[len] = 0;
}
Serial.printf("UDP packet contents: %s\n", incomingPacket);
// If file requested run this code and transmit the data
// open file for reading
File f = SPIFFS.open("/fileName.txt", "r");
if (!f) {
Serial.println("file open failed");
}
Serial.println("====== Reading file =======");
for(i=0; i<f.size(); i++) //Read upto complete file size
{
Serial.print((char)f.read());
}
f.close(); //Close file
Serial.println();
Serial.println("File Closed");
// Run this after the file has been uploaded to clear the memory
//Delete file WORKS
//SPIFFS.remove("/fileName.txt");
// send back a reply, to the IP address and port we got the packet from
udp.beginPacket(udp.remoteIP(), udp.remotePort());
udp.write(outputData);
udp.endPacket();
download = 1;
}