I'm trying with Arduino and PHP. I can turn led on, but I can't turn it off.
Arduino:
const int ledPin = 9;
int incomingByte;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop() {
if (Serial.available() > 0) {
incomingByte = Serial.read();
if (incomingByte == 'L') {
digitalWrite(ledPin, LOW);
}
if (incomingByte == 'H') {
digitalWrite(ledPin, HIGH);
}
}
}
PHP:
<?php
include "php_serial.class.php";
$serial = new phpSerial;
$serial->deviceSet("COM7");
$serial->confBaudRate(9600);
$serial->confParity("none");
$serial->confCharacterLength(1);
$serial->confStopBits(1);
$serial->confFlowControl("none");
$serial->deviceOpen();
$serial->sendMessage('H');
$serial->deviceClose();
?>
I tried to invert H and L in Arduino code (H was turning off and L was turning on). With inverted code I can't turn it on or off (other words, nothing was working). That tells me that letter "L" is not working. I replaced it with S and now I can turn led on and off. But why letter L doesn't work?
Every time you open the serial port, the Arduino resets. You need some delay between opening the serial port and sending data, to allow the Arduino time to reboot.
Ok, as you see that PHP code is sending one character at time. So I need to store all characters in a string. I think it's possible by saving in array with for loop and than converting array into string. I tried, but I can't make it. Actually, I can, but only if i know number of characters. Can someone suggest me something?
Actually, I can, but only if i know number of characters. Can someone suggest me something?
Sure. Send some known end-of-packet marker from PHP. Have the Arduino just keep reading characters until that marker arrives.
This is a packet!
Stop reading when the exclamation point arrives!
Then, do something with the data received!
Make sure to check for room in the array, before adding the new character!
I recognized each and every one of those words. Comprehending what they mean in that order escapes me.
If you are asking whether EOT/4 can be used as the end-of-packet marker, the answer is yes. I prefer a visual character, for printing purposes, but the marker does not have to be a printable character.
Take pencil and a piece of paper. Write down the values that should be in serial, serial_last, i, and array, when the string "OFF" is sent to the serial port.