Hi all.
I’m new to the particle, I have a couple i’m setting up to get data from a cattle feedlot.
This is the stuff I couldn’t find on the forum:
Coding could do with a bit of work but it’s getting better.
Save a struct array to sd (sdFat)
for this struct (i know it’s huge)
struct {
uint32_t ts; // 4 bytes
int32_t wg[3]; // 12 bytes
uint64_t id[3]; // 24 bytes
} rec[100];
// total 40 * 100 = 4000 bytes (need to know these values for php script later)
stick some data in it
rec[recCnt].ts = Time.now();
rec[recCnt].wg[0] = newWeight[0]; rec[recCnt].wg[1] = newWeight[1]; rec[recCnt].wg[2] = newWeight[2];
rec[recCnt].id[0] = id[0]; rec[recCnt].id[1] = id[1]; rec[recCnt].id[2] = id[2];
recCnt++;
stick it on a sd card
if (myFile.open(fileName, O_RDWR | O_CREAT | O_AT_END)) {
myFile.write((uint8_t *)&rec, sizeof(rec)/sizeof(uint8_t));
myFile.close();
}
get it back off the sd
if (myFile.open(fileName, O_READ)) {
while (myFile.peek() >= 0) {
myFile.read((uint8_t *)&rec, sizeof(rec)/sizeof(uint8_t));
// send the data here and repeat till the sd is empty
}
}
send the data
if (client.connect("www.my.website", 80)) {
client.println("POST /receivedata.php HTTP/1.0");
client.println("Host: www.my.website");
client.print("Stuff "); // custom headers just go in
client.println(stuffVar);
client.print("Content-Length: ");
client.println(sizeof(rec)/sizeof(uint8_t));
client.println();
client.write((uint8_t *)&rec, structSize);
client.println();
while (!client.available()) {
if (cnt++ > 100) break; delay(100); // timeout 10 seconds
}
while (client.available()) {
reply = client.read(); // last char received is the reply from receivedata.php
}
client.stop();
} else reply = '4';
In receivedata.php
$postContents = file_get_contents('php://input'); // the struct I sent
$cl = (int) $_SERVER['CONTENT_LENGTH']; // always 4000 but I check anyway
$stuffVar = $_SERVER['HTTP_STUFF']; // get my header
$structure = "";
for ($i=0;$i<($cl/40);$i++) { // 40 bytes each array member
$structure .= "Lts" . $i . "/L3w" . $i . "/Q3id" . $i . "/"; // the structure of the struct
}
$pcArray = unpack($structure, $postContents); // the unpack array
// save the unpack array, it's a bit strange how unpack does the array, I use a for loop and make a new one
$pcString = json_encode($pcArray);
$fileName = 'postContents.txt';
file_put_contents($fileName, $pcString, FILE_APPEND | LOCK_EX);
Pete