YFROBOT创客社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9857|回复: 8
打印 上一主题 下一主题

ESP8266 core for Arduino应用:远程遥控LED 基于yeelink云平台(无需连接其他控制板)

[复制链接]

签到天数: 866 天

[LV.10]以坛为家III

跳转到指定楼层
楼主
发表于 2016-4-28 10:34:49 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 AllBlue 于 2019-6-17 14:42 编辑

YEELINK 云平台已失联,帖子仅供参考学习,请谨慎阅读!!


Arduino CORE for ESP8266应用:远程遥控LED 基于yeelink云平台(无需连接其他控制板)
测试模块:为了方便测试,购买了一块 nodemcu(带通信芯片,可以直接下载程序)
首先你需要搭建Arduino开发环境,arduino IDE 直接下载程序进ESP8266,相当于下载常说的固件,所以连接方式为烧写模块:GPIO0拉低;我使用nodemcu就非常方便电路都连接好,直接下载即可!
电路图:
nodemcu引脚图:

连接好电路后,我们开始下载程序(程序获取yeelink平台的开关数据从而控制GPIO口输出高低):
[C] 纯文本查看 复制代码
/*
   ESP8266 TCPcleint 连接到NET WORK ,实现远程遥控LED
   基于yeelink 免费物联平台 [url=http://www.yeelink.net]www.yeelink.net[/url]
   by yfrobot
   [url=http://www.yfrobot.com]http://www.yfrobot.com[/url]
*/

#include <ESP8266WiFi.h>
#define ledPin 4                          // 定义ledPin连接到GPIO4
const char* ssid     = "XXXXXX";         // XXXXXX -- 使用时请修改为当前你的 wifi ssid
const char* password = "XXXXXX";     // XXXXXX -- 使用时请修改为当前你的 wifi 密码
const char* host = "www.yeelink.net";
const char* APIKEY = "XXXXXX";    //API KEY 
int deviceId = XXXXX;
int sensorId = XXXXX;

WiFiClient client;
const int tcpPort = 80;
char data[512] ;
int x = 0;
int dat = 0;

void setup() {
  WiFi.mode(WIFI_AP_STA);                 //set work mode:  WIFI_AP /WIFI_STA /WIFI_AP_STA
  Serial.begin(115200);
  pinMode(ledPin, OUTPUT);
  delay(10);

  // We start by connecting to a WiFi network
  Serial.println("");
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  delay(2000);

  if (!client.connect(host, tcpPort)) {
    Serial.println("connection failed");
    return;
  }

  // We now create a URI for the request
  String url = "/v1.0/device/";
  url += String(deviceId);
  url += "/sensor/";
  url += String(sensorId);
  url += "/datapoints";
  
  // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" + 
//               "Accept: */*\r\n"+ 
               "U-ApiKey:" + APIKEY + "\r\n"
               "Connection: close\r\n\r\n");
               
  unsigned long timeout = millis();
  while (client.available() == 0) {
    if (millis() - timeout > 2000) {
      Serial.println(">>> Client Timeout !");
      client.stop();
      return;
    }
  }
  
  // Read all the lines of the reply from server and print them to Serial
  while(client.available()){
    int e = client.read();
    if(e == '{'&&x == 0){
      x = 1;
    }else if(x == 1){
      data[dat] = e;
//      Serial.print(e);
      if(e == '}'){
        digitalWrite(ledPin,data[dat-1]-'0');
        Serial.print("button value :");
        Serial.print(data[dat-1]);
        Serial.print("\t");
        x = 0;
        dat = 0;
        break;
      }
      dat++;
    }
  }
  
//  Serial.println();
  Serial.println("closing connection");
}

//void colLED(char sta){
//    if (sta == '0') {
//      digitalWrite(ledPin, LOW);
//    }
//    if (sta == '1') {
//      digitalWrite(ledPin, HIGH);
//    }
//}
程序下载:

特别注意,程序非直接下载,需要根据实际情况进行相关参数修改:
     const char* ssid     = "XXXXXX";          //  使用时请修改为当前你的 wifi ssid
     const char* password = "XXXXXX";     //  使用时请修改为当前你的 wifi 密码
     //你的yeelink平台数据,APIKEY、设备ID、传感器ID。对这3个参数有疑问可以看:http://www.yfrobot.com/thread-11794-1-1.html
     const char* APIKEY = "XXXXXX";   
     int deviceId = XXXXX;
     int sensorId = XXXXX;

程序下载进去后,我们可以使用手机或者电脑网页来改变云端Smart Button状态:

这时 ESP8266 WIFI 模块去读取云端 LED状态值并设置当前LED的状态。
串口输出:

这样就实现了远程遥控了(反应有点慢,2S读取一次云端值)!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 支持支持 反对反对

该用户从未签到

沙发
发表于 2016-5-19 20:11:17 | 只看该作者
Arduino:1.6.9 (Windows 7), 开发板:"Arduino Nano, ATmega328"

C:\Users\Administrator\AppData\Local\Temp\HZ$D.283.2145\HZ$D.283.2146\remoteSwitch\remoteSwitch.ino:14:16: warning: overflow in implicit constant conversion [-Woverflow]

int deviceId = 347973;

                ^

C:\Users\Administrator\AppData\Local\Temp\HZ$D.283.2145\HZ$D.283.2146\remoteSwitch\remoteSwitch.ino:15:16: warning: overflow in implicit constant conversion [-Woverflow]

int sensorId = 389020;

                ^

remoteSwitch:17: error: 'WiFiClient' does not name a type

WiFiClient client;

^

C:\Users\Administrator\AppData\Local\Temp\HZ$D.283.2145\HZ$D.283.2146\remoteSwitch\remoteSwitch.ino: In function 'void setup()':

remoteSwitch:24: error: 'WiFi' was not declared in this scope

   WiFi.mode(WIFI_AP_STA);                 //set work mode:  WIFI_AP /WIFI_STA /WIFI_AP_STA

   ^

remoteSwitch:24: error: 'WIFI_AP_STA' was not declared in this scope

   WiFi.mode(WIFI_AP_STA);                 //set work mode:  WIFI_AP /WIFI_STA /WIFI_AP_STA

             ^

remoteSwitch:36: error: 'WL_CONNECTED' was not declared in this scope

   while (WiFi.status() != WL_CONNECTED) {

                           ^

C:\Users\Administrator\AppData\Local\Temp\HZ$D.283.2145\HZ$D.283.2146\remoteSwitch\remoteSwitch.ino: In function 'void loop()':

remoteSwitch:50: error: 'client' was not declared in this scope

   if (!client.connect(host, tcpPort)) {

        ^

remoteSwitch:63: error: 'client' was not declared in this scope

   client.print(String("GET ") + url + " HTTP/1.1\r\n" +

   ^

exit status 1
'WiFiClient' does not name a type

在文件 -> 首选项开启
“编译过程中显示详细输出”选项
这份报告会包含更多信息。
回复 支持 反对

使用道具 举报

该用户从未签到

板凳
发表于 2016-5-19 20:12:47 | 只看该作者
上面错误是什么意思啊,求解答!
回复 支持 反对

使用道具 举报

签到天数: 866 天

[LV.10]以坛为家III

地板
 楼主| 发表于 2016-5-21 08:10:51 | 只看该作者
kangzhuo 发表于 2016-5-19 20:12
上面错误是什么意思啊,求解答!

你用的是什么板子?
你没有正确选择开发板类型,你选择的是Nano!
开发环境搭建贴:http://www.yfrobot.com/thread-11797-1-1.html
回复 支持 反对

使用道具 举报

签到天数: 1 天

[LV.1]初来乍到

5#
发表于 2016-10-14 14:43:39 | 只看该作者
图片里面接的是GPIO2 程序就是4了.....
回复 支持 反对

使用道具 举报

签到天数: 866 天

[LV.10]以坛为家III

6#
 楼主| 发表于 2016-10-15 20:43:54 | 只看该作者
回忆泛黄 发表于 2016-10-14 14:43
图片里面接的是GPIO2 程序就是4了.....

看nodemcu的引脚图就知道了
回复 支持 反对

使用道具 举报

签到天数: 1 天

[LV.1]初来乍到

7#
发表于 2017-2-5 16:10:02 | 只看该作者
AllBlue 发表于 2016-10-15 20:43
看nodemcu的引脚图就知道了

对对 我没注意到之前
回复 支持 反对

使用道具 举报

签到天数: 22 天

[LV.4]偶尔看看III

8#
发表于 2019-5-6 17:19:52 | 只看该作者
不错,很值得学习一下
回复 支持 反对

使用道具 举报

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

本版积分规则

QQ|小黑屋|联系我们|YFROBOT ( 苏ICP备20009901号-2  

GMT+8, 2024-4-25 06:06 , Processed in 0.093341 second(s), 26 queries .

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

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