I am using a Arduino Uno Wifi Board and is currently connected to my PC with USB. I have already set up the wifi connection of Arduino Uno Wifi with the guide that they have given here: Arduino - Home
I have been trying to connect it to my House Wifi but could never get a WL_Connected() Status. My Serial monitor just keeps repeating Not connected all the way. i do not know if it is my Arduino's problem or Wifi Problem or most likely my code's problem. Please help me out as this is my Final Year project and i am dead stuck in this area. If there is a need for clarification in any areas, do tell me and i will try my best to explain it.
This is my Code<Sorry for not cleaning up the includes, quite stressed and will clean it up once done>:
IPAddress server_addr(x,x,x,x); // IP of the MySQL server here
char user[] = "xxxxx"; // MySQL user login username
char password[] = "xxxxx"; // MySQL user login password
char ssid[] = " xxxxx"; // for wifi
char pass[] = "xxxxx"; // for wifi
// Sample query
char UPDATE_SQL[] = "UPDATE plant Set temperature = '100' where plant_id = '1'";
EthernetClient client;
MySQL_Connection conn((Client *)&client);
void setup() {
Serial.begin(9600); //your baud rate may be different, usually it is either 115200 or 9600 depending on device
while (!Serial); //wait for serial port to connect
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print("Not Connected");
Serial.print(".");
}
Serial.println("WiFi connected");
if (conn.connect(server_addr, 3306, user, password)) {
delay(1000);
}
else
Serial.println("Connection failed.");
}
At first glance, your code looks alright. May be a stupid question, but are you sure that your WiFi SSID and passphrase are correct? Keep in mind that they are case sensitive.
It's not a good idea to leave your MySQL server open to connections from the network/internet. You should only allow local connections from within the server (i.e. from PHP or other server side script interpreters). If you allow connections from outside of the server, you expose yourself to serious security threats.
It's a good idea to use a PHP script that receives HTTP requests, does some error checking (are all values present?), escapes the input (to prevent SQL injection), connects to the database, and inserts the values into the right table.
For example:
<?php
try { // connect to database
$db = new PDO('mysql:host=localhost;dbname=IoT;charset=utf8mb4',
'MySQL username', 'password',
array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
} catch(PDOException $ex) {
http_response_code(500);
echo "Unable to connect to the database";
error_log($ex->getMessage());
die();
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (!isset($_POST["time"]) or !isset($_POST["value"])) { // check if request contains 'time' and 'value' data
http_response_code(400); // bad request
}
try { // insert new values into database
$stmt = $db->prepare("INSERT INTO sensordata (time, value) VALUES (:t, :v);");
$stmt->bindValue(':t', $_POST["time"], PDO::PARAM_INT);
$stmt->bindValue(':v', $_POST["value"], PDO::PARAM_STR);
$stmt->execute();
} catch(PDOException $ex) {
http_response_code(500);
echo "Can't insert data into database";
error_log($ex->getMessage());
}
} elseif ($_SERVER['REQUEST_METHOD'] === 'GET') { // display the table as a webpage
try {
echo "<html><body><table>\r\n<tr><th>Index</th><th>Time</th><th>Value</th></tr>\r\n";
foreach($db->query("SELECT * FROM sensordata") as $row) {
echo "<tr><td>".$row['id']."</td><td>".$row['time']."</td><td>".$row['value']."</td></tr>\r\n";
}
echo "</table>";
} catch(PDOException $ex) {
echo "</table>\r\nUnable to retrieve data from database\r\n";
error_log($ex->getMessage());
} finally {
echo "</body></html>";
}
}
?>
For the time being, security is not an issue as what im doing is just putting in the data to display. There is no sensitive data. My Wifi SSID and passphrase are correct as i am using a laptop and tested it out time and time again. If i do a Serial.println(WiFi.localIP()), it will give me the ip address of 0.0.0.0 instead of the ip address that i was suppose to connect to. For example, the network SSID ip address is 192.168.30.10. But it returns back 0.0.0.0.
Warning:
The Arduino UNO WiFi uses a particular library to manager the WiFi.
If you are using the Arduino 1.7.x then you need to use the ArduinoWiFi.h library that it is already included in the IDE.
Instead you are using the Arduino 1.8.x then you need to download the UNOWiFiDev.Edition library from Library Manager.
WiFi.localIP() returning 0.0.0.0 is not that surprising, because it can't possibly know the IP address without being connected first.
If by using the UNOWiFiDev.Edition library, there is a example of a RESTClient for my phpfiles. But i read them for a very long time but still do not get it as they are using ciao. Is it possible for you to explain some stuff for me regarding the example? Quite stressed out as i am running out of time. This is a code for GET request. I am able to test and this works but what i need is POST request. Which i cannot really find.
Code:
/*
File: RestClient.ino
This example makes an HTTP request after 10 seconds and shows the result both in
serial monitor and in the wifi console of the Arduino Uno WiFi.
Note: works only with Arduino Uno WiFi Developer Edition.