Senhores,

Estou fazendo testes no meu primeiro projeto de automação.

Entre uma dificuldade e outra tenho conseguindo resolver os problemas com  ajuda do que tenho lido aqui no forum. Por isso já gradeço.

No memento estou com problema para fazer o PHP ler as informações envidas via serial pelo Arduino.

Resolvi o "problema" de reset do Arduino quando acessamos via serial com um capacitor de 10uF entre o pino de reset e o GND.

Todas todos os comandos de leitura es escrita funcionam perfeitamente pelo Monitor Serial. Mas no PHP só consigo enviar comandos. Sempre que tento ler o https.exe trava e a porta serial fica presa, só consigo liberar se eu matar o processo httpd.exe.

Já fiz testes em 2 maquinas com windows 8.1 e uma com o windows 7. Irei testar com o XP em breve.

Apesar de não achar necessário devido ao capacitor de 10uF, Usei deley para fazer a leitura e nada.

Cheguei a capturar o trafego da porta serial e tudo funciona até o momento da leitura quando o trafego para.

Seguem os códigos:

<?php
exec("mode COM2: BAUD=9600 PARITY=N data=8 stop=1 xon=off ");
$port = fopen('COM2', 'w+');
fwrite($port, '9');
echo fgets($port);
fclose($port);


if(isset($_GET["1"])){
$port = fopen('COM2', 'w');
fwrite($port, '1');
fclose($port);
echo "LED VERMELHO";
}

if(isset($_GET["2"])){
$port = fopen('COM2', 'w');
fwrite($port, '2');
fclose($port);
echo "LED VERDE";
}

?>

ARDUINO:


//=======================================DECLARAÇÃO DE VARIÁVEIS

//====================CONSTANTES
const int LigaPC = 2;
const int LuzQuarto = 3;
const int LuzOficina = 4;
const int Interruptor = 13;

//VARIÁVEIS
char Leitura_Serial;
//network NB: Pins 10, 11, 12 and 13 are reserved for Ethernet module.

int PC_Button = 0;
int Ler_Interuptor = 1;
int Interruptor_Conut = 0;
int InterruptorState = LOW;
int LastInterruptorState = LOW;

int Status_Quarto = LOW;
int Status_Oficina = LOW;

//======Delay
unsigned long PCPowerButtonDelay = 0;
unsigned long HardShutdownDelay = 0;
unsigned long lastDebounceTime = 0;
int Action_Delay = 1300;
int Interruptor_Delay = 50;


void setup() {
// put your setup code here, to run once:
//=======================================================INICIA SERIAL
Serial.begin(9600);
//Serial.println("INICIANDO SKEYNET V1.1");

//=================================================INICIA PINOS

pinMode(LigaPC, OUTPUT);
digitalWrite(LigaPC,HIGH);

pinMode(Interruptor, INPUT);
digitalWrite(Interruptor,HIGH);

pinMode(LuzQuarto, OUTPUT);
digitalWrite(LuzQuarto,LOW);

pinMode(LuzOficina, OUTPUT);
digitalWrite(LuzOficina,LOW);
}

void loop() {
//====================================================INICIO DO LOOP

//==============================INTERRUPTOR


if (Serial.available()){ // Verificar se há caracteres disponíveis
//
Interruptor_Conut = Serial.read();
if (Interruptor_Conut == '9') {
if (digitalRead(LuzQuarto) == 1) Serial.println("Ligada");
else Serial.println("Desligada");
}

if (Interruptor_Conut != '9') Interruptor_Conut = Interruptor_Conut - 48;

}


/*Serial.println("Testando");
delay (1000); */
Ler_Interuptor = digitalRead(Interruptor);
if (Ler_Interuptor != LastInterruptorState) {
lastDebounceTime = millis();
}

//=====Debauce Interruptor
if ((unsigned long)(millis() - lastDebounceTime) >= Interruptor_Delay) {
if (Ler_Interuptor != InterruptorState){
if (Ler_Interuptor == 0) {
Interruptor_Conut++;
}
InterruptorState = Ler_Interuptor;
}
}

//=====Ações interruptor
if (Interruptor_Conut != 0) { //Só executará ações quando houver interaçã!!!!!
if ((unsigned long)(millis() - lastDebounceTime) >= Action_Delay) {

//==========CHAMA QUARTO
if (Interruptor_Conut == 1) QUARTO (Interruptor_Conut);

//==========OFICINA
if (Interruptor_Conut == 2) OFICINA (Interruptor_Conut);

//==========INVERTE STATUS
if (Interruptor_Conut == 3) {
QUARTO(Interruptor_Conut);
OFICINA(Interruptor_Conut);
}

//==========LIGAR COMPUTADOR
if (Interruptor_Conut == 5) {
PC_Button = 5;
INTERAGE(Interruptor_Conut);
PCPowerButtonDelay = millis();
}

//==========HARD SHUTDOWN
if (Interruptor_Conut == 6) {
PC_Button = 7;
INTERAGE(Interruptor_Conut);
HardShutdownDelay = millis();
}

//====Aquardando nova leitura
Interruptor_Conut=0;

}
}
//==============================FIM INTERRUPTOR

//=================================SETA VARIÁVEIS FINAIS
//====Setar Status das Luzes

LastInterruptorState = Ler_Interuptor;

//========================LIGA PC DELAY
if ((((unsigned long)(millis()) - PCPowerButtonDelay >= 1000)) && ((PC_Button == 5))) {
PC_Button = 99;
Serial.println("solta boto");
digitalWrite(LigaPC,HIGH);
}

//========================FORÇA DESLIGAMENTO DO PC DELAY
if ((((unsigned long)(millis()) - HardShutdownDelay >= 4000)) && ((PC_Button == 7))) {
PC_Button = 99;
Serial.println("solta bot33");
digitalWrite(LigaPC,HIGH);
}

}

//==============================FIM DO LOOP


//==============================FUNÇÕES

String QUARTO (int pop) {
Interruptor_Conut=0;
Status_Quarto = !Status_Quarto;
digitalWrite(LuzQuarto, Status_Quarto);
Serial.println("LUZ DO QUARTO");
}

String OFICINA (int pop) {
Interruptor_Conut=0;
Status_Oficina = !Status_Oficina;
digitalWrite(LuzOficina, Status_Oficina);
//Serial.println("LUZ DA OFICINA");
}

String INTERAGE (int pop) {
Interruptor_Conut=0;
digitalWrite(LigaPC,LOW);
//Serial.println("COMPUTADOR");
}

Exibições: 1806

Responder esta

Respostas a este tópico

Assim como "cada cliente é um cliente" (caso diferente), "cada sistema é um sistema".

Na minha opinião, dependendo do nível de sofisticação do sistema, deveria exigir que o cliente possuísse IP fixo e um delay de 10s seria inaceitável.

Por outro lado, com todo respeito, se é um sistema mais "fuleiro" e envolva energia elétrica eu diria para você nem comercializar pois por em risco a vida das pessoas não é nada legal. Outro ponto seria você pensar se tem capacidade técnica para caso ocorra algum imprevisto não deixar os clientes "no escuro" até encontrar um solução.

Caros,


Recomendo a todos e ao autor do tópico que utilize a classe PHPSerial (http://www.phpclasses.org/browse/file/17926.html)

Utilizo ela para controlar um modem USB 3G/GSM e ela funciona muito bem para o que preciso (enviar comandos AT)

Segue meu código de exemplo;

<?php
require "php_serial.class.php";
$serial = new phpSerial;
$serial->deviceSet("COM8");
$serial->confBaudRate(115200);

$serial->deviceOpen();
$serial->sendMessage("AT+CMGF=1\n\r");
$serial->sendMessage("AT+cmgs=\"$numero\"\n\r");
$serial->sendMessage("$msg\n\r");
$serial->sendMessage(chr(26));
sleep(7);
$read=$serial->readPort();
$serial->deviceClose();

?>

Comigo ela funciona muito bem e as respostas se necessário eu pego no $read
Porém não é algo que eu preciso, então eu apenas guardo para debugs futuros.

Abs

RSS

© 2024   Criado por Marcelo Rodrigues.   Ativado por

Badges  |  Relatar um incidente  |  Termos de serviço