Okay, so with this project I don’t have access to node.js so I’m having to try something else. After a lot of mucking around, I’ve focused on using TCP Client & a GET request through PHP to insert into the mySQL DB
I’m basing my code on the TCP Client documentation here: https://docs.particle.io/reference/firmware/photon/#tcpclient
And similar code from an Arduino project here:
Here’s my photon code:
TCPClient client;
char server[] = "www.example.net";
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println("connecting...");
if (client.connect(server, 80)) {
client.println("GET /write-data.php?value=10");
client.println(" HTTP/1.1"); // Part of the GET request
client.println("www.example.net");
client.println("Connection: close");
client.println(); // Empty line
client.println(); // Empty line
client.stop(); // Closing connection to server
Serial.println("done..");
}
else {
// If Arduino can't connect to the server (your computer or web page)
Serial.println("connection failed");
}
delay(5000);
}
Here’s my PHP
<?php
// Prepare variables for database connection
$servername = "dbland.example.net";
$username = "user";
$password = "pass";
$dbname = "db";
// Create connection
$connect = new mysqli($servername, $username, $password, $dbname);
// Prepare the SQL statement
$sql = "INSERT INTO table (a) VALUES ('".$_GET["value"]."')";
// Execute SQL statement
mysqli_query($connect, $sql);
?>
the PHP seems to work perfectly fine when i just paste the url into the browser like so: the new data is inserted as a fresh row into the database with whatever value i enter. (it’s just two cols, one auto incrementing and one to hold the value.)
www.example.net/write-data.php?value=10
When I watch the Serial Monitor, I’m seeing it say
connecting...
done...
connecting...
done...
Never connection failed. So based on the code I wrote, it seems to be continuously reaching the server at least. Something about the GET isn’t working though? I don’t know. I feel like i’m so close.
Anyone have any thoughts?