极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 13786|回复: 3

使用ENC28J60 通过HTTP 传输传感器数据给服务器遇到问题~

[复制链接]
发表于 2013-8-26 17:28:59 | 显示全部楼层 |阅读模式
本帖最后由 woshiysc 于 2013-8-26 17:28 编辑

新手才接触arduino,遇到好多小白问题请教。

使用ENC28J60和arduino UNO连接后,使用ethernetCard的库。

硬件上使用DHT11感受温度和湿度,通过IRremote发送红外线遥控数据,网页端发送需要遥控的数据:比如button=1,则通过遥控器发送1编码。
下面这个代码是ethernetcard的设置。。

我的web服务器地址是192.168.2.42(在我的局域网内外网中)

  1. static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };

  2. byte Ethernet::buffer[700];
  3. Stash stash;
  4. char website[] PROGMEM = "192.168.2.42";   
复制代码



下面这个是定义红外编码的,定义了 123字节长度的红外编码1,2,3,4.

  1. #include <IRremote.h>
  2. IRsend irsend;   //defult PIN is 3;


  3. /*======设置不同种类的红外遥控编码及编码长度======*/
  4. unsigned int button_1[123]={

  5. };

  6. unsigned int button_2[123]={
  7. };

  8. unsigned int button_3[123]={
  9. };

  10. unsigned int button_4[123]={
  11. };
复制代码


然后定义了DHT11。。
  1. /*========DHT11传感器库调用=======*/
  2. #include "DHT.h"
  3. #define DHTPIN 8
  4. #define DHTTYPE DHT11   
  5. DHT dht(DHTPIN, DHTTYPE);
复制代码




这个使用enthercard库定义setup阶段遇到好多问题不懂的。
就是该如何知道我的enc28J60已经连接好了,和我的website已经连接的语法问题了~



  1. void setup() {
  2.   dht.begin();                //初始化DHT11传感器
  3.   pinMode(8,INPUT_PULLUP);    //使用IO口内上拉 将DHT11输出口上拉
  4.   ether.begin(mymac, ip);    //init Ethernet
  5.   server.begin();             //Ethernet
  6.   
  7.   Serial.begin(9600);         //自检程序
  8.   Serial.println("UNIControl demo");
  9.   Serial.println();

  10.   if (!ether.begin(sizeof Ethernet::buffer, mymac)) {
  11.     Serial.println( "Failed to access Ethernet controller");
  12.     while(1);
  13.   }
  14.    else Serial.println("Ethernet controller initialized");

  15.   if (!ether.staticSetup(myip, gwip,mydns)) {
  16.     Serial.println("Failed to get configuration from DHCP");
  17.     while(1);
  18.   } else Serial.println("DHCP configuration done");

  19.   if (!ether.dnsLookup(website)) {
  20.     Serial.print("Unable to resolve Website IP");
  21.     while(1);
  22.   } else Serial.println("Website IP resolved");

  23.   Serial.println();
  24.   ether.printIp("IP Address:\t", ether.myip);
  25.   ether.printIp("Netmask:\t", ether.mymask);
  26.   ether.printIp("Gateway:\t", ether.gwip);
  27.   ether.printIp("Website IP:\t", ether.hisip);
  28.   Serial.println();
  29.   

复制代码


setup完成就到了loop()中,我自定义了获取温度和湿度的函数   tempAndhum(temp,hum);  红外控制函数 ircontrol(); 还有发送温度湿度数据的函数 send_data2web(temp,hum,alarm,led);

  1. void loop() {

  2.   tempAndhum(temp,hum);
  3.     delay(200);
  4.   ircontrol();
  5.     delay(200);
  6.   send_data2web(temp,hum);
  7.   }
复制代码



下面是感受温湿度的自定义函数,并把数据保存为char字符 格式为temp={"value"=温度数据} hum={"value"=湿度数据}
就是不知道这样写tempAndhum(OUT char *temp ,OUT char *hum) {} 对不对,还有在编译过程中老是说我这些函数“was not declared in this scope”

  1. tempAndhum(OUT char *temp ,OUT char *hum)
  2. {

  3.   int chk = DHT11.read(DHTPIN);
  4. // sensors.requestTemperatures();  
  5.   int Tc_100 =(float) DHT11.temperature*10;

  6.   int whole_temp, fract_temp;
  7.   whole_temp = Tc_100/10 ;  // separate off the whole and fractional portions
  8.   fract_temp = Tc_100 % 10; //通过%计算取余数得出数据小数位置
  9.   //whole=35;
  10.   //fract=3;
  11.   sprintf(temp,"{"value":%d.%d}",whole_temp,fract_temp);



  12.   int Tc_200 =(float) DHT11.humidity*10;
  13.   whole_temp = Tc_200/10 ;  // separate off the whole and fractional portions
  14.   fract_temp = Tc_200 % 10;
  15.   //whole=35;
  16.   //fract=3;
  17.   sprintf(hum,"{"value":%d.%d}",whole_hum,fract_hum);
复制代码


在红外遥控function 我想通过服务器给我的HTTP GET数据来控制我的红外线发送红外代码

  1. void ircontrol() {
  2.   
  3.   word len = ether.packetReceive();
  4.   word pos = ether.packetLoop(len);
  5.   int a,i;
  6.   for (i=1;i<4;i++)

  7.   {
  8.     if (strstr((char *)Ethernet::buffer + pos, "GET /?button=%d",i)!=0);
  9.     a = i;
  10.   }
  11.   
  12.   switch(a)
  13.   {
  14.    
  15.     case 1:
  16.       Serial.println("press button1");
  17.       irsend.sendRaw(butten_1,123,38);
  18.       delay(3000);
  19.       break;
  20.     case 2:
  21.       Serial.println("press button2");
  22.       irsend.sendRaw(butten_2,123,38);
  23.       delay(3000);
  24.       break;
  25.     case 3:
  26.       Serial.println("press button3");
  27.       irsend.sendRaw(butten_3,123,38);
  28.       delay(3000);
  29.       break;

  30. }
复制代码



最后就是把温湿度数据发送给服务器的function了,这里遇到的问题最大,看的好多使用enc28j60发送HTTP POST的方式都不一样,有的用sltash保存数据再发送,有的直接使用原始数据的char发送,还有发送的头文件("OST /api/V1/Gateway/UpdateSensors/01 HTTP/1.1\r\n"
                      "Host: open.lewei50.com\r\n"
                      "Content-Length: $D\r\n"
                      "userkey: $F\r\n"
                      "\r\n"
                      "$H")
这里不是很懂该如何自定义这个HTTP 头,因为有时候看到的头文件只有 POST "解析PHP文件地址","服务器地址" 有的又不一样,还有判断是不是连上我的服务器80端口的语句我不知道在ethercard库该如何定义 我看的现成W5100的例子是
  if (client.connect(website, 80))  



  1. send_data2web(IN char *temp,IN char *hum)

  2. {

  3.   char data2send1 = temp;
  4.   char data2send2 = hum;



  5.   String PostData="sample={"fittingId":1,";
  6.   unsigned char i;
  7.   for(i=1;i<3;i++)
  8.    
  9.   {
  10.     PostData=PostData+""channel-";
  11.     PostData=String(PostData+i);
  12.     PostData=PostData+"":";
  13.     PostData=String(PostData + String(data2send+i);
  14.     if(i!=5)
  15.       PostData=PostData+",";
  16.   }
  17.     PostData=PostData+"}";

  18.   byte sd = stash.create();
  19.   stash.println(PostData);
  20.   stash.save();

  21.   // generate the header with payload - note that the stash size is used,
  22.   // and that a "stash descriptor" is passed in as argument using "$H"
  23.   

  24.   // send the packet - this also releases all stash buffers once done
  25.   ether.tcpSend();

  26.   if (client.connect(website, 80))   //这个如何判断是不是连上我的服务器
  27.   {
  28.        Serial.println("\nconnected...");
  29.        Serial.println("ARDUINO: forming HTTP request message");
  30.        client.println("POST /UNIControl/PhpPost.php HTTP/1.1"); //setting PHP
  31.        client.println("From: Arduino_room1 ");
  32.       
  33.        client.println("POST /tinyFittings/index.php HTTP/1.1");
  34.        client.println("Host:  192.168.2.42");
  35.        client.println("User-Agent: Arduino/1.0");
  36.        client.println("Connection: close");

  37.         client.println("User-Agent: HTTPTool/1.0");
  38.         client.println("Content-Type: application/x-www-form-urlencoded");
  39.         client.print  ("Content-Length:");
  40.         client.println(DatatoSend.length());
  41.         client.println("Connection: close");
  42.         client.println(DatatoSend);
  43.         client.println();

  44.       Serial.println("ARDUINO: HTTP message sent");
  45.       delay(3000);
  46.     if(client.available())
  47.     {
  48.         Serial.println("ARDUINO: HTTP message received");
  49.         Serial.println("ARDUINO: printing received headers and script response...\n");
  50.         
  51.         while(client.available())
  52.         {
  53.           char c = client.read();
  54.           Serial.print(c);
  55.         }
  56.     }
  57.     else
  58.     {
  59.         Serial.println("ARDUINO: no response received / no response received in time");
  60.       }
  61.       
  62.       client.stop();
  63.     }
  64.     else
  65.     {
  66.       Serial.println("connection failure");
  67.     }
  68.     delay(2000);

  69. }
复制代码


HTTP协议中的一个是User-Agent: HTTPTool/1.0 还有个是User-Agent: Arduino/1.0" 不知道该用哪一个~~ 不太懂这个
还有client.available();这个在W5100官方库的实现在ethercard中不知道该如何实现。。。

好多问题:
我总结下主要就是
1、我的自定义函数不能使用,我放在loop();之后 是不是该放到loop();前面。
2、自定义函数输出格式问题,我用的是比如tempAndhum(OUT char *temp ,OUT char *hum);输出temp和hum两个char量。
3、在HTTP post到服务器数据这个阶段不懂该如何定义传输数据的格式。
4、HTTP 连接到服务器状态的函数不知道该如何定义client.available();这个貌似用不了
5、HTTP get接收服务器数据我用的是word len = ether.packetReceive();
  word pos = ether.packetLoop(len); if (strstr((char *)Ethernet::buffer + pos, "GET /?button=%d",i)!=0); 不知道这样对不对。
因为我的编译不成功不能判断。

请教各位大侠啦,,完成后我会把该项目细节都上传到坛子,还有PHP文件,mysql数据库等等~~{:soso_e183:}
回复

使用道具 举报

发表于 2013-8-27 09:09:13 | 显示全部楼层
用w5100吧,少走很多弯路。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-8-27 14:24:26 | 显示全部楼层
瘦网虫 发表于 2013-8-27 09:09
用w5100吧,少走很多弯路。

谢谢哦,但是我遇到的很多问题不是换模块能解决的 比如自定义的函数为啥都显示“was not declared in this scope”这个问题。。自定义函数的写法问题tempAndhum(OUT char *temp ,OUT char *hum){} 不知道这样是不是合规
回复 支持 反对

使用道具 举报

发表于 2013-12-21 22:38:51 | 显示全部楼层
我也在看ENC28J60这部分,现在暂时只能让dth11一个参数上网,需要改进
回复 支持 反对

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-14 08:54 , Processed in 0.072737 second(s), 20 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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