ROBOTDYN UNO R3 + ENC28J60 + MySQL
Resumen:
Cuando el sensor PIR se active el RobotDyn + ENC28J60 enviarán ese evento a una base de datos MySQL.
Materiales:
- RobotDyn UNO R3 puedes usar un Arduino UNO
- ENC28J60
- PIR
- Arduino IDE
Conexión:
ENC28J60 a RobotDyn:
ENC28J60
|
RobotDyn
UNO R3
|
SO
|
Pin 12
|
SCK
|
Pin13
|
ST
|
Pin 8
|
CS
|
Pin10
|
GND
|
GND
|
Vcc
|
+3V3
|
Conexión sensor PIR a RobotDyn:
Sensor
PIR
|
RobotDyn
UNO R3
|
Vcc
|
5V
|
GND
|
GND
|
Out
|
Pin 2
|
Arduino IDE:
Si aun no tienes instalado el IDE de aArduino lo puedes hacer desde aquí. Una alternativa es utilizar la opción online.
Libreria UIPEthernet:
Usaremos la libreria UIPEthernet para ENC28J60, puede ser descargada desde aquí
Driver CH340G:
Puedes dercarga el driver desde aquí
MySQL:
Por favor, crear una tablla llamada: "table"y campos como la figura adjunta:
PHP:
Crear a php called "index.php" asi:
Arduino Code:<?php$conexion = mysql_connect("localhost", "user", "pass");mysql_select_db("antudb",$conexion);//include 'ConectarDB.php' ;mysql_query("INSERT INTO `table`(`valor`, `location`, `sensor`) VALUES ('" . $_GET['valor'] . "', '" . $_GET['location'] . "', '" . $_GET['sensor'] . "')", $conexion);?>
Arduino code below:
#include <UIPEthernet.h> // Used for Ethernet
const int led = 3 ;
const int sensor = 2 ;
byte mac[] = { 0x54, 0x34, 0x41, 0x30, 0x30, 0x31 };
EthernetClient client;
char server[] = "192.12.5.55";
int interval = 5000; // Wait between dumps
int valor = 0;
void setup()
{ pinMode( led , OUTPUT) ;
pinMode (sensor , INPUT);
Serial.begin(9600);
Ethernet.begin(mac);
Serial.println(" Sensor - v1.6");
Serial.println("****************************************\n");
Serial.print("IP Address : ");
Serial.println(Ethernet.localIP());
Serial.print("Subnet Mask : ");
Serial.println(Ethernet.subnetMask());
Serial.print("Default Gateway IP: ");
Serial.println(Ethernet.gatewayIP());
Serial.print("DNS Server IP : ");
Serial.println(Ethernet.dnsServerIP());
Serial.println("****************************************\n");
}
void loop()
{
if (digitalRead( sensor)) {
digitalWrite( led , LOW);
valor = 1;
Serial.println("Encendido!");
Serial.println("connecting...");
if (client.connect(server, 80)) {
Serial.println("-> Connected");
client.print( "GET /index.php??");
client.print("valor=");
client.print(valor);
client.print("&&");
client.print("location=");
client.print( "4th_floor" );
client.print("&&&");
client.print("sensor=");
client.print( "motion" );
client.println( " HTTP/1.1");
client.print( "Host: " );
client.println(server);
Serial.println("-> Dato saved en MySQL");
client.println( "Connection: close" );
client.println();
client.println();
client.stop();
} else {
Serial.println("--> connection failed/n");
}
delay(interval);
}
else {
digitalWrite( led , HIGH);
Serial.println("PIR stopped");
valor = 0;
Serial.println("Apagado!");
}
}
Resultado:
Si el sensor detecta presencia enciende la lámpara y guarda ese evento en nuestra base de datos.