16671浏览
查看: 16671|回复: 9

[入门] esp8266 arduino 之 与服务器通信

[复制链接]
前一篇文章已经把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")
复制代码

hnyzcj  版主

发表于 2017-4-27 13:18:32

好教程值得一看
回复

使用道具 举报

hnyzcj  版主

发表于 2017-4-27 13:18:39

以后慢慢琢磨
回复

使用道具 举报

pATAq  版主

发表于 2017-4-27 19:47:30

mark一下
回复

使用道具 举报

dsweiliang  初级技神

发表于 2017-4-27 23:37:09

mark一下,学习学习了
回复

使用道具 举报

DeepMind  见习技师

发表于 2017-6-29 17:23:29

我想知道,能不能通过家里的无线路由器,连接到服务器上去?
回复

使用道具 举报

question  初级技师
 楼主|

发表于 2017-7-5 23:24:28

DeepMind 发表于 2017-6-29 17:23
我想知道,能不能通过家里的无线路由器,连接到服务器上去?

这个代码就是通过wifi和阿里云进行通信的例子
回复

使用道具 举报

ishmaelQ  学徒

发表于 2017-8-5 16:32:25

有没有用php通信的例子,
回复

使用道具 举报

question  初级技师
 楼主|

发表于 2017-8-26 00:33:23

ishmaelQ 发表于 2017-8-5 16:32
有没有用php通信的例子,

php的服务器我不会写 arduino那边是标准的get请求 你用PHP也能用的
回复

使用道具 举报

maomaopcy  学徒

发表于 2018-3-16 17:18:52

请问怎么下载要用到的库
回复

使用道具 举报

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

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail