1

I have an annoying problem with my Arduino and PHP server. I want send a value using PHP to port COM with Windows using a simple serial communication.

  1. I tried everything , even deleting the fclose of the PHP code, or change the value send of '1' > "1" or 1. But the communication of the Arduino never activated

  2. I put another LED for trying "debug" and that LED works but other doesn't.

The PHP code.

<?php
$fot = fopen("com1", "r+");
sleep(3);
fwrite($fot, '1');
?>

The arduino code

int numero = -5;
void setup() {
// put your setup code here, to run once:
Serial.begin(57600);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
}

void loop() {
if(Serial.available() > 0){
numero = Serial.read();
digitalWrite(11, HIGH);  
}
if(numero == "1"){
digitalWrite(12, HIGH);
}
Serial.println(numero);
delay(1000);
}
  1. the led on pin 11 works. And the entire code work on the serial monitor but with data send from php only the led 11 works.
halfer
  • 19,824
  • 17
  • 99
  • 186
Geeh Oliveira
  • 103
  • 2
  • 8
  • I don't know if Arduino is different, but in C `if(numero == "1")` will not work, since you cannot compare the content of a string directly. Try `if(numero == '1')`. – Weather Vane Jul 16 '16 at 14:15
  • Yeah youre right , but i already try, '1', "1" and 1. And dont work too, the correct way is '1' but no efect :(. Thanks for the answer man :D – Geeh Oliveira Jul 16 '16 at 14:18
  • Where do you configure the com port being used by PHP? What value gets sent back by `Serial.println(numero);`? – Weather Vane Jul 16 '16 at 14:31
  • In the console, the println on loop with normal execution return a lot of -5, the 2 leds works the console return the number of 1 after return a lot of "10 10 10 10 10 10 " – Geeh Oliveira Jul 16 '16 at 14:38
  • The console works everything fine the code. Strage is in php , the led 11 works but the comparation of the led 12 dont. I think was something like the type of number in php, but i try everything e do not work. – Geeh Oliveira Jul 16 '16 at 14:40
  • Please show, in the question, exactly how you have configured the Windows com port, before the PHP transmits. – Weather Vane Jul 16 '16 at 14:46
  • Please have a look at [this answer](http://stackoverflow.com/a/1074273/4142924) from @GiorgosPap. – Weather Vane Jul 16 '16 at 14:54
  • fot = fopen("com1", "w+"); – Geeh Oliveira Jul 16 '16 at 14:57
  • I tried change the "COM1", 'com1' as the upper case, in arduino ide i check the port, and tried to change the port too, in the windows. Triend change the serial begin value too. Was 9600 but even the 57600 speed dont work. – Geeh Oliveira Jul 16 '16 at 14:59
  • The windows com port has to be configured. You have not shown *in the question* how you have configured it. This is a separate operation from opening the port. If you have not configured it, please see the answer I linked to. Out. – Weather Vane Jul 16 '16 at 15:07
  • That $output on this answer should be like $output = ' ` mode COM1: BAUD=115200 PARITY=N data=8 stop=1 XON=off TO=on ` ' ; ? Cause without the '' this is wrong fatal erro here. – Geeh Oliveira Jul 16 '16 at 15:10
  • Arduino is **not** C. Don't spam tags. – too honest for this site Jul 16 '16 at 17:01
  • @Olaf Actually it is. An Arduino program is based on a template that takes care of the main() function and includes some libraries, but apart from that you are just writing C. – GolezTrol Jul 23 '16 at 12:29
  • @GolezTrol: Please read the Arduino homepage before stating such nonsense! It is **based** on **C++**, not C. C++ is **not** C- they are different languages. Due to the mofifications, it is also not fully compliant to the C++ standard. A simple look at the code and occam's razor would have shown it is not C: C does not support methods like the code above. – too honest for this site Jul 23 '16 at 12:32
  • They actually say it's "C/C++", suggesting both are true. From the [FAQ](https://www.arduino.cc/en/Main/FAQ): *"Can I program the Arduino board in C? In fact, you already are; the Arduino language is merely a set of C/C++ functions that can be called from your code. [blabla] passed directly to a C/C++ compiler"*. But even if I would be wrong, your first comment could have been *"Arduino is not C, it's based on C++"* instead of *"Arduino if not C. Don't spam tags"*. Just as much text, knowledge you apparently had anyway, and it would have prevented this confusion. – GolezTrol Jul 24 '16 at 11:09

0 Answers0