Arduino TopLevel Client for aliyun IoT Platform

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.

Owner
芋头
Developer/Designer/Productor/Manager
芋头
Comments
  • 找不到#include <SHA256.h>头文件

    找不到#include 头文件

    尝试了很多种办法用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,

    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 .

  • AliYun IoT device settings

    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

  • sendEvent(const char *eventId, const char *param)-----what is the format of the param.

    sendEvent(const char *eventId, const char *param)-----what is the format of the param.

    i try may times,but also compile error。 i know it is json format ,pls give me a example., like this {"ledstatue": on} or other ......pls .tks.

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

    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

  • SHA256.h: No such file or directory

    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
    
  • Specify library dependencies in library.properties

    Specify library dependencies in library.properties

    Specifying the library dependencies in the depends field of library.properties causes the Arduino Library Manager (Arduino IDE 1.8.10 and newer) to offer to install any missing dependencies during installation of this library.

    arduino-cli lib install will automatically install the dependencies (arduino-cli 0.7.0 and newer).

    Reference: https://github.com/arduino/Arduino/wiki/Arduino-IDE-1.5:-Library-specification#libraryproperties-file-format

  • 关于编译时错误 hwcrypto/aes.h: No such file or directory

    关于编译时错误 hwcrypto/aes.h: No such file or directory

    由于hwcrypto/* 在新版中移动到了 esp32/*. 编译会出现hwcrypto/aes.h: No such file or directory 的错误 解决方案如下:

  • 无法进入 subscribe 中的回调函数

    无法进入 subscribe 中的回调函数

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

  • new delete

    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;
    
A library to simplify the process of getting and storing data to Antares IoT Platform through HTTP on ESP8266.
A library to simplify the process of getting and storing data to Antares IoT Platform through HTTP on ESP8266.

Antares ESP8266 HTTP This is the documentation for Antares ESP8266 library. This library is meant to simplify the process of retrieving and deploying

Jul 2, 2021
A library to simplify the process of subscribing and publishing data to Antares IoT Platform through MQTT on ESP8266.
A library to simplify the process of subscribing and publishing data to Antares IoT Platform through MQTT on ESP8266.

Antares ESP8266 MQTT A Library to simplify the process of MQTT publication and subscription to Antares IoT Platform using ESP8266. This library works

Mar 9, 2022
AlmaviosLitMqtt allows an easy connection(IoT) to cloud providers

AlmaviosLitMqtt A simple arduino library provisioning a client to connect with Cloud Providers, via MQTT. It is looking for agnostic connection. This

Nov 20, 2020
Anto client library for ESP8266-Arduino

Anto client library for ESP8266-Arduino ESP8266-AntoIO provides common and easy way to connect your ESP8266 to Anto.io IoT platform service. Stable ve

Dec 1, 2021
Arduino client library for websocket based communication with archer panels.

ArduinoArcherPanelClient Arduino client library for websocket based communication with archer panels. Archer Cloud panels provide you an easy and fast

May 18, 2018
Arduino HTTP Client library

ArduinoHttpClient ArduinoHttpClient is a library to make it easier to interact with web servers from Arduino. Derived from Adrian McEwen's HttpClient

Jan 6, 2023
MQTT client for Arduino

ArduinoMqtt About MQTT Client library for Arduino based on the Eclipse Paho project. This library bundles the C/C++ MQTTPacket library of the Eclipse

Nov 11, 2022
Arduino client for the Serial To TCP Bridge Protocol PC side service

Arduino Serial to TCP Bridge Client Arduino client for the Serial To TCP Bridge Protocol gateway service. Open a TCP connection to a server from the A

Apr 12, 2022
Websocket client for Arduino, with fast data send

Websocket client for Arduino, with fast data send This is a simple library that implements a Websocket client running on an Arduino. Rationale For our

Aug 4, 2022
OAuth 1.0 client library for Arduino

Arduino_OAuth Library for Arduino OAuth 1.0 client library for Arduino. This library depends on ArduinoHttpClient and ArduinoBearSSL. License Copyrigh

Dec 13, 2022
Arduino Arduino library for the CloudStorage server project. The library provides easy access to server-stored values and operations.

Arduino-CloudStorage Arduino/ESP8266 library that allows you to easly store and retreive data from a remote (cloud) storage in a key/value fashion. Cl

Jan 30, 2022
Arduino library for making an IHC in or output module using an Arduino

Introduction This is an Arduino library for making an IHC in or output module using an Arduino. (IHC controller is a home automation controller made b

Mar 26, 2020
The World's Most Useless Arduino Platform.
The World's Most Useless Arduino Platform.

boot2duino boot2duino serves no other purpose than to generate a bootable x86 floppy image using the Arduino IDE, which seemingly serves no purpose. W

Nov 6, 2022
Server side minimalistic HTTP protocol implementation for the Arduino platform.

ArduinoHttpServer Server side minimalistic Object Oriented HTTP protocol implementation for the Arduino platform. ArduinoHttpServer is a simple HTTP r

Oct 17, 2022
A simple C++ API client for the Ark Blockchain.
A simple C++ API client for the Ark Blockchain.

Ark C++ - Client A simple C++ API client for the Ark Blockchain. Lead Maintainer: Simon Downey Documentation You can find installation instructions an

Jan 21, 2022
Arduino library for controlling the MCP2515 in order to receive/transmit CAN frames.
Arduino library for controlling the MCP2515 in order to receive/transmit CAN frames.

107-Arduino-MCP2515 Arduino library for controlling the MCP2515 in order to receive/transmit CAN frames. This library is prepared to interface easily

Nov 16, 2022
Arduino library for interfacing with any GPS, GLONASS, Galileo or GNSS module and interpreting its NMEA messages.
Arduino library for interfacing with any GPS, GLONASS, Galileo or GNSS module and interpreting its NMEA messages.

107-Arduino-NMEA-Parser Arduino library for interfacing with any GPS, GLONASS, Galileo or GNSS module and interpreting its NMEA messages. This library

Jan 1, 2023
Arduino library for providing a convenient C++ interface for accessing UAVCAN.
Arduino library for providing a convenient C++ interface for accessing UAVCAN.

107-Arduino-UAVCAN Arduino library for providing a convenient C++ interface for accessing UAVCAN (v1.0-beta) utilizing libcanard. This library works f

Jan 2, 2023
A RESTful environment for Arduino

aREST Overview A simple library that implements a REST API for Arduino & the ESP8266 WiFi chip. It is designed to be universal and currently supports

Dec 29, 2022