cannot receive serial command via php

I'm new to Arduino and what I'm trying to do is send a float as a string from php to the Arduino. I'm using Arduino UNO.

It will parse the float from the string and compare it. If it matches, pin 13 will flash. So far, I've done all I can such as add a delay after opening the port in php but it doesn't seem to work.

Arduino Code

float val;
void setup() {

  Serial.begin(9600);
  
  pinMode(13,OUTPUT);
  digitalWrite(13,LOW);
  val=29.1; //default float value

  Serial.println("Start!");
}

void loop() {

  if(Serial.available()){
    String temp = Serial.readString(); //read string
    val = temp.toFloat(); //convert string to float
  }

  if(val>60){
    digitalWrite(13,HIGH);  
  }else{
    digitalWrite(13,LOW);  
  }

  Serial.println(val);
  delay(1000);
  
}

PHP Code

<?php
$port = fopen("COM3", "w");
sleep(2);
fwrite($port, "62.1");
fclose($port);
?>

Any tips would be much appreciated. Thank you for reading.

EDIT: I got it working. Apparently, I need to open the serial monitor in the Arduino IDE then run the script. Only then it will work.

Does the PHP fwrite() function terminate what it sends with the newline character that readStringUntil() is looking for ?

Arduino floats have more than 2 decimal places but a comparison using == will test for an exact match. Try printing val with more than 2 decimal places before you test its value. What do you see ?

Also note that in the case of most Arduinos opening the Serial port causes a reset

From what I am reading, fwrite() terminates at the EOF according to this. I'm not sure how it will work with a serial port.

For the comparison, I figure if it is more than the value say 62.1, then it would be like val>60.

I see. I've read about that. I understand it is to prep the Arduino for new code.

Then a further question, how do I send new data to the Arduino without causing it to reset?

how do I send new data to the Arduino without causing it to reset?

Leave the port open between writes

Which Arduino board are you using ? If you are using an AVR based Arduino then you cannot sensibly use pins 0 and 1 for serial communications if that is what you are doing

Please describe, or better still post a schematic, of how your Arduino is connected to whatever is running the PHP code.

62.1 is certainly larger than 60 but if you are putting the value received into a String then you could compare "60.1" with "60.1" directly

Apologies. I'm currently using an Arduino UNO.

I'm using the USB serial connection to communicate between the board and my computer, not the 1 or 0 pins.

My board is bare bones at the moment. I just want to test out serial communications.

That is true. After receiving the string, I would convert it to float using the toFloat() function.

I'm using the USB serial connection to communicate between the board and my computer, not the 1 or 0 pins.

On a Uno USB Serial comms uses pins 0 and 1

If I understand correctly you upload code using COM3: and PHP is writing to COM3:
Is that correct ?

Have you tried printing the value of val or temp ?

What happens if you use the Serial monitor to input a value instead of PHP ? Note that you can change the line ending value used by the Serial monitor

Yes.

When I do, I open the serial monitor and the Arduino resets, sadly.

Yes, it works perfectly fine if I enter the value through the serial monitor from the Arduino IDE.

When I do, I open the serial monitor and the Arduino resets, sadly.

If this is when you try to print the values then open the Serial monitor before sending the values

It seems that only after I open the serial monitor in the Arduino IDE and close it. Then run the php script. It works. So weird.

As pointed out earlier the PHP script and the Serial monitor are using the same COM port. That is very unlikely to work

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.