极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 19702|回复: 9

W5100能不能连接mysql 并发送传感器数据

[复制链接]
发表于 2012-8-30 09:37:03 | 显示全部楼层 |阅读模式
W5100能不能连接mysql 并发送传感器数据
回复

使用道具 举报

发表于 2012-8-30 20:47:49 | 显示全部楼层
本帖最后由 弘毅 于 2017-3-23 08:00 编辑

用php+apache+mysql在自己的电脑上搭建一个网站,或者如果你有一个网站的话也可以。可以让W5100发送查询字符串到一个网站,网站再根据收到的字符串做一个数据库写入或者查询。下面是一段php的查询和写入代码。

  1. $statue = $_GET['statue'];

  2.         if($statue==NULL || $statue!=1){
  3.                 $statue=0;
  4.         }
  5.         switch ($statue)
  6.         {
  7.                 case 0: getHistory();break;
  8.                 case 1: getStatue();break;
  9.                 default:getHistory();break;
  10.        
  11.         }

  12. function getStatue(){
  13.         require('inc\conn.inc.php');
  14.         $sensor = $_GET['sensor'];
  15.         $sdata = $_GET['sdata'];
  16.         $geeker = $_GET['geeker'];
  17.         $showtime = date("Y-m-d H:i:s");
  18.        
  19.         if (mysqli_connect_errno()){
  20.                 echo 'can not connect to database!';
  21.         } else {
  22.                 if($sensor==NULL || $sdata == NULL || $geeker==NULL){
  23.                 echo 'Error. Please check your data!';
  24.                
  25.         }else{
  26.                 $query="insert into arduino values(".'NULL'. ", '".$geeker. "', '".$sensor. "', '".$sdata. "', '".$showtime."')";
  27.                 $result = $db->query($query);               
  28.                 echo 'Submitted OK!';
  29.         }
  30. }
  31. }

  32. function getHistory(){

  33.         require('inc\conn.inc.php');
  34.         echo '<html xmlns="http://www.w3.org/1999/xhtml">
  35.                   <head><title>Arduino上传数据至网络测试页面</title></head>
  36.                   <div align="center"><h1>Arduino上传数据至网络测试历史记录</h1></div>';
  37.                   
  38.         if (mysqli_connect_errno()){
  39.                 echo 'can not connect to database!';
  40.         } else {
  41.                 $query='select * from arduino order by id desc limit 100';
  42.                 $result=$db->query($query);
  43.                 $num=$result->num_rows;
  44.                
  45.                 if($num!=0)
  46.                 {
  47.                         echo '<table width="640" border="1" align="center">
  48.                                   <tr>
  49.                                             <td><strong>编号</strong></td>
  50.                                     <td><strong>作者</strong></td>
  51.                                     <td><strong>传感器</strong></td>
  52.                                     <td><strong>传感器数据</strong></td>
  53.                                     <td><strong>上传时间</strong></td>
  54.                                 </tr>';
  55.                        
  56.                 while($allrow=$result->fetch_array()){
  57.                         echo '<tr>';
  58.                         echo '<td>'.$allrow['id'].'</td>';
  59.                         echo '<td>'.$allrow['geek'].'</td>';
  60.                         echo '<td>'.$allrow['sensor'].'</td>';
  61.                         echo '<td>'.$allrow['sensor_data'].'</td>';
  62.                         echo '<td>'.$allrow['time'].'</td>';
  63.                         echo '</tr>';
  64.                 }

  65.                          echo '</table>';
  66.                 }else{
  67.                 echo '没有历史记录可查';
  68.                 }               
  69.         }          
  70.         echo '<body></body></html>';
  71.         $db->close();
  72. }
复制代码


W5100的代码如下,你需要自己写获取传感器数据的部分,不过如果只是做测试的,直接使用下面的代码也行。红色的部分需要改成你自己的,黄色的部分可以改也可以不改。具体的做法参见 http://www.soxitoday.com/archives/1926.html
  1.     #include <SPI.h>
  2.     #include <Ethernet.h>

  3.    [color=Red] byte mac[] = {  0x8C, 0xB8, 0×64, 0xEF, 0×71, 0×97 };
  4.     IPAddress ip(218,23,63,34);[/color]
  5.     EthernetClient client;

  6.     char serverName[] = "www.lycabc.com";

  7.     void setup() {
  8.        Serial.begin(9600);
  9.        while (!Serial) {
  10.         ; // wait for serial port to connect. Needed for Leonardo only
  11.       }

  12.       Serial.println("Attempting to get an IP address using DHCP:");
  13.       if (!Ethernet.begin(mac)) {
  14.         Serial.println("failed to get an IP address using DHCP, trying manually");
  15.         Ethernet.begin(mac, ip);
  16.       }
  17.       Serial.print("My address:");
  18.       Serial.println(Ethernet.localIP());
  19.       connectToServer();
  20.     }

  21.     void loop()
  22.     {
  23.       if (client.connected()) {
  24.         if (client.available()) {
  25.           char inChar = client.read();
  26.           Serial.print(inChar);
  27.         }
  28.     }
  29.     }

  30.     void connectToServer() {
  31.       Serial.println("connecting to server…");
  32.       if (client.connect(serverName, 80)) {
  33.         Serial.println("making HTTP request…");
  34.        String Requesting;
  35.        [backcolor=Yellow]String sensor="mysensor";//传感器的名称
  36.         int sdata=100;//传感器的数值
  37.         String geeker="yourname";//你的昵称[/backcolor]
  38.         Requesting= "GET /?sensor="+sensor+"&sdata="+sdata+"&geeker="+geeker+"&statue=1 HTTP/1.1";   
  39.         client.println(Requesting);
  40.         client.println("HOST: [url]www.lycabc.com[/url]");
  41.         client.println();
  42.       }
  43.     }  
  44.        
复制代码
回复 支持 1 反对 1

使用道具 举报

发表于 2012-8-30 10:32:24 | 显示全部楼层
mysql端做个接口这个简单。
回复 支持 反对

使用道具 举报

发表于 2012-8-30 10:35:25 | 显示全部楼层
这个应该是服务器端脚本解决吧。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-8-30 13:10:14 | 显示全部楼层
完全无从下手
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-8-31 10:31:23 | 显示全部楼层
实在是太感谢啦 很有帮助
回复 支持 反对

使用道具 举报

发表于 2016-5-12 14:57:00 | 显示全部楼层
soxitoday 发表于 2012-8-30 20:47
用php+apache+mysql在自己的电脑上搭建一个网站,或者如果你有一个网站的话也可以。可以让W5100发送查询字符 ...

你好,请问inc\conn.inc.php这个函数在哪可以找到?
回复 支持 反对

使用道具 举报

发表于 2017-3-22 22:54:26 | 显示全部楼层
试了一下,代码编译不过去,期待更多细节
回复 支持 反对

使用道具 举报

发表于 2017-3-27 09:40:04 | 显示全部楼层
终于搞定,返回了那么多的400,终于见到了200
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-3-29 17:02 , Processed in 0.042970 second(s), 23 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表