Arduinowilliam hill官网
直播中

zhlgcc

12年用户 3经验值
私信 关注
[问答]

arduino作为Web客户端如何使用http请求访问本地服务器

想法是这样的,使用arduino测温湿度,我在本地架了一个服务器,现在想将arduino测量的温湿度传递给服务器,服务器对数据进行保存。
我用protues做了模拟,用arduino做为web服务器可以访问,在网页上控制灯的开关都没有问题。
但是,用arduino作为web客户端就不会了。

1.服务端:服务端保存数据的URL为http://localhost:8080/yundu/arduino/wd/saveFromArduino?wd=10(←只有一个参数,就是温度,变量名为wd)
我直接在浏览器中输入这个URL,可以保存到数据库。
2.现在就卡在arduono怎么使用HTTP GET请求了。也想了很多办法,找了一些库文件。
根据Ethernet自带的WebClient例子,我做了如下修改,尚未成功

(我本地服务器的ip为192.168.1.102)我有个疑问,有劳各位解惑,是不是访问本地服务器需要配置网关呢?


  1. #include
  2. #include

  3. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

  4. IPAddress server(192,168,1,102);  // 我本地服务器的IP

  5. IPAddress ip(192,168,1,177);

  6. EthernetClient client;

  7. void setup() {
  8.   Serial.begin(9600);
  9.    while (!Serial) {
  10.   }

  11.   // start the Ethernet connection:
  12.   if (Ethernet.begin(mac) == 0) {
  13.     Serial.println("Failed to configure Ethernet using DHCP");

  14.     Ethernet.begin(mac, ip);
  15.   }
  16.   // give the Ethernet shield a second to initialize:
  17.   delay(1000);
  18.   Serial.println("connecting...");

  19.   // if you get a connection, report back via serial:
  20.   if (client.connect(server, 8080)) {
  21.     Serial.println("connected");
  22.     // Make a HTTP request:
  23.     client.println("GET /yundu/arduino/wd/saveFromArduino?wd=10 HTTP/1.1");//保存数据的URL
  24.     client.println("Host:192.168.1.102");
  25.     client.println("Connection: close");
  26.     client.println();
  27.   }
  28.   else {
  29.     // kf you didn't get a connection to the server:
  30.     Serial.println("connection failed");
  31.   }
  32. }

  33. void loop()
  34. {
  35.   // if there are incoming bytes available
  36.   // from the server, read them and print them:
  37.   if (client.available()) {
  38.     char c = client.read();
  39.     Serial.print(c);
  40.   }

  41.   // if the server's disconnected, stop the client:
  42.   if (!client.connected()) {
  43.     Serial.println();
  44.     Serial.println("disconnecting.");
  45.     client.stop();

  46.     // do nothing forevermore:
  47.     while(true);
  48.   }
  49. }



已退回3积分

回帖(4)

dylandavid

2015-12-20 18:22:21
我就看看,学习学习,抱着学习的态度。
举报

王生

2016-1-9 17:08:11
看起来还不错哈
举报

838663302

2017-1-26 17:12:39
楼主搞定了吗,我也遇到了同样的问题,但是http请求可以访问远程服务器
举报

838663302

2017-1-26 17:13:07
楼主解决了吗
举报

更多回帖

发帖
×
20
完善资料,
赚取积分