Got everything working pretty much exactly how it should!
Here’s what I used for whoever’s interested:
Spark Code
byte server[] = {192, 168, xxx, xxx};
String line1("PUT /~jkennedy/get_test.php HTTP/1.1\r\n");
String line2("User-Agent: A Spark Core\r\n");
String line3("Host: 192.168.xxx.xxx\r\n");
String line4("Connection: close\r\n");
String line5("Content-Type: text/plain\r\n");
String line6("Content-Length: ");
//line7 is added by transmit()
String line8("\r\n");
String line9("\r\n");
String line10("\r\n");
//FUNCTION TO TRANSMIT DATA TO DATABASE
void transmit(char Name[20],float temp) {
char tmpString[40];
sprintf(tmpString, "%s, %f",Name,temp );
String message = String(tmpString);
String line7(message.length());
TCPClient client;
if(client.connect(server, 80)) {
client.write( (uint8_t*)line1.c_str(), line1.length() );
client.write( (uint8_t*)line2.c_str(), line2.length() );
client.write( (uint8_t*)line3.c_str(), line3.length() );
client.write( (uint8_t*)line4.c_str(), line4.length() );
client.write( (uint8_t*)line5.c_str(), line5.length() );
client.write( (uint8_t*)line6.c_str(), line6.length() );
client.write( (uint8_t*)line7.c_str(), line7.length() );
client.write( (uint8_t*)line8.c_str(), line8.length() );
client.write( (uint8_t*)line9.c_str(), line9.length() );
delay(20);
client.write( (uint8_t*)message.c_str(), message.length() );
client.write( (uint8_t*)line10.c_str(), line10.length() );
delay(20);
Serial.print("Data Sent");
}
client.flush();
client.stop();
Serial.println(message);
}
and here’s the php script to handle the data. There’s probably a better way to do this as it involves an intermediary text file to store the data and then read it back to send it. It ain’t pretty but it works!
<?php
$dbhost="localhost";
$dbuser="*************";
$dbpass="*************";
$targetDB="***********";
$handle = fopen("./helloworld.txt", "w+",true);
$content = fopen("php://input","r");
while($data=fread($content,1024)){
fwrite($handle,$data);
}
fclose($content);
$put_vars = file_get_contents("./helloworld.txt",true);
$TestData = str_getcsv($put_vars,",");
$TestName = $TestData[0];
$TestTemp = $TestData[1];
$server = mysqli_connect($dbhost, $dbuser, $dbpass, $targetDB);
if(mysqli_errno()) {
fwrite( "Could not connect: " . mysqli_connect_error());
}
else{
fwrite( "connected. \n");
}
$result = "INSERT INTO TestTable(TestName,TestTemp) VALUES ('" . $TestName . "', '" . $TestTemp . "')";
if(!mysqli_query($server, $result)) {
die('error: ' . mysqli_error($server));
}
if($result){fwrite( "query sent ");}else{fwrite( "oops ");}
fclose($handle);
mysqli_close($server);
?>