Arduino Uno and PHP

Hello,

I’m noob on the arduino world. I buyed an Arduino Uno set and I’m doing test with some leds.

I want to turn on/off a LED using a relay in the output 13. It works well when send signal (B = HIGHT or b = LOW) from Arduino IDE but have and strange behavior when move to PHP. The final purpose is handle some lights by PHP and a light sensor since a RaspberryPi.

My arduino code is this

#include <BH1750.h>
#include <Wire.h>

 
BH1750 luxometro;
 
const byte luxMode = BH1750_CONTINUOUS_HIGH_RES_MODE;
// BH1750_CONTINUOUS_HIGH_RES_MODE
// BH1750_CONTINUOUS_HIGH_RES_MODE_2
// BH1750_CONTINUOUS_LOW_RES_MODE
// BH1750_ONE_TIME_HIGH_RES_MODE
// BH1750_ONE_TIME_HIGH_RES_MODE_2
// BH1750_ONE_TIME_LOW_RES_MODE

int output13 = 13;
char data[4];

void setup () {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(output13, OUTPUT); //LED 13 como salida
  Serial.begin(9600); //Inicializo el puerto serial a 9600 baudios

  Serial.println(F("Inicializando sensor..."));
  luxometro.begin(luxMode); // Inicializar BH1750
}

void loop () {
  if (Serial.available()) { //Si está disponible
      char c = Serial.read(); //Guardamos la lectura en una variable char
      uint16_t lux = 0;
      
      switch (c){
          case 'a':
            digitalWrite(LED_BUILTIN, LOW);
            Serial.println(F("Apagando LED_BUILTIN..."));
            break;
          case 'b':            
            digitalWrite(output13, LOW);
            Serial.println(F("Apagando Output13..."));
            break;    
          case 'A':
            digitalWrite(LED_BUILTIN, HIGH);
            Serial.println(F("Encendiendo LED_BUILTIN..."));
            break;
          case 'B':            
            digitalWrite(output13, HIGH);
            Serial.println(F("Encendiendo Output13..."));
            break;
          case 'S':
            lux = luxometro.readLightLevel(); // Lectura del BH1750
            Serial.print(F("Iluminancia:  "));
            Serial.print(lux);
            Serial.println(" lx");          
            break; 
          default: 
            // if nothing else matches, do the default
            // default is optional
            Serial.println(F("Parametro no reconozido"));
          break;
        }      
  }  
}

And my PHP code, using PhpSerial is this

       // Let's start the class
        $serial = new PhpSerial;
        // First we must specify the device. This works on both linux and windows (if
        // your linux serial device is /dev/ttyS0 for COM1, etc)
        $serial->deviceSet("/dev/ttyUSB0");
        // We can change the baud rate, parity, length, stop bits, flow control
        $serial->confBaudRate(9600);
        $serial->confParity("none");
        $serial->confCharacterLength(8);
        $serial->confStopBits(1);
        $serial->confFlowControl("none");

        // Then we need to open it
        $serial->deviceOpen();

        sleep(5);

        // Read data
        $read = $serial->sendMessage(chr("B\r"));

        sleep(5);

        // Or to read from
        $read = $serial->readPort();

        sleep(5);

        // If you want to change the configuration, the device must be closed
        $serial->deviceClose();

        echo "<p>Response: $read</p>";

I put a capacitor between RST and GND to avoid autoreset. I connect my Arduino and RaspberryPi by a USB cable.

My output on $read value is this:

Response: Inicializando sensor... Parametro no reconozido Parametro no reconozido Parametro no reconozido Apagando Output12... Parametro no reconozido Apagando LED_BUILTIN... Parametro no reconozido Parametro no reconozido Parametro no reconozido Apagando LED_BUILTIN... Parametro no reconozido Apagando Output11... Parametro no reconozido Parametro no reconozido Parametro no reconozido Apagando Output10...

And go on over 100 lines

When send a command I get some blinks on leds. Arduino LEDs blinks, then 3 LEDs stay ON for a seconds and turn OFF until only one LED is ON.

When PIN13 is LOW and send a Hight request (B) after blicks LED stay turned ON. And, for some reason, LED_BUILTIN stay ON too.

What I doing wrong? Any advise about link o text to read?

Thanks.

I am not familiar with PHP but I suspect your problem is that your PHP code is opening and closing the serial port. The Uno will reset when the serial port is opened. Your PHP code should open the serial port, allow a few seconds for the Arduino to reset and then keep the serial port open until your PC is completely finished with the Arduino.

Have a look at how it is done in this Python - Arduino demo

Also have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R

Thanks for the reply and for the links. I'm reading both starting by second.

I put a 10uF capacitor between RST and GND to avoid reset. I think it works fine because output only show one time setup function execution. I could be a misunderstanding by myself about how to send / receive commands from PHP to Arduino because Arduino receive an input but show a wrong behavior.

I'm going to read this documentation and If find a solution then I will update thread.

Thanks.

Hello,

Finally I get some advance.

In the first place, it was true that I have a reset on each communication. After Robin2 response I put another LED on setup that turn on 1 second and then turn off. With this I notice that LED turn on on each communication. I review the capacitor between RST and GND and find this was wrong. After correct this I already have not reset on serial communication.

I back to basics and changed my code in arduino to this

#include <BH1750.h>
#include <Wire.h>

 
BH1750 luxometro;
 
const byte luxMode = BH1750_CONTINUOUS_HIGH_RES_MODE;
// BH1750_CONTINUOUS_HIGH_RES_MODE
// BH1750_CONTINUOUS_HIGH_RES_MODE_2
// BH1750_CONTINUOUS_LOW_RES_MODE
// BH1750_ONE_TIME_HIGH_RES_MODE
// BH1750_ONE_TIME_HIGH_RES_MODE_2
// BH1750_ONE_TIME_LOW_RES_MODE

int output13 = 13;
int output12 = 12;

char received;
boolean newData = false;

void setup () {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(output13, OUTPUT); //LED 13 como salida
  pinMode(output12, OUTPUT); //LED 13 como salida
  Serial.begin(9600); //Inicializo el puerto serial a 9600 baudios

  //Serial.println(F("Inicializando sensor..."));
  luxometro.begin(luxMode); // Inicializar BH1750

  blink();
}

void loop () {
  receive();
  handle();
}

void receive() {
    if (Serial.available() > 0) {
        received = Serial.read();
        newData  = true;
        delay(100);
    }
}

void handle() {
    if (newData == true) {
        newData = false;
        
        switch (received)
        {
          case 'b':
            digitalWrite(output13, LOW);
            break;
          case 'B': 
            digitalWrite(output13, HIGH);
            break;
          default: 
            Serial.print("Unrecognised operation ");
            Serial.println(received);          
            break;
        }
    }
}

bool blink()
{
  digitalWrite(output12, HIGH);
  delay(1000);
  digitalWrite(output12, LOW);

  return true;
}

On the other hand I have the strange behavior on Serial response. Too many data, outputs without sense and wrong behavior. I tried to send data directly from RaspberryPi terminal to arduino with this:

stty -F /dev/ttyUSB0 cs8 9600 ignbrk -brkint -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke noflsh -ixon -crtscts

echo 'B' > /dev/ttyUSB0

With this everything works OK and the LED turn on/off but, after this, my PHP communication already works!. So I suppose that my Serial configuration in PHP is wrong or the PhpSerial class is not capable to set up the serial port.

If anyone is interested this is my actual PHP code

        // Let's start the class
        $serial = new PhpSerial;
        // First we must specify the device. This works on both linux and windows (if
        // your linux serial device is /dev/ttyS0 for COM1, etc)
        // $serial->deviceSet("COM1");
        // Specify the device being used
        $serial->deviceSet("/dev/ttyUSB0");

        // Set baud rate
        $serial->confBaudRate(9600);
        $serial->confParity("none");
        $serial->confCharacterLength(1);
        $serial->confStopBits(1);
        $serial->confFlowControl("none");

        // Open the device
        $serial->deviceOpen();

        // Write to the device
        $serial->sendMessage($char);

        sleep(1);

        $read = $serial->readPort();

        // Close the port
        $serial->deviceClose();

        echo "<p>Response: $read</p>";

I will make test with other sleep values and request/responses.