Note: This article was written in August 2018. As the SDK of ESP8266 and Ali Cloud platform update iteration is fast, some information may be out of date. The following solution is for reference only.

The introduction

As an Internet of Things developer, ESP8266 should not be unfamiliar at all. With only a small development board of more than ten yuan, you can connect to Wi-Fi to access the Internet, and enjoy the fun of controlling the device from the mobile end or the Web end. It is fine for ESP8266 to access Wi-Fi, but the client cannot be designed to communicate with ESP8266 directly. A central server is needed to act as the proxy between the client and the device. One solution is to build and design such a central server, just time-consuming and laborious; Another solution is to directly use the Internet of Things platform services provided by Dafang to PaaS the design scheme.

Internet search for Internet of things platform solutions, Baidu, Ali, Tencent have launched their own Internet of things platform, but also collect a little other companies platform. I then took a quick look at the three platforms, as well as some common ones. Finally, I chose Aliyun Internet of Things platform (hereinafter referred to as Aliyun IoT Hub) for in-depth understanding.

So, about ESP8266 and Ali Cloud IoT Hub, first to tell you, Lexin official Github has “ESP8266 docking Ali Cloud” repository, github: github.com/espressif/e…

But! When I downloaded it and ran some frantic compile tests, it still didn’t work! Most importantly, the Encore ESP8266 IDE is not supported, seemingly because of changes to the top Makfile file.

In a temper! According to the documents provided by AliYun IoT Hub, I first used Python script to simulate the device to connect to Aliyun IoT Hub, then used ESP8266 to try to connect to MQTT Broker, and finally used ESP8266 based on the official SDK to make an ESP8266 app by myself. It is connected to ali Cloud Internet of Things platform. Github:github.com/AngelLiang/…

Suitable for the reader

This article is suitable for readers with ESP8266 development experience, if familiar with anxin can ESP8266 IDE better. Readers familiar with Aliyun IoT Hub and development environment can directly skip to “4, download aliyun_mqtt_app and import” section.

1. Obtain the triad of Aliyun IoT equipment certification

This section mainly talks about how to obtain ali Cloud IoT Hub device authentication triplet, familiar readers can skip.

Step 1: Open the platform

First of all, you need to enter the Ali Cloud IoT Hub console for operation, if not opened directly open, free. The console connections: www.aliyun.com/product/iot

Step 2: Create the product

After opening, we first need to create a product, product name can be input, other default.

Step 3: Create a device

To create the device, enter any DeviceName.

Step 4: Obtain the authentication triplet

You end up with the device authentication triples: ProductKey, DeviceName, and DeviceSecret.

Two, anxin can ESP8266 IDE

The following is how to build anxin can ESP8266 IDE environment documents, familiar readers can directly skip:

  • How to install the Anxin integrated development environment: wiki.ai-thinker.com/ai_ide_inst…
  • How to use the anxin ESP series integrated development environment: wiki.ai-thinker.com/ai_ide_use
  • How to burn firmware for ESP series modules: wiki.ai-thinker.com/esp_downloa…

ESP8266 official SDK

Finally, how to download ESP8266 official SDK, you can go to Lexin official website or Github or download. Familiar readers can skip it. The SDK I used for development is the latest version: ESP8266_nonOS_SDK-2.2.1, aliyun_mqtt_app theoretically supports SDK 2.0+. (2018-11-02 update: Github has been updated to SDKv3.0.0, SDKv2.0 and SDKv3.0 are not compatible, please refer to this blog if compiling error is reported.)

After downloading the official SDK, compress and back up driver_lib, examples, and third_party folders before deleting them to avoid disturbing information during compilation.

Download aliyun_mqtt_app and import it

Download aliyun_mqtt_app and copy the aliyun_mqtt_app folder to it.

Edit the app/include/user_config.h file and modify the following information:

#define PRODUCT_KEY     "PRODUCT_KEY"
#define DEVICE_NAME     "DEVICE_NAME"
#define DEVICE_SECRET   "DEVICE_SECRET"

#define WIFI_SSID       "WIFI_SSID"
#define WIFI_PASS       "WIFI_PASS"

Copy the code

5. Access is successful

Compile, write, restart ESP8266, observe the information printed by the serial port, and look at the Ali Cloud IoT Hub console after feeling no exception, you can see that the device has been connected successfully!

Then go to the device’s topic list, and you can see that the number of Update topic messages has increased by one. That’s because I wrote in the code to post a “Hello” message whenever the connection succeeds.

Then try Posting to get Topic!

You can see the serial port print as follows:

TCP: data received 45 bytes
Receive topic: /PRODUCT_KEY/esp8266_test/get, data: hello wolrd! 
Copy the code

In this case, the device reports data to the platform and the platform sends commands to the device have been successfully tested.

Vi. About Aliyun IoT Hub

access

According to the document, Aliyun IoT Hub supports at least three device access modes:

  1. MQTT methods, including TCP based MQTT and WebSocket based MQTT
  2. CoAP
  3. HTTP

For ESP8266, the most convenient is based on MQTT-TCP. Of course, Aliyun IoT Hub supports not only single device access, but also gateway device access.

Billing terms

Currently, the platform is free. The fee is per message, but the first 1 million messages per month are free, which is plenty for Internet of Things enthusiasts like me to play around with.

Basic and advanced

In fact, Aliyun IoT Hub is divided into two versions: basic version and advanced version. Advanced edition in the basic edition on the basis of all functions, but also some more practical functions, of course, the advanced edition is free to open, the charge of advanced edition is more than a device daily living fee, 0.01 yuan/daily living equipment/day, currently each account has 10 devices free quota.

One thing that attracted me to the advanced version: the device’s data storage and query capabilities. As long as the device side uploads data according to the advanced version of the AliYun IoT Hub model and Alink protocol, aliyun IoT Hub will store the relevant data, and can also obtain historical data through the cloud API.

Related terms:

  • Object model: Alibaba Cloud IoT Hub describes the functions of devices in the cloud, including device attributes, services and events.
  • Alink protocol: a communication protocol defined by AliYun between devices and the cloud.

April 1, 2019 update: Too many people have asked about this t/H sensor program, here is a special note. This example I use Python (not MicroPython) on the computer to write MQTT client code to simulate the device, according to the Ali Cloud protocol to upload the value of the relevant attributes can be. So there is no 8266 temperature and humidity sensor program!! There is no 8266 T/H sensor program!! There is no 8266 T/H sensor program!! But there is a chance to update this back, the blogger has not touched 8266 for a long time.

So the introduction of Ali Cloud IoT Hub is here, the following is a simple talk I wrote aliyun_mqtt_app.

About aliyun_mqtt_app

Of course, I already mentioned the reason for the app project in the introduction. Here is a brief introduction to the app.

Since the MQTT sample project already exists in the ESP8266 official SDK, I added a bit of my own code to this project. One of the most troublesome is ali Cloud IoT Hub device authentication problem, direct use of MQTT example project to fill in the configuration information about MQTT can also be, but MQTT password to be generated in the PC end of the good, the use of string patchwork static generation device authentication triplet seems to be unable to do. Then simply let ESP8266 can dynamically generate ali Cloud MQTT password can be, that is, an HMACMD5 signature.

The core code generated by MQTT password is in the user/aliyun_mqtt.c/ gen_mqTT_password () function.

Then there are some accessibility features, such as the ability to configure Wi-Fi using SmartConfig, so you don’t have to write down wi-fi information in code. At the same time, Ali Cloud IoT Hub mostly uses JSON format to transfer data, to the APP cJSON parsing work after free to consider (ESP8266 use cJSON parser already has, in my ESP8266 project example collection warehouse).

Eight, epilogue

Since then, the long and long words have no words to write so much, this article briefly introduced how to use ESP8266 access Ali Cloud IoT Hub, as to what can play out the game depends on the developer’s imagination. Words, some platform should give me some soft text fee ~?

The relevant data

  • Esp8266 Aliyun MQTT app: github.com/AngelLiang/…
  • Ali cloud iot platform document: help.aliyun.com/product/305…
  • Aliyun IoT Hub-MQTT-TCP connection communication: help.aliyun.com/document_de…