I am having a problem in sending Arduino data into my database. I am currently using WAMPSERVER. I have an arduino mini w/ ATMEGA 328 and I am using ENC28J60. The problem is I have tried different libraries and examples to send data into the server but I failed. I just notice that some libraries are not compatible with my enc28J60. I have tried UIPEthernet.h, Ethernet.h, etherShield.h and EtherCard.h. The etherShield.h and EtherCard.h seemed to work just fine. But I prefer to use EtherCard.h because I heard etherShield is the older lib. I know a little php.

I think the things that might guide me is to see a working example of using the EtherCard.h library demonstrating the sending of sensor data from arduino into my database. The network setup I am currently working on is that, the ENC28J60 is connected in my home network with an ip address of 192.168.10.10. The server to which I placed the database is my laptop with an IP address of 192.168.10.2. I am placing the php files in this directory C:\wamp\www. I hope I have explained it well. I'm sorry for my English.

有帮助吗?

解决方案

I am currently working of a port of my homewatch server from Arduino Uno w Ethernet lib to AVR-NETIO with Ethercard lib (or UIPEthernet).

On the server side there is a Apache/PHP/MySQL server.

To update data on a webserver connected database, you can use POST or GET requests to a php web page. These data can then be added easily to a database and the same or another web page can also display the data of the database.

<?php
require 'dbhelp.php';

//header('Content-type: text/plain');
echo "<html>";
echo "<head>";
echo "<meta name='viewport' content='width=device-width, user-scalable=false, initial-scale=1;'>";
echo "</head>";
echo "<body>" .        "<h2>Temperatur und Luftfeuchte</h2>";


// GET requests are sent as a query string on the URL:
// GET index.html?name1=value&name2=value HTTP/1.1
// Host: about.com
// http://192.168.0.40/homewatch/index.php?channel=1&temp=165&humidity=80&datetime=010120131234
if($DEBUG){
       print("<pre>");
print_r($_GET);
print("</pre>");
}

openDB();

if( isset($_GET['channel']) && isset($_GET['temp']) && isset($_GET['humidity']) )
{
  if($DEBUG)
    echo "<p>addData()";
  $c=$_GET['channel'];
  $t=$_GET['temp'];
  $h=$_GET['humidity'];
  addData($c, $t, $h);
  if($DEBUG)
     echo "<p>all done";
  //listData();
  echo "<p>OK updated data for channel:" . $c . "</p>";
}
else
{
  if($DEBUG)
    echo "listData()";
  //listData();
  //echo "<p>missing arg";
}
echo "<p><a href='linechart_hour.php'>Stunden-&Uuml;bersicht</a></p>";
echo "<p><a href='barchart_days.php'>Tages-&Uuml;bersicht</a></p>";
echo "<p><a href='http://www.unwetterzentrale.de/uwz/getwarning_de.php?plz=41363&uwz=UWZ-DE&lang=de'>Unwetterwarnungen J&uuml;chen</a></p>";

echo showAllCharts();
//#################################################################################
// see http://code.google.com/p/googlechartphplib/wiki/GettingStarted
//#################################################################################
// don't forget to update the path here
require './lib/GoogleChart.php';

$chart = new GoogleChart('lc', 500, 200);

// manually forcing the scale to [0,100]
$chart->setScale(0,100);

// add one line
$data = new GoogleChartData(array(49,74,78,71,40,39,35,20,50,61,45));
$chart->addData($data);

// customize y axis
$y_axis = new GoogleChartAxis('y');
$y_axis->setDrawTickMarks(false)->setLabels(array(0,50,100));
$chart->addAxis($y_axis);

// customize x axis
$x_axis = new GoogleChartAxis('x');
$x_axis->setTickMarks(5);
$chart->addAxis($x_axis);

echo $chart->toHtml();
//#################################################################################
// END
//#################################################################################

echo "<p>v0.9" . "</body>" . "</html>";
?>

the dbhelp.php file creates the DB automatically if not already exists:

<?php
//global dbhelp.php file

$server="localhost";
$user="root";
$passwd="password";
$names=array(
1 => "Aussen",
2 => "Schlaf",
3 => "Andreas");
$DEBUG=false;

function openDB(){
    global $DEBUG;
    global $user;
    global $passwd;
    $link = mysql_connect("localhost", $user, $passwd);
    if(!$link){
            echo "SqlConnect failed";
            echo mysql_error();
    }
    else
            if($DEBUG)
                    echo "SqlConnect OK";

    $query="CREATE database IF NOT EXISTS avrdb;";
            $result = mysql_query($query, $link);
    if(!$result){
            echo "CreateDB failed";
            echo mysql_error();
    }
    else
            if($DEBUG)
                    echo "CreateDB OK";

    $result = mysql_select_db("avrdb", $link);
    if(!$result){
            echo "SelectDB failed";
            echo mysql_error();
    }
    else
            if($DEBUG)
                    echo "SelectDB OK";

    $query="CREATE TABLE IF NOT EXISTS `avrtemp` (" .
    "`id` int(11) NOT NULL AUTO_INCREMENT, " .
    "`channel` int(11), " .
    "`temp` int(11), " .
    "`humidity` int(11), " .
    "`date_time` TIMESTAMP, " .
    "PRIMARY KEY (`id`) );";
    $result = mysql_query($query, $link);
    if(!$result){
            echo "CreateTable failed";
            echo mysql_error();
    }
    else
            if($DEBUG)
                    echo "CreateTable OK";

    return true;        
}
...
function addData($c,$t,$h){
        global $DEBUG;
        $lastStoredDateTime=getLastStoredDateTime();
        if($DEBUG){
                echo "<p>" . $c . "</p>\n";
                echo "<p>LastDateTime=".$lastStoredDateTime."</p>\n";
        }
        // add data but do not save seconds (use 00)
        $query="INSERT INTO avrtemp (`channel`,`temp`,`humidity`,`date_time`)".
              " VALUES ( $c,$t,$h,".
//                         " DATE_FORMAT(NOW()),".
                         " DATE_FORMAT('".$lastStoredDateTime."', ".
              " '%Y-%c-%d %H:%i') )";
        if($DEBUG)
                echo "<p>" . $query;
        $result = mysql_query($query);
        if(!$result){
                echo "addData failed";
                echo mysql_error();
        }
        else
                if($DEBUG)
                        echo "addData OK";
}

The Arduino posts (using GET) updated data every 5 minutes. The data arrives at the Arduino via 433MHz wireless sensors every minute or so.

Using the Ethernet Lib the data update is done via

char cBuf[maxBuf+1];
const char getHttpString[] =
  "GET /homewatch/index.php?channel=%i&temp=%i&humidity=%i&time=%i\0";
...
  // and send data: /index.php?channel=1&temp=165&humidity=80&datetime=010120131234
  if (client.connect(server, 80)) {
    Serial.println("connected. Send GET...");
    snprintf(cBuf, maxBuf,
      getHttpString,
      sensorData[idxChannel].channel,
      sensorData[idxChannel].temp,
      sensorData[idxChannel].humidity,
      sensorData[idxChannel].time_long
      );

Serial.println(F("####"));
Serial.println(cBuf);
Serial.println(F("####"));

    client.println(cBuf);
...

https://github.com/hjgode/homewatch/blob/master/arduino/SketchBook/WebClientTemperature/WebClientTemperature.ino:

The string "GET /homewatch/index.php?channel=%i&temp=%i&humidity=%i&time=%i\0" is simply filled with actual sensor data and the server saves that to its database.

The Ethercard lib comes with similar examples: see the examples noipClient, pachube and webclient. Unfortunately I was not yet able to adopt these for my AVR-NETIO.

The code to post (GET) some data to the server I am currently working is

void sendData(int idxChannel){
    Serial.print(F("in sendData for idx=")); Serial.println(idxChannel);
    byte sd;
    int channel=sensorData[idxChannel].channel;
    int temp=sensorData[idxChannel].temp;
    int humidity=sensorData[idxChannel].humidity;
    char* stime="201301011122";
    stime=printDateTime((char*)&stime, 13, sensorData[idxChannel].time_long);

    if(sensorData[idxChannel].bUpdated==0){
      //nothing new
      if (MYDEBUG==0){
        Serial.println(F("leaving for not updated channel data"));
        goto exit_sendData;
      }
      else
      {
        sensorData[idxChannel].channel=idxChannel;
        sensorData[idxChannel].temp=222;
        sensorData[idxChannel].humidity=55;
        sensorData[idxChannel].time_long=now();      
      }
    }

    // generate two fake values as payload - by using a separate stash,
    // we can determine the size of the generated message ahead of time
    sd = stash.create();
    stash.print("0,");
    stash.println((word) millis() / 123);
    stash.print("1,");
    stash.println((word) micros() / 456);
    stash.save();

    // generate the header with payload - note that the stash size is used,
    // and that a "stash descriptor" is passed in as argument using "$H"
    Stash::prepare(PSTR("GET http://$F/homewatch/index.php?channel=$D&temp=$D&humidity=$D&time=$S" "\r\n"
                        "Host: $F" "\r\n"
                        "Content-Length: $D" "\r\n"
                        "\r\n"
                        "$H"),
            website, 
            channel,
            temp,
            humidity,
            stime,
            website, stash.size(), sd);

    // send the packet - this also releases all stash buffers once done
    ether.tcpSend();
exit_sendData:
  lastConnectionTime = now();//millis();
  Serial.println(F("...end of sendData()"));
}

As you see, there is a formatted string

PSTR("GET http://$F/homewatch/index.php?channel=$D&temp=$D&humidity=$D&time=$S"

which is filled with variables. $F fills from Flash Memory, $D a number var, $S from a RAM memory string. Using this without my SensorCode (which uses Interrupts) is working.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top