Amigos, boa tarde. Sou novo com arduino, e estou com uma dúvida!
Eu tenho um código onde ligo e desligo o relê, pela internet e queria saber como fazer o PHP recuperar um valor do arduino!
Arduino.ino
#include <SPI.h>
#include <Ethernet.h>
// Dados da rede
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 99 }; // Ip do arduino
byte gateway[] = { 196,168, 1, 1 };
byte subnet[] = {255, 255, 255, 0};
EthernetServer server(81); // Porta do arduino
int pin = 5; //RELÊ NO PINO 5
void setup()
{
Serial.begin(9600);
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
delay(1000);
Serial.println("Setup complete");
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
}
void loop ()
{
EthernetClient client = server.available();
if (client) {
Serial.println("Cliente conectado");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.print(c);
//Execute code based on the package the server sent
if (c == '1')
{
Serial.println("ON");
digitalWrite(pin, HIGH);
}
if (c == '0')
{
Serial.println("OFF");
digitalWrite(pin, LOW);
}
if (c == '9')//Check the state of Pin 7 and send that to the connected clent
{
Serial.println("CHECKSTATUS");
if (digitalRead(pin)== LOW)
{server.write("OFF");}
if (digitalRead(pin)== HIGH)
{server.write("ON");}
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("Cliente Desconectado");
}
}
PHP
<?php
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
// Se conecta ao IP e Porta:
socket_connect($sock,"192.168.1.99", 81);
if (isset($_POST['ligarele'])) {
socket_write($sock,1);
}
if (isset($_POST['desligarele'])) {
socket_write($sock,0);
}
if (isset($_POST['verifica'])) {
socket_write($sock,9);
}
?>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<body>
<form action="" method="post">
<input type="submit" name="ligarele" value="Liga Relê" />
<input type="submit" name="desligarele" value="Desliga Relê" />
<br />
<input type="submit" id="ver" name="verifica" value="Verifica Relê" />
</form>
<div id="content"></div>
</body>
</html>
Desde já agradeço!