当前位置:网站首页>Esp8266 uses Arduino to connect Alibaba cloud Internet of things
Esp8266 uses Arduino to connect Alibaba cloud Internet of things
2022-07-06 12:05:00 【A geek is as deep as the sea】
Use here AliyunIoTSDK Library for fast access
Need to make preliminary preparations
install
The library files 【AliyunIoTSDK】
-【ArduinoJson】
-【Crypto】
-【PubSubClient】 This is mqtt The support package What is shown in the picture 1 and 2 Fine , Choose one of them . I have installed it here because of some other projects . It will not affect the later development
Alibaba cloud device creation ( The explanation of pictures is too troublesome , Go directly to the video ) Video link
// introduce wifi modular , And instantiate ,
#include <ESP8266WiFi.h>
static WiFiClient espClient;
// Introducing Alibaba cloud IoT SDK
#include <AliyunIoTSDK.h>
// Set product and device information , Check from alicloud device information
#define PRODUCT_KEY "a1rCF0cxG0w"
#define DEVICE_NAME "ceshi"
#define DEVICE_SECRET "32f735fc37b68a86b6eed749f1ba2bf8"
#define REGION_ID "cn-shanghai"
// Set up wifi Information
#define WIFI_SSID "xxxx"
#define WIFI_PASSWD "12345678"
#define led D4
void setup()
{
Serial.begin(115200);
// initialization wifi
wifiInit(WIFI_SSID, WIFI_PASSWD);
// initialization iot, Need to transfer in wifi Of client, And device product information
AliyunIoTSDK::begin(espClient, PRODUCT_KEY, DEVICE_NAME, DEVICE_SECRET, REGION_ID);
// Bind a device property callback , When this property is modified remotely , Will trigger powerCallback
// PowerSwitch It refers to the Internet of things model defined in the device product id
AliyunIoTSDK::bindData("PowerSwitch_1", powerCallback);
}
void loop()
{
AliyunIoTSDK::send("CurrentTemperature", ((float)random(1000,9999))/100);
AliyunIoTSDK::send("CurrentHumidity", (int)random(0,100));
AliyunIoTSDK::send("PowerSwitch_1", !(int)digitalRead(led));
delay(2000);
AliyunIoTSDK::loop();
}
// initialization wifi Connect
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");
pinMode(led,OUTPUT);
}
// Callback function for power attribute modification
void powerCallback(JsonVariant p)
{
int PowerSwitch = p["PowerSwitch_1"];
if (PowerSwitch == 1)
{
digitalWrite(led,0);
}
if (PowerSwitch == 0)
{
digitalWrite(led,1);
}
}```
边栏推荐
- Variable parameter principle of C language function: VA_ start、va_ Arg and VA_ end
- STM32 如何定位导致发生 hard fault 的代码段
- Some concepts often asked in database interview
- 共用体(union)详解【C语言】
- 【ESP32学习-2】esp32地址映射
- Matlab learning and actual combat notes
- Come and walk into the JVM
- Several declarations about pointers [C language]
- Vert. x: A simple login access demo (simple use of router)
- List and set
猜你喜欢
Several declarations about pointers [C language]
【ESP32学习-1】Arduino ESP32开发环境搭建
Small L's test paper
OPPO VOOC快充电路和协议
AMBA、AHB、APB、AXI的理解
Programmers can make mistakes. Basic pointers and arrays of C language
优先级反转与死锁
Pytorch four commonly used optimizer tests
Kconfig Kbuild
Apprentissage automatique - - régression linéaire (sklearn)
随机推荐
R & D thinking 01 ----- classic of embedded intelligent product development process
Gallery之图片浏览、组件学习
Comparaison des solutions pour la plate - forme mobile Qualcomm & MTK & Kirin USB 3.0
STM32 如何定位导致发生 hard fault 的代码段
A possible cause and solution of "stuck" main thread of RT thread
Cannot change version of project facet Dynamic Web Module to 2.3.
IOT system framework learning
关键字 inline (内联函数)用法解析【C语言】
STM32型号与Contex m对应关系
Small L's test paper
Détails du Protocole Internet
Several declarations about pointers [C language]
Kconfig Kbuild
物联网系统框架学习
Imgcat usage experience
ESP8266使用arduino连接阿里云物联网
open-mmlab labelImg mmdetection
Linux Yum install MySQL
Reno7 60W超级闪充充电架构
ESP8266通过arduino IED连接巴法云(TCP创客云)