|
|
本帖最后由 woshiysc 于 2013-8-26 17:28 编辑
新手才接触arduino,遇到好多小白问题请教。
使用ENC28J60和arduino UNO连接后,使用ethernetCard的库。
硬件上使用DHT11感受温度和湿度,通过IRremote发送红外线遥控数据,网页端发送需要遥控的数据:比如button=1,则通过遥控器发送1编码。
下面这个代码是ethernetcard的设置。。
我的web服务器地址是192.168.2.42(在我的局域网内外网中)
- static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
- byte Ethernet::buffer[700];
- Stash stash;
- char website[] PROGMEM = "192.168.2.42";
复制代码
下面这个是定义红外编码的,定义了 123字节长度的红外编码1,2,3,4.
- #include <IRremote.h>
- IRsend irsend; //defult PIN is 3;
- /*======设置不同种类的红外遥控编码及编码长度======*/
- unsigned int button_1[123]={
- };
- unsigned int button_2[123]={
- };
- unsigned int button_3[123]={
- };
- unsigned int button_4[123]={
- };
复制代码
然后定义了DHT11。。
- /*========DHT11传感器库调用=======*/
- #include "DHT.h"
- #define DHTPIN 8
- #define DHTTYPE DHT11
- DHT dht(DHTPIN, DHTTYPE);
复制代码
这个使用enthercard库定义setup阶段遇到好多问题不懂的。
就是该如何知道我的enc28J60已经连接好了,和我的website已经连接的语法问题了~
- void setup() {
- dht.begin(); //初始化DHT11传感器
- pinMode(8,INPUT_PULLUP); //使用IO口内上拉 将DHT11输出口上拉
- ether.begin(mymac, ip); //init Ethernet
- server.begin(); //Ethernet
-
- Serial.begin(9600); //自检程序
- Serial.println("UNIControl demo");
- Serial.println();
-
- if (!ether.begin(sizeof Ethernet::buffer, mymac)) {
- Serial.println( "Failed to access Ethernet controller");
- while(1);
- }
- else Serial.println("Ethernet controller initialized");
-
- if (!ether.staticSetup(myip, gwip,mydns)) {
- Serial.println("Failed to get configuration from DHCP");
- while(1);
- } else Serial.println("DHCP configuration done");
-
- if (!ether.dnsLookup(website)) {
- Serial.print("Unable to resolve Website IP");
- while(1);
- } else Serial.println("Website IP resolved");
-
- Serial.println();
- ether.printIp("IP Address:\t", ether.myip);
- ether.printIp("Netmask:\t", ether.mymask);
- ether.printIp("Gateway:\t", ether.gwip);
- ether.printIp("Website IP:\t", ether.hisip);
- Serial.println();
-
复制代码
setup完成就到了loop()中,我自定义了获取温度和湿度的函数 tempAndhum(temp,hum); 红外控制函数 ircontrol(); 还有发送温度湿度数据的函数 send_data2web(temp,hum,alarm,led);
- void loop() {
- tempAndhum(temp,hum);
- delay(200);
- ircontrol();
- delay(200);
- send_data2web(temp,hum);
- }
复制代码
下面是感受温湿度的自定义函数,并把数据保存为char字符 格式为temp={"value"=温度数据} hum={"value"=湿度数据}
就是不知道这样写tempAndhum(OUT char *temp ,OUT char *hum) {} 对不对,还有在编译过程中老是说我这些函数“was not declared in this scope”
- tempAndhum(OUT char *temp ,OUT char *hum)
- {
-
- int chk = DHT11.read(DHTPIN);
- // sensors.requestTemperatures();
- int Tc_100 =(float) DHT11.temperature*10;
-
- int whole_temp, fract_temp;
- whole_temp = Tc_100/10 ; // separate off the whole and fractional portions
- fract_temp = Tc_100 % 10; //通过%计算取余数得出数据小数位置
- //whole=35;
- //fract=3;
- sprintf(temp,"{"value":%d.%d}",whole_temp,fract_temp);
- int Tc_200 =(float) DHT11.humidity*10;
- whole_temp = Tc_200/10 ; // separate off the whole and fractional portions
- fract_temp = Tc_200 % 10;
- //whole=35;
- //fract=3;
- sprintf(hum,"{"value":%d.%d}",whole_hum,fract_hum);
复制代码
在红外遥控function 我想通过服务器给我的HTTP GET数据来控制我的红外线发送红外代码
- void ircontrol() {
-
- word len = ether.packetReceive();
- word pos = ether.packetLoop(len);
- int a,i;
- for (i=1;i<4;i++)
- {
- if (strstr((char *)Ethernet::buffer + pos, "GET /?button=%d",i)!=0);
- a = i;
- }
-
- switch(a)
- {
-
- case 1:
- Serial.println("press button1");
- irsend.sendRaw(butten_1,123,38);
- delay(3000);
- break;
- case 2:
- Serial.println("press button2");
- irsend.sendRaw(butten_2,123,38);
- delay(3000);
- break;
- case 3:
- Serial.println("press button3");
- irsend.sendRaw(butten_3,123,38);
- delay(3000);
- break;
- }
复制代码
最后就是把温湿度数据发送给服务器的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))
- send_data2web(IN char *temp,IN char *hum)
- {
- char data2send1 = temp;
- char data2send2 = hum;
- String PostData="sample={"fittingId":1,";
- unsigned char i;
- for(i=1;i<3;i++)
-
- {
- PostData=PostData+""channel-";
- PostData=String(PostData+i);
- PostData=PostData+"":";
- PostData=String(PostData + String(data2send+i);
- if(i!=5)
- PostData=PostData+",";
- }
- PostData=PostData+"}";
- byte sd = stash.create();
- stash.println(PostData);
- 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"
-
- // send the packet - this also releases all stash buffers once done
- ether.tcpSend();
- if (client.connect(website, 80)) //这个如何判断是不是连上我的服务器
- {
- Serial.println("\nconnected...");
- Serial.println("ARDUINO: forming HTTP request message");
- client.println("POST /UNIControl/PhpPost.php HTTP/1.1"); //setting PHP
- client.println("From: Arduino_room1 ");
-
- client.println("POST /tinyFittings/index.php HTTP/1.1");
- client.println("Host: 192.168.2.42");
- client.println("User-Agent: Arduino/1.0");
- client.println("Connection: close");
- client.println("User-Agent: HTTPTool/1.0");
- client.println("Content-Type: application/x-www-form-urlencoded");
- client.print ("Content-Length:");
- client.println(DatatoSend.length());
- client.println("Connection: close");
- client.println(DatatoSend);
- client.println();
- Serial.println("ARDUINO: HTTP message sent");
- delay(3000);
- if(client.available())
- {
- Serial.println("ARDUINO: HTTP message received");
- Serial.println("ARDUINO: printing received headers and script response...\n");
-
- while(client.available())
- {
- char c = client.read();
- Serial.print(c);
- }
- }
- else
- {
- Serial.println("ARDUINO: no response received / no response received in time");
- }
-
- client.stop();
- }
- else
- {
- Serial.println("connection failure");
- }
- delay(2000);
- }
复制代码
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:}
|
|