Arduino UNO + Enc28j60 + DHT11 +PHP & Mysql (LAN MODE)

I'm a arduino newbie, I would like to build an web project for my thesis. I use arduino uno as microcontroller, DHT11 for temperature sensor and Enc28j60 for the ethernet shield. The project is used to controll LED and monitor the temperature and humidity through a website. I want to set arduino as client. I want to simulate this project in LAN mode. I want to ask: 1. how to set arduino as client? 2. how to receive the temperature data from the arduino and store it to the database(mysql)? 3. how to controll led via website?

I had tried arduino as server, and it succed. But I confuse how to set it as client thank you,

anyone can help me?

here's a simple example on how to use Arduino+enc28j60 in client-mode: https://github.com/ntruchsess/arduino_uip/blob/master/examples/TcpClient/TcpClient.ino

thank you for your respond :D. Is it possible that I use only one arduino for lamp controller and monitoring the temperature? how to call an php file from the localhost? I use xampp.

sure, that is possible.

One of the many options would be the use of ConfigurableFirmata for that. You'd have to include the example custom-feature class from DHT11Firmata to do that. But you would need a Firmata-php software that is able to controll everything. Thomas Weinert has written a Firmata-Host in php and he is activly developing it. It's quite new implementation and I'm not aware whether it supports the newer protocolls implemented in ConfigurableFirmata yet, but you might want to get in touch with him. A Firmata-host-software that does implement latest Firmata-features (including Ethernet-support) is the perl-firmata. And if your development is about creating Web-pages (on the xampp-machine) to controll the arduno, you could use one of the Firmata-hosts written in JavaScript as there are Breakout and johnny-five (cannot tell how mature they are, but Breakout is maintained by Jeff Hoeffs who is the main developer on the Firmata-project - so if anything is missing, I think he is willing to implement ;-)

I'm still confuse about it. Is there any other way to solve my project? I'm still newbie and don't understand about firmata. I have to finished this project this month.

Thank you

if you are not confident to use Firmata, you don't have to. It's just an option if you prefer not to invent your own protocol and framework. It's propably not the easiest starting point for a newbe if it's about a feature that requires extending firmata (and not just installing and using).

You are free to roll your own protocoll. I'd stongly suggest to make the Arduino the client that initiates the connection and not the server as it is easyer to implement (and less error-prone for a newbe). Es a starting-point you may take the UIPEthernet-client-code I mentioned before, and implement some Serverside php-code that is able to receive and parse what your arduino-client sends. After that you include OneWire (to mesure temperature with DS18B20) and DHT11-lib. That's doable in a month (including the learing-curve).

Where should I put the PHP file to store the temperature data? And how to get the temperature from the arduino to the php file? Actually I'm still confuse of the arduino's code.

Thank you

Did you try running the client-example of post #2? If you have troubble running this or need to know about specific details don't hasitate to ask.

If you have questions about 'how to write code with php' you better go and ask google for the appropriate forum as you might not get those answers here

yah I need the detail of it. I already could connect arduino to the server. The problem is I don't know how to send the data from arduino to the server. I already create the php code. I only need to get the variable temperature and humidity from arduino to the php. The last is I dont understand arduino's code for controlling the lamp if the arduino as webclient. Thank you for your help

I saw this forum. I use reply #14. But it doesnt work. Do you know why, ntruchsess?
I change my ethernet card from enc28j60 to wiznet ethernetcard.

And also already saw this http://winkleink.blogspot.com/2012/08/arduino-ethernet-ethercard-xamp-web.html
but the arduino’s code has error. The error is “‘Ethernet’ has not been declared.” Do you know why?
this code:

 #include <EtherCard.h>

// ethernet mac address - must be unique on your network
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
// ethernet interface ip address
static byte myip[] = { 192,168,1,5 };
// gateway ip address
static byte gwip[] = { 192,168,1,1 };

byte Ethernet::buffer[500]; // tcp/ip send and receive buffer

// Using a Variable for the Pin, but it is not necessary 
const int ledPin2 = 2;
const int ledPin4 = 4;


// Some stuff for responding to the request
char* on = "ON";
char* off = "OFF";
char* statusLabel;
char* buttonLabel;

// Small web page to return so the request is completed
char page[] PROGMEM =
"HTTP/1.0 503 Service Unavailable\r\n"
"Content-Type: text/html\r\n"
"Retry-After: 600\r\n"
"\r\n"
"<html>"
  "<head><title>"
    "Arduino 192.168.1.5"
  "</title></head>"
  "<body>"
    "<h3>Arduino 192.168.1.5</h3>"
  "</body>"
"</html>"
;

void setup(){
// Set Pin2 to be an Output
  pinMode(ledPin2, OUTPUT);
// Set Pin4 to be an Output
  pinMode(ledPin4, OUTPUT);

// Scary complex intializing of the EtherCard - I don't understand this stuff (yet0  
  ether.begin(sizeof Ethernet::buffer, mymac);
// Set IP using Static
  ether.staticSetup(myip, gwip);
}

void loop(){
  
  word len = ether.packetReceive();
  word pos = ether.packetLoop(len);

// IF LED2=ON turn it ON
  if(strstr((char *)Ethernet::buffer + pos, "GET /?LED2=ON") != 0) {
      Serial.println("Received ON command");
      digitalWrite(ledPin2, HIGH);
    }

// IF LED2=OFF turn it OFF  
    if(strstr((char *)Ethernet::buffer + pos, "GET /?LED2=OFF") != 0) {
      Serial.println("Received OFF command");
      digitalWrite(ledPin2, LOW);
    }

// IF LED4=ON turn it ON
  if(strstr((char *)Ethernet::buffer + pos, "GET /?LED4=ON") != 0) {
      Serial.println("Received ON command");
      digitalWrite(ledPin4, HIGH);
    }

// IF LED4=OFF turn it OFF  
    if(strstr((char *)Ethernet::buffer + pos, "GET /?LED4=OFF") != 0) {
      Serial.println("Received OFF command");
      digitalWrite(ledPin4, LOW);
    }

//Return a page so the request is completed.

    memcpy_P(ether.tcpOffset(), page, sizeof page);
    ether.httpServerReply(sizeof page - 1);
  
}

and this one is the html code

<html>
<head>
<title>Click to Turn on or OFF the LED in the background</title>
</head>
<body bgcolor="#FF9933">
<?php

// Check of LED2 is set.  If it is use it
if (isset($_POST["LED2"]))
{
$LED2= $_POST["LED2"];
//echo "<b>$LED2</b>";
}
else
{
$LED2 ="";
}
if ($LED2 == "ON")
{
// Set led2 ON by calling the Arduino using fopen
//ini_set("allow_url_fopen On", true);
$h = @fopen("http://192.168.1.5/?LED2=ON", "rb");
}
else if ($LED2 == "OFF")
{
// Set led2 OFF by calling the Arduino using fopen
//ini_set("allow_url_fopen On", true);
$h= @fopen("http://192.168.1.5/?LED2=OFF", "rb");
}

// Check of LED4 is set.  If it is use it
if (isset($_POST["LED4"]))
{
$LED4= $_POST["LED4"];
//echo "<b>LED4 is $LED4</b>";
}
else
{
$LED4 ="";
}
if ($LED4 == "ON")
{
// Set led4 ON by calling the Arduino using fopen
//ini_set("allow_url_fopen On", true);
$h = @fopen("http://192.168.1.5/?LED4=ON", "rb");
}
else if ($LED4 == "OFF")
{
// Set led4 OFF by calling the Arduino using fopen
//ini_set("allow_url_fopen On", true);
$h= @fopen("http://192.168.1.5/?LED4=OFF", "rb");
}

?>
<!-- LED2 FORM -->
<table>
<tr><td colspan="2"><font size="4" color="yellow">Turn on and off the LED2</font></H4></td></tr>
<tr><td>
<form action="led2.php" method="post">
<input type="hidden" name="LED2" value="ON">
<input type="submit" name="submit" value="ON">
</form>
</td><td>
<form action="led2.php" method="post">
<input type="hidden" name="LED2" value="OFF">
<input type="submit" name="submit" value="OFF">
</form>
</td></tr>
</table>

<table>
<tr><td colspan="2"><font size="4" color="green">Turn on and off the LED4</font></td></tr>
<tr><td>
<form action="led2.php" method="post">
<input type="hidden" name="LED4" value="ON">
<input type="submit" name="submit" value="ON">
</form>
</td><td>
<form action="led2.php" method="post">
<input type="hidden" name="LED4" value="OFF">
<input type="submit" name="submit" value="OFF">
</form>
</td></tr>
</table>


</body>
</html>

I cannot possible know why the Client-example you reference doesn't work if you don't tell any details about what it does and what it does not. And I cann only guess what the sketch might have written do your Serial-console when you tried it.

Don't know about the EtherCard, never used it... Hope you noticed and understood the difference in between the first example (Client - where the arduino is supposed to call the php on the Server and request the status from there) whereas the EtherCard-example works the other way around. (The php crafts a request to the Server that runs on the Arduino and sends a command command).

  • Norbert