esp8266 arduino 之 与服务器通信

2017-04-262.9万
AI 快速预览详细 收起
本文介绍了如何使用ESP8266和Arduino通过HTTP GET请求与远程服务器进行通信。首先,需要在代码中加入ESP8266HTTPClient库支持。然后,通过WiFi连接到路由器,并使用HTTP GET请求从服务器获取数据。如果请求成功,将打印出服务器返回的JSON数据。这种方法适用于物联网项目中的实时数据交互。
前一篇文章已经把wifi连接上了 下面我们连接下远程服务器哈哈

和服务器通信方式一般是tcp udp 和http的post get方式 还有现在流行的实时websocket哈哈
tcp 和udp需要自己写server代码 比较烦 我的阿里云是python的环境 就直接用http的方式吧

加入ESP8266HTTPClient.h的库支持

下面是ESP8266代码:

  1. #include <ESP8266HTTPClient.h>
  2. #include <ESP8266WiFi.h>
  3. HTTPClient http;
  4. char ssid[] = "Question";        //  你家的路由器wifi名称
  5. char paswd[] = "*******";       // 你家的路由器wifi密码
  6. void setup() {
  7.   // put your setup code here, to run once:
  8.    Serial.begin(115200);
  9.    Serial.println();
  10.    Serial.print("Connecting to ");
  11.    Serial.println(ssid);
  12.    WiFi.begin(ssid, paswd);                //开始连接wifi
  13.    while (WiFi.status() != WL_CONNECTED)   //等待wifi连接成功
  14.    {
  15.       delay(500);
  16.       Serial.print(".");
  17.    }
  18.    Serial.println("");
  19.    Serial.println("WiFi connected");
  20.    Serial.println("IP address: ");
  21.    Serial.println(WiFi.localIP());     //打印连接上wifi后获取的ip地址
  22. }
  23. void loop() {
  24.   // put your main code here, to run repeatedly:
  25.   char url[100]="http://**********:81/test?msg="hello"";   //把*号改成你的服务器地址
  26.   http.begin(url);
  27.   int httpCode = http.GET();
  28.   if(httpCode >0)
  29.   {
  30.      Serial.printf("[HTTP] GET...code:%d\n",httpCode);
  31.      String serdata = http.getString();                                        //获取服务器返回的数据
  32.      Serial.println(serdata);                                                           
  33.   }
  34.   else
  35.   {
  36.     Serial.printf("[HTTP] GET...failed,error:%s\n",http.errorToString(httpCode).c_str());  
  37.   }
  38.   http.end();
  39.   delay(10000);
  40. }
复制代码


运行效果
esp8266 arduino 之 与服务器通信图1

返回200 表示成功请求成功,后面是服务器返回的json数据 ESP8266通过返回的数据就能判断是否有命令过来了


esp8266 arduino 之 与服务器通信图2


服务器的代码:

环境 ubuntu django python

文件 url.py
  1. <font face="" "="">     url(r'^test', collect_data.views.test),[/mw_shl_code]</font>
  2. <font face="" "="">文件 collect_data/views.py</font>
  3. [mw_shl_code=python,true]#coding=utf-8
  4. from django.shortcuts import render
  5. # Create your views here.
  6. from collect_data.models import Air_data
  7. from django.shortcuts import render,render_to_response
  8. from django.http import HttpResponseRedirect,HttpResponse
  9. import datetime
  10. import json,os,time,random,string,types
  11. def test(request):
  12.     print request.get_full_path()
  13.     return HttpResponse(json.dumps({'status':'ok'},sort_keys=True), content_type="application/json")
复制代码

创作许可协议

本项目采用 None(不开放任何权利,保留所有权利) 进行许可。

评论(7)
maomaopcy
maomaopcy
请问怎么下载要用到的库
ishmaelQ
ishmaelQ
有没有用php通信的例子,
question
question
作者
回复ishmaelQ
php的服务器我不会写 arduino那边是标准的get请求 你用PHP也能用的
DeepMind
DeepMind
我想知道,能不能通过家里的无线路由器,连接到服务器上去?
question
question
作者
回复DeepMind
这个代码就是通过wifi和阿里云进行通信的例子
dsweiliang
dsweiliang
mark一下,学习学习了
pATAq
pATAq
mark一下
hnyzcj
hnyzcj
以后慢慢琢磨
hnyzcj
hnyzcj
好教程值得一看
- 没有更多了 -