ROBOTDYN UNO R3 + ENC28J60 + MySQL


Resume:

When the sensor PIR gets active the RobotDyn + ENC28J60 will send this status to a data base MySQL.



Materials:
  • RobotDyn UNO R3
  • ENC28J60
  • PIR
  • Arduino IDE
  • Library
  • Driver

Pin layout:
Pin layout connections ENC28J60 to RobotDyn:
ENC28J60
RobotDyn UNO R3
SO
Pin 12
SCK
Pin13
ST
Pin 8
CS
Pin10
GND
GND
Vcc
+3V3

Pin layout connections sensor PIR to RobotDyn:
Sensor PIR
RobotDyn UNO R3
Vcc
5V
GND
GND
Out
Pin 2








Arduino IDE:
If you haven't installed the IDE of Arduino yet, you can get it from here. You can use the offline or online option.

Library UIPEthernet:
We'll use the  library UIPEthernet for ENC28J60, it can be downloaded here

Driver CH340G:
Also we'll need the right driver, if needed then check this link

MySQL:
Create a tablet called "table" like below:

PHP:
Create a php called "index.php" like below:
<?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:
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!");
       }      
   }


Result:
If the sensor detects presence then turn on the lamp and save these events in the data base.


 









Popular posts from this blog

Smart Home - Part 1

nRF52 - Hola mundo