Post temp value using WiFiNINA to SQL database using Uno Wifi REV 2

Hey all! I am really hoping you can assist me. I have been working on this for weeks!

I have been testing and trying to post my temp values from my Arduino to an SQL database over wifi. For some reason, the values cannot be sent! Every it connects, it just write blanks in the SQL server. May you can see something I am not?

#include <SPI.h>
#include <WiFiNINA.h>
// EDIT: Change the 'ssid' and 'password' to match your network
char ssid[] = "";  // wireless network name
char password[] = ""; // wireless password
int status = WL_IDLE_STATUS;
WiFiClient client;

//String query = "temp=2300&humid=5000";


void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  // this check is only needed on the Leonardo:
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  // check for the WiFi module:
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true);
  }
  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network:
    status = WiFi.begin(ssid, password);

    // wait 10 seconds for connection:
    delay(10000);
  }
  //pinMode(7, OUTPUT);


}

void loop()
{
  
  int temp;
temp = 1;

client.print(temp);
Serial.print("Yeah... it ain't holding");
 //int temp = 4;
 // int humid = 5;

  if (client.connect("please put ip for your local host", 80))
  {
    // REPLACE WITH YOUR SERVER ADDRESS
    Serial.println("connected");
    client.println("GET /insertmysql.php?temp=");
    //client.println("?temp=");
    client.println("4");
    //client.println("Humid=");
    //client.println(humid);
    client.println(" HTTP/1.1");
    client.println("Host: 10.0.0.56");
    //client.println("Content-Type: application/x-www-form-urlencoded; charset=UTF-8");
    client.println();
    //client.print(query);
    //client.print("Content-Length: ");
    //int thisLength = query.length();
    //client.println(thisLength);
    
  }
  delay(2000);
  Serial.println("Response: ");
  while (client.connected() || client.available())
  {
    Serial.write(client.read());
  }

}
<?php
error_reporting(E_ALL);

$tempf = $_POST["temp"];
/*$humid = $_POST["humid"];
*/

/*$temp = "";
$humid = "";

if (isset($_GET['temp'])){
   $temp = $_GET['temp'];
   echo $temp;
}
else{
   echo "Data not received\n";
}

if (isset($_GET['humid'])){
   $temp = $_GET['humid'];
   echo $humid;
}
else{
   echo "Data not received\n";
}
*/
echo("at least something\n");
//echo json_encode($_POST);
// EDIT: Your mysql database; account information
$servername = "";
$username = "";
$password = "";
$dbname = "";
//$test = 100;
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (mysqli_connect_error()) {
    die("Connection failed: " . mysqli_connect_error());
}


//$temp = mysqli_real_escape_string($conn, $temp);



$sql = "INSERT INTO alldata (temp) VALUES ('$temp')"; 
echo($sql);
if (mysqli_query($conn, $sql)) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "
" . mysqli_error($conn);
}

//echo 'GET temp = ' . htmlspecialchars($_GET["temp"]) . '';

mysqli_close($conn);
?>

Here are a few things I noted about your code. I will keep it short. Let me know when any point is unclear.

  • you make use of the delay() function. This is generally a bad idea but especially with communication stacks. They need to be executed all the time so they can react to data coming in and sending data when required.

Have a look at the following example on how to control timing without delay()

File -> Examples -> 02.Digital -> BlinkWithoutDelay

  • What is temp? A temporary value? Use proper variable names they are for humans not for the compiler.

  • The first thing you do in loop is print to a client without knowing whether there is one.

  • Your loop() function should be executed as fast as possible many hundreds/thousands of times a second, that is why it is called loop.

  • You make use of while, try to avoid that, it can block your code. In your case it stops loop from running continuously.

  • while (client.connected() || client.available()) This is true when one OR the other is true. If nothing is available from the client there is no need to read from the client

  • Serial.write(client.read()); Try to avoid combining function calls. Store everything in a local variable and then use that. This will make your understanding and reasoning clear. Do you know what client.read() returns? When you store it in a local variable the reader of your code (and you and the compiler) will know and can tell you when you might be wrong. The compiler will optimize local variables away. So, do not worry about using them.

  • To get the best feedback from the compiler. Make sure you have ALL warnings on. In the Arduino IDE go to

File -> Preferences on the "Settings" Tab make sure "Compiler warnings" is set to ALL.

  • for ssid = "" and password = "" use the arduino_secrets.h used by all examples. This will allow others to just use their own files when they want to try your example and prevents you from accidentally sharing your data.

  • Serial.print("Yeah... it ain't holding"); Avoid unnecessary code. It is just a distraction.

  • client.connect("please put ip for your local host", 80) What is this supposed to do?

In the long run you will need to get all your WiFi code into one place including everything required for setup and execute it from loop(). Right now, you assume your WiFi connecting will work after you have started it. That is not the case. Your WiFi will disconnect at some point. A few disconnects per day is normal in an average household WiFi network. Your code will need to restart the connection when it is lost.

I recommend you play a bit with the library examples and get more familiar with all the techniques used.

Where is the SQL server and where is the PHP stuff running? I do not know too much about this part because I use other protocols and software. Mainly MQTT, Node-RED and influxdb.

If that server is under your control you could run Wireshark on that machine to see what data you send to the server. Wireshark is the best free open source network analyzer available.

So I cleaned up the Arduino code as you pointed out in some cases. My issue is not the connection itself. I am able to connect to my local host and everything and even write within the data table. Problem is, it's writing blanks.
I also took out my Wifi name and password because of the personal info, and I took out my IP address and replaced with "please put your own"

#include <SPI.h>
#include <WiFiNINA.h>
// EDIT: Change the 'ssid' and 'password' to match your network
char ssid[] = "";  // wireless network name
char password[] = ""; // wireless password
int status = WL_IDLE_STATUS;
WiFiClient client;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  // this check is only needed on the Leonardo:
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  // check for the WiFi module:
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true);
  }

  
  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network:
    status = WiFi.begin(ssid, password);

  }

}

void loop()
{
  
  int temperature;
temperature = 1;

client.print(temp);
 

  if (client.connect("please put ip for your local host", 80))
  {
    // REPLACE WITH YOUR SERVER ADDRESS
    Serial.println("connected");
    client.println("GET /insertmysql.php?temp=");
    client.println(" HTTP/1.1");
    client.println("Host: 10.0.0.56");
    client.println();
    
  }
  delay(2000);
  Serial.println("Response: ");
  while (client.connected() || client.available())
  {
    Serial.write(client.read());
  }

}

See the blanks? The 55 is one I tested on the webpage.

unknown.png

unknown.png