Problems making Arduino serial connection with PHP

Asked

Viewed 322 times

1

I’m trying to make a thermometer with PHP, in Windows 10, and I’m capturing the data through an Arduino with code:

#include <OneWire.h>

#include <DallasTemperature.h>

// Conectar o pino central dos sensores ao pino 10 do Arduino
#define ONE_WIRE_BUS 10

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);
DeviceAddress thermometerOne = { 0x28, 0xFF, 0x72, 0xC0, 0x62, 0x15, 0x01, 0x38 };
DeviceAddress thermometerTwo = { 0x28, 0xFF, 0xFA, 0x65, 0x72, 0x15, 0x02, 0x4D };

void setup(void)
{
  // start serial port
  Serial.begin(9600);
  // Start up the library
  sensors.begin();
  // set the resolution to 10 bit (good enough?)
  sensors.setResolution(thermometerOne, 12);
  sensors.setResolution(thermometerTwo, 12);
}

void getTemp(){
  printTemperature(thermometerOne);
  Serial.print(" ");
  printTemperature(thermometerTwo);
}

void printTemperature(DeviceAddress deviceAddress)
{ 
  sensors.requestTemperatures();
  float tempC = sensors.getTempC(deviceAddress);
  if (tempC == -127.00) 
  {
    Serial.print("Erro");
  } 
  else 
  {
    Serial.print(tempC, 4);
  }
}

void IDFuncao(String funcao){
 if (funcao == String("temp")){
  getTemp(); 
 }
}

void decodificaMensagem(String mensagem){
  int i;
  String funcao = "";
  for (i=1; i<=4; i++){
    funcao = funcao + String(mensagem[i]);
  }
  IDFuncao(funcao);
}

void loop(void)
{ 
  //Verificando se tem algo na Porta
  if (Serial.available() > 0){
    String mensagem = Serial.readString();
    String inicio = String(mensagem[0]);

    //Verificando se o que eu recebi começa com #
    if (inicio == String("#")){
      decodificaMensagem(mensagem);
    }
  }
}

I already researched several ways to capture the data in PHP like this, using phpserial: Php Serial

include 'PhpSerial.php';

$serial = new PhpSerial;

$serial->deviceSet("COM5");
$serial->confBaudRate(9600);
$serial->confParity("none");
$serial->confCharacterLength(8);
$serial->confStopBits(1);
$serial->confFlowControl("none");    

$serial->deviceOpen();

sleep (1);
$serial->sendMessage("#");

global $ler_serial;
$ler_serial = $serial->readPort();

$serial->deviceClose();

but I get that message:

Warning: Unable to open the device [...]

And making:

$port = "COM5";
$fp = fopen($port, "r+b");
//sleep(1);
fwrite($fp,'#');
print_r($fp);
print_r(fgets($fp));
fclose($fp);

receiving:

Warning: fopen(COM5): failed to open stream: Permission denied

A friend of mine who made the Arduino code, he said I needed to send an '#' to the Arduino, as if it were a protocol, sending the '#', or not, the result is the same.

What can I do to receive the temperatures captured by the Arduino?

1 answer

1

This seems to indicate only that the door COM5 is not the correct port, or this off, to check the correct port open the driver manager, then go to COM & LPT Something like:

porta

If nothing appears in Portas (COM & LPT) (or Ports COM & LPT in systems in English) means that there is some problem in the connection of the equipment and maybe it is in other devices, see if there’s anything there with an exclamation symbol on a yellow icon, for example:

falha

This means that some driver is missing from your system, or that there is some hardware failure.

If you are on Ports/Ports and are running normally, but not is displayed the door between (COM ...) then just right click the mouse/mouse on it and click Properties/Properties

propriedades

If everything is correct and there is no yellow icon with exclamation then you can perform the port test using software like Hyperterminal, however this software does not seem to exist for Windows10 (maybe exists on windows-server), as an alternative can download the:

Open the program and click Set Up > Port Configuration, or simply press Alt+C, will open this window:

port

Watch in the Box Port:, see if the door appears COM5, if it does not appear it is because it is not connected, if it appears select it and click the button next called Connect, if any error occurs is some problem in the Hardware or problem in the configuration of the same.

  • Is appearing normally "USB Serial Device (COM 5)". This hardware was used in another program (along with the same Arduino code I put here)

  • @Matheus is sure he is the Arduino? Do the following open the CMD and type this: mode com5:9600,n,8,1

  • "Incorrect device name - COM5". Well, I’m sure it’s Arduino, because here we only use Arduinos for hardware.

  • @Matheus has no yellow exclamation on top of the Arduino icon in Driver Manager?

  • Not, print, Cmd

  • @Matheus edited the answer and put a step-by-step for you to follow on how to test the port with Hype! Terminal (Serial Port Commport)

  • I downloaded and tested, I did not receive any return value, but I was able to connect normally. What you think may be ?

  • @Matheus try like this fopen('\\.com5', 'r+b');

  • "Warning: fopen(.COM5): failed to open stream: No such file or directory" There is a counter-bar appearing, but I put two in fopen.

  • @Matheus open the hype! Terminal, go to Setup and select the COM5 port, click connect and then close the setup window, in the main window, see where this written command:, type something there and try and click "Send".

  • @Matheus you wrote \\.com5 or \\.COM5?

  • I tried both ways, also tried even with extra counter-bars. The result was the same.

  • @Matheus your PHP server is on the same machine that Arduino is installed on?

  • Yes, I’m using a local server usbwebserver

  • @Matheus, I’ll try to get you a test rig and I’ll let you know as soon as I can.

Show 10 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.