GithubHelp home page GithubHelp logo

yootou-dev / arduino-aliyun-iot-sdk Goto Github PK

View Code? Open in Web Editor NEW
7.0 1.0 3.0 33 KB

运行于 arduino 的 阿里云 IoT 接入 SDK,在底层连接的基础上增加数据回调绑定、发送数据和事件等 api 的封装,免去自己解析数据的痛苦

License: MIT License

C++ 100.00%

arduino-aliyun-iot-sdk's Introduction

Arduino TopLevel Client for aliyun IoT Platform

AliyunIoTSDK 可以帮助你快速连接阿里云 IoT 平台,通过和阿里云物联网开发平台配合,可快速实现各种硬件应用,包括了很上层的封装,无需自己解析数据体,绑定事件即可,在 esp8266 平台充分测试(NodeMCU 1.0)

update

  • v0.2 增加属性发送 buffer,5秒一次或者10条buffer满,才会一起发送数据,节省请求次数
  • v0.1 上线

依赖项

  • Arduino需要安装 ArduinoJson,Crypto,PubSubClient库
  • Esp8266 需要在Arduino中安装 ESP8266库

Usage 使用示例

// 引入 wifi 模块,并实例化,不同的芯片这里的依赖可能不同
#include <ESP8266WiFi.h>
static WiFiClient espClient;

// 引入阿里云 IoT SDK
#include <AliyunIoTSDK.h>

// 设置产品和设备的信息,从阿里云设备信息里查看
#define PRODUCT_KEY "xxx"
#define DEVICE_NAME "Device_D"
#define DEVICE_SECRET "xxxxxxxxxxxxxx"
#define REGION_ID "cn-shanghai"

// 设置 wifi 信息
#define WIFI_SSID "xxxxx"
#define WIFI_PASSWD "xxxxx"

void setup()
{
    Serial.begin(115200);
    
    // 初始化 wifi
    wifiInit(WIFI_SSID, WIFI_PASSWD);
    
    // 初始化 iot,需传入 wifi 的 client,和设备产品信息
    AliyunIoTSDK::begin(espClient, PRODUCT_KEY, DEVICE_NAME, DEVICE_SECRET, REGION_ID);
    
    // 绑定一个设备属性回调,当远程修改此属性,会触发 powerCallback
    // PowerSwitch 是在设备产品中定义的物联网模型的 id
    AliyunIoTSDK::bindData("PowerSwitch", powerCallback);
    
    // 发送一个数据到云平台,LightLuminance 是在设备产品中定义的物联网模型的 id
    AliyunIoTSDK::send("LightLuminance", 100);
}

void loop()
{
    AliyunIoTSDK::loop();
}

// 初始化 wifi 连接
void wifiInit(const char *ssid, const char *passphrase)
{
    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid, passphrase);
    while (WiFi.status() != WL_CONNECTED)
    {
        delay(1000);
        Serial.println("WiFi not Connect");
    }
    Serial.println("Connected to AP");
}

// 电源属性修改的回调函数
void powerCallback(JsonVariant p)
{
    int PowerSwitch = p["PowerSwitch"];
    if (PowerSwitch == 1)
    {
        // 启动设备
    } 
}

API 可用方法

// 在主程序 loop 中调用,检查连接和定时发送信息
  static void loop();

  /**
   * 初始化程序
   * @param ssid wifi名
   * @param passphrase wifi密码
   */
  static void begin(Client &espClient,
                    const char *_productKey,
                    const char *_deviceName,
                    const char *_deviceSecret,
                    const char *_region);

  /**
   * 发送数据
   * @param param 字符串形式的json 数据,例如 {"${key}":"${value}"}
   */
  static void send(const char *param);
  /**
   * 发送 float 格式数据
   * @param key 数据的 key
   * @param number 数据的值
   */
  static void send(char *key, float number);
  /**
   * 发送 int 格式数据
   * @param key 数据的 key
   * @param number 数据的值
   */
  static void send(char *key, int number);
  /**
   * 发送 double 格式数据
   * @param key 数据的 key
   * @param number 数据的值
   */
  static void send(char *key, double number);
  /**
   * 发送 string 格式数据
   * @param key 数据的 key
   * @param text 数据的值
   */
  static void send(char *key, char *text);

  /**
   * 发送事件到云平台(附带数据)
   * @param eventId 事件名,在阿里云物模型中定义好的
   * @param param 字符串形式的json 数据,例如 {"${key}":"${value}"}
   */
  static void sendEvent(const char *eventId, const char *param);
  /**
   * 发送事件到云平台(空数据)
   * @param eventId 事件名,在阿里云物模型中定义好的
   */
  static void sendEvent(const char *eventId);

  /**
   * 绑定回调,所有云服务下发的数据都会进回调
   */
  // static void bind(MQTT_CALLBACK_SIGNATURE);

  /**
   * 绑定事件回调,云服务下发的特定事件会进入回调
   * @param eventId 事件名
   */
  // static void bindEvent(const char * eventId, MQTT_CALLBACK_SIGNATURE);
  /**
   * 绑定属性回调,云服务下发的数据包含此 key 会进入回调,用于监听特定数据的下发
   * @param key 物模型的key
   */
  static int bindData(char *key, poniter_fun fp);
  /**
   * 卸载某个 key 的所有回调(慎用)
   * @param key 物模型的key
   */
  static int unbindData(char *key);

Examples 示例

buiding...

Limitations 使用限制和说明

  • 本库不包含 wifi 连接的代码,需先建立连接,然后将 client 传入
  • 依赖 PubSubClient ,在使用前,请务必修改 PubSubClient 的连接参数,否则无法使用
  • PubSubClient 中的 MQTT_MAX_PACKET_SIZE 修改为 1024
  • PubSubClient 中的 MQTT_KEEPALIVE 修改为 60
  • 掉线后会一直尝试重新连接,可能会触发阿里云的一些限流规则(已经做了规避),并且会导致挤掉其他同设备 ID 的设备
  • 默认 5000ms 检测一次连接状态,可以通过 CHECK_INTERVAL 修改此值

Compatible Hardware 适用硬件

本 SDK 基于 PubSubClient 底层库开发,兼容列表与 PubSubClient 相同。

The library uses the Arduino Ethernet Client api for interacting with the underlying network hardware. This means it Just Works with a growing number of boards and shields, including:

  • Arduino Ethernet
  • Arduino Ethernet Shield
  • Arduino YUN – use the included YunClient in place of EthernetClient, and be sure to do a Bridge.begin() first
  • Arduino WiFi Shield - if you want to send packets > 90 bytes with this shield, enable the MQTT_MAX_TRANSFER_SIZE define in PubSubClient.h.
  • Sparkfun WiFly Shield – library
  • TI CC3000 WiFi - library
  • Intel Galileo/Edison
  • ESP8266
  • ESP32

The library cannot currently be used with hardware based on the ENC28J60 chip – such as the Nanode or the Nuelectronics Ethernet Shield. For those, there is an alternative library available.

License

This code is released under the MIT License.

arduino-aliyun-iot-sdk's People

Contributors

billypon avatar hthing avatar njh avatar per1234 avatar small-tou avatar tu6ge avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

arduino-aliyun-iot-sdk's Issues

use the library to compile. has some issues. pls help me!

E:\IoT\test-Project\libraries\AliyunIoTSDK\src\AliyunIoTSDK.cpp: In function 'void parmPass(ArduinoJson::JsonVariant)':

E:\IoT\test-Project\libraries\AliyunIoTSDK\src\AliyunIoTSDK.cpp:72:42: error: 'ArduinoJson::Internals::EnableIf<true, ArduinoJson::Internals::JsonObjectSubscript<const char*> >::type {aka class ArduinoJson::Internals::JsonObjectSubscript<const char*>}' has no member named 'containsKey'

         bool hasKey = parm["params"].containsKey(poniter_array[i].key);

                                      ^

E:\IoT\test-Project\libraries\AliyunIoTSDK\src\AliyunIoTSDK.cpp: In function 'void callback(char*, byte*, unsigned int)':

E:\IoT\test-Project\libraries\AliyunIoTSDK\src\AliyunIoTSDK.cpp:92:9: error: 'StaticJsonDocument' was not declared in this scope

     StaticJsonDocument<200> doc;

     ^

E:\IoT\test-Project\libraries\AliyunIoTSDK\src\AliyunIoTSDK.cpp:92:33: error: 'doc' was not declared in this scope

     StaticJsonDocument<200> doc;

                             ^

E:\IoT\test-Project\libraries\AliyunIoTSDK\src\AliyunIoTSDK.cpp:93:9: error: 'DeserializationError' was not declared in this scope

     DeserializationError error = deserializeJson(doc, payload); //反序列化JSON数据

     ^

E:\IoT\test-Project\libraries\AliyunIoTSDK\src\AliyunIoTSDK.cpp:95:14: error: 'error' was not declared in this scope

     if (!error) //检查反序列化是否成功

          ^

E:\IoT\test-Project\libraries\AliyunIoTSDK\src\AliyunIoTSDK.cpp:97:40: error: expected primary-expression before '>' token

         parmPass(doc.as<JsonVariant>()); //将参数传递后打印输出

                                    ^

E:\IoT\test-Project\libraries\AliyunIoTSDK\src\AliyunIoTSDK.cpp:97:42: error: expected primary-expression before ')' token

         parmPass(doc.as<JsonVariant>()); //将参数传递后打印输出

                                      ^

"WiFi.h" 对应多个库
已使用: E:\IoT\Arduino\hardware\espressif\esp32\libraries\WiFi
未使用:E:\IoT\Arduino\libraries\WiFi

AliYun IoT device settings

Hello, the MQTT gets a err -1 .

I set up and used:

#define PRODUCT_KEY "a1D3XrOF0mM"
#define DEVICE_NAME "QT1_04_80001"
#define DEVICE_SECRET "d48aa96143167af9e6de8879155f6752"
#define REGION_ID "cn-shanghai"

image

thanks in advance

Terence

增加OTA功能

在我自己使用该库时,同时也需要使用阿里云下发OTA方式进行固件更新,于是在该库自己增加了OTA功能,该功能说不定也是其他人所需要的,所以这个要不也加上去?

.

issue with aliyun

当断网时esp8266程序卡在发送数据上云

我现在用这个库的时候遇到了一个问题,就是在wifi强度一般的地方,它的数据上不了云的时候,会一直卡在上云的地方。当wifi强度变好,自己还是卡在上云地方。谢谢作者这个好用的库,用的很开心。

无法进入 subscribe 中的回调函数

您好,我在使用您开发的 AliyunIoTSDK 库的时候,使用 AliyunIoTSDK::subscribe("/sys/a1L4LpLaySY/ESPduan/thing/service/property/set", callback);
然后在云端发送数据,但是无法进入 callback 这个回调函数,请问是什么问题呀,谢谢。

SHA256.h: No such file or directory

hi
Which sha256.h are you using?
I cannot find it.

/Users/terao/Documents/Arduino/libraries/AliyunIoTSDK/src/AliyunIoTSDK.cpp:4:20: fatal error: SHA256.h: No such file or directory

new delete

boolean AliyunIoTSDK::publishUser(const char *topicSuffix, const char *payload){
    char topic[150]; 
    strcpy(topic, ALINK_TOPIC_USER);
    return AliyunIoTSDK::publish(strcat(topic, topicSuffix), payload);
}

boolean AliyunIoTSDK::subscribeUser(const char *topicSuffix, poniter_fun fp){
    char *topic = new char[150];
    strcpy(topic, ALINK_TOPIC_USER);
    return AliyunIoTSDK::subscribe(strcat(topic, topicSuffix), fp);
}

boolean AliyunIoTSDK::unsubscribeUser(char *topicSuffix){
    char *topic = new char[150];
    strcpy(topic, ALINK_TOPIC_USER);
    return AliyunIoTSDK::unsubscribe(strcat(topic, topicSuffix));
}

new 是否需要 delete

void AliyunIoTSDK::begin(Client &espClient,
                         const char *_productKey,
                         const char *_deviceName,
                         const char *_deviceSecret,
                         const char *_region)
{
    if (NULL != client)
        delete client;
        
    client = new PubSubClient(espClient);
...
}

网络重连时,重新调用begin() 是否需要 delete client

    if (NULL != client)
        delete client;

找不到#include <SHA256.h>头文件

尝试了很多种办法用esp8266连接阿里云,

  1. 参考NodeMCU(ESP8266)接入阿里云物联网平台这篇博客,直接自己贴上mqtt的password,先是报错error=-1的错,没有修改#include <PubSubClient.h>的头文件修改之后,参考这篇博客基于开源MQTT自主接入阿里云IoT平台,修改相应密码报,errr=4的错误,用户或密码格式数据无效,不懂了???

  2. 参考这篇博客,直接代码生产密码,ESP8266(arduino方式)快速连接阿里云物联网平台,和博主的方法大致是一样的,但是运行到最后也是缺少include<SHA256.h>头文件
    博主能解答一下吗,谢谢!

Assistance please: getting property not found parsing error,

based on the INO example,
set up ok
device on line ok,

基于 INO 示例,
设置确定
设备在线确定,

reading the Device Log (Cloud run log)
阅读设备日志(云运行日志)

{"id": "123","version": "1.0","params": {},"method": "thing.event.xxx.post"}
{"Content":"Publish message to topic:/sys/a1RPb9ZIzak/QT1_80003/thing/event/xxx/post,QoS=0"}

{"id":"123","version":"1.0","method":"thing.event.property.post","params":{"CurrentTemperature":30}}

i get the error
我得到的错误

{"Params":{"CurrentTemperature":30},"ResultData":{"CurrentTemperature":"5092 - property not found"},"Reason":"tsl parse failed"}

has a status 6332 .

clearly I am misunderstanding something in the setting.

However, I am located in Australia, and the REGION_ID is Shanghai ? is that an issue.

显然,我误解了设置中的东西。

然而,我位于澳大利亚,REGION_ID是上海?这是一个问题。

Thank you very much .. :)

Terence .

MQTT Connect err:2

设备:ESP8266
我尝试连接阿里云 返回错误 MQTT Connect err:2
REGION_ID被设置为cn-shanghai,其余的信息均准确无误,网络连接正常……
由于我对C++并不熟悉,无法进一步查明原因,望dalao相助

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.