so please read this whole thing.
My Project: I am making a bell system that is based on PHP. The bell is connected to the arduino UNO via a relay and the arduino is connected to the computer via a USB cable. The interface with the database is built with PHP and MySql. Then we have the code which checks every minuit if the bell has to be rung and if it does send a numeric value to the arduino. There is also an option for a longer bell if we send a '0' to the arduino.
My Code: So this is the file that checks if bell has to rung and sends value to arduino
<?php
include('database.php');
$usb_comPort = "COM3";
date_default_timezone_set("Asia/Kolkata");
$time = date("H:i");
$time = trim($time);
$sql = "SELECT bell_amount FROM bell_db WHERE DATE_FORMAT(bell_time,'%H:%i') LIKE '$time'";
$result = mysqli_query($conn, $sql);
$numRows = mysqli_num_rows($result);
if ($numRows == 0) {
echo 'Error: Nothing found!';
} else {
$row = mysqli_fetch_assoc($result);
$bellAmount = $row['bell_amount'];
$runCommand = "ECHO " . $bellAmount . " > $usb_comPort";
echo $runCommand;
exec($runCommand);
}
?>
<html>
<meta http-equiv="refresh" content="60">
<body>
</body>
</html>
and this is the arduino's code:
int b = 0;
void setup() {
Serial.begin(9600);
pinMode(10, OUTPUT); //relay
}
void loop() {
b = Serial.read();
b = b - 48;
if(b == 0){
digitalWrite(10,HIGH);
delay(3000);
digitalWrite(10,LOW);
}
else{
for (int i=0; i < b; i++){
digitalWrite(10, HIGH);
delay(200);
digitalWrite(10, LOW);
delay(200);
}
}
}
The Issue that i am facing: My aim is that the code should run automatically when the computer is turned on. The code works fine but there is random bug. The code is uploaded to the arduino. When the computer is turned on i want that the system should work but when i run the file (the one that checks if there is a bell to be rung or not ) it processes the code but it is not able to send a value to the com port so the bell does not ring(the serial light on the arduino does not flash). But if i go to the arduino ide and send a value through the serial monitor once then run the php file, it works!
I am running this on a windows 10 computer, i have an arduino UNO and i have tried this with 2 arduino's same result on both.
So anyone know what i can do?
Serial.available()rather than blocking onSerial.read(). Also what happens if your code receives a space?b = b -48 = -16and thenfor (int i = 0; i < -16; i++)it isn't going top work how you expect.