0

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?

closed as off-topic by gre_gor, jfpoilpret, Enric Blanco, KIIV, sa_leinad Apr 14 '17 at 13:11

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question does not appear to be about Arduino, within the scope defined in the help center." – gre_gor, jfpoilpret, Enric Blanco, KIIV, sa_leinad
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    To make your question easier to read and answer you should remove all the database- and time-related stuff from your PHP code and concentrate only on the communications with Arduino. This forum isn't the place to be debugging non-Arduino code. I suggest you create a function that tells the Arduino to ring the bell. You debug that by calling it directly and post that function here if you have problems with it. You can simply print out what you would pass to the function when debugging your database and other code. – Curt J. Sampson Apr 2 '17 at 14:10
  • You need to look at Serial.available() rather than blocking on Serial.read(). Also what happens if your code receives a space? b = b -48 = -16 and then for (int i = 0; i < -16; i++) it isn't going top work how you expect. – Code Gorilla Apr 3 '17 at 7:36
  • @Matt I have made sure that white space and alphabets are not sent and even if blank is sent nothing happens. – Dhruva Apr 3 '17 at 16:39
0

Sounds like the COM port is not setup. Opening the Arduino IDE probably does this automatically.

Try running the following command, only once, after turning on your pc to initialize the COM port.

mode COM3 BAUD=9600 PARITY=n DATA=8

Ps. I'm not a windows user, so this is an educated guess - reference.

Pps. I think you're winning the award for the most complicated way to ring a bell. :)

  • Thanks dude.. well do you have any other better methods of doing this? – Dhruva Apr 3 '17 at 16:40
  • Well I'm not exactly sure what your requirements are, I'll assume is to ring the bell based on information in the database. In that case you could eliminate the webpage and browser all together and write a command line application/daemon/service. You could still write it in php if you wanted, and it would be more accurate and less error prone than relying on the meta/refresh in a browser. – Mazaryk Apr 3 '17 at 16:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.