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?
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 Barbosa
@Matheus is sure he is the Arduino? Do the following open the CMD and type this:
mode com5:9600,n,8,1
– Guilherme Nascimento
"Incorrect device name - COM5". Well, I’m sure it’s Arduino, because here we only use Arduinos for hardware.
– Matheus Barbosa
@Matheus has no yellow exclamation on top of the Arduino icon in Driver Manager?
– Guilherme Nascimento
Not, print, Cmd
– Matheus Barbosa
@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)
– Guilherme Nascimento
I downloaded and tested, I did not receive any return value, but I was able to connect normally. What you think may be ?
– Matheus Barbosa
@Matheus try like this
fopen('\\.com5', 'r+b');
– Guilherme Nascimento
"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 Barbosa
@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".
– Guilherme Nascimento
@Matheus you wrote
\\.com5
or\\.COM5
?– Guilherme Nascimento
I tried both ways, also tried even with extra counter-bars. The result was the same.
– Matheus Barbosa
@Matheus your PHP server is on the same machine that Arduino is installed on?
– Guilherme Nascimento
Yes, I’m using a local server usbwebserver
– Matheus Barbosa
@Matheus, I’ll try to get you a test rig and I’ll let you know as soon as I can.
– Guilherme Nascimento