ESP-01SWIFI模块、阿里云IOT、APP inventor实现物联网项目

本文记录了一个本科院校双创项目,通过ESP-01S模块连接阿里云IOT平台,结合APPInventor开发手机客户端,以及使用Arduino编程,实现了物联网设备的连接、消息收发功能。项目涉及ESP-01S的阿里云服务器接入、APPInventor的客户端应用开发以及Arduino的硬件控制,最终能够通过物联网平台进行数据交互。
摘要由CSDN通过智能技术生成

本科院校双创项目,记录一下我在其中实现物联网的过程


前言

        借助ESP-01SWIFI模块、阿里云IOT、APP inventor实现物联网项目,本文即记录项目实现过程,其中参考多篇文章实现。


一、阿里云IOT

        1.登录阿里云服务器,使用支付宝账号即可

        2.阿里云IOT平台

        3.管理控制台—>公共实例—>产品—>确认

        4.设备—>添加设备

        分别添加esp8266 与 APP 用以对接

         此时阿里云基础配置已基本足够

二、ESP-01S

1、接入阿里云服务器

          对与esp-01S模块,其通过串口与主控通信,主控通过串口发送AT指令配置该模块。在测试阶段,可使用串口助手对其控制,波特率115200。
ESP-01S接入阿里云

        所用到的指令如下:应注意clientId中添加' \ '

AT
AT+RST
AT+CWMODE=1
AT+CIPSNTPCFG=1,8,"ntp1.aliyun.com"
AT+CWJAP="WIFI名","WIFI密码"
AT+MQTTUSERCFG=0,1,"NULL","username","passwd",0,0,""
AT+MQTTCLIENTID=0,"clientId"

AT+MQTTCONN=0,"mqttHostUrl",1883,1

       连接成功后,阿里云服务器上设备连接状态将由“未激活”变为“在线”

2、消息发送与接收

AT指令连接阿里云平台

其中订阅主题与发布主题:

  •  阿里云服务器—>产品—>Topic类列表—>自定义Topic

        将黄字为ProductKey、蓝字为对应DeviceName   

 发布:  /iag77MwiXZa/${deviceName}/user/update
订阅:/iag77MwiXZa/${deviceName}/user/get
  •  消息发送

日志服务中

        可查看是否发送成功

  •  消息接收

        串口发送如下订阅主题

         此时对应设备Topic列表里则出现对应Topic

        发布消息,同时观察串口

 

 

         此时串口收到服务器设备发送内容

三、APP Inventor

        使用APP inventor开发手机客户端,主要参考如下文章:

基于appinventor开发阿里云物联网Android软件(胎教级包懂教程)_app inventor 物联网_HDUGEEK的博客-CSDN博客

        本人也是在该博客所分享的APP inventor离线版下开发,在GitHub下载该项目时,直接Download ZIP项目文件会受损,需要用到git,可参考下文:

如何使用git下载Github代码_git怎么下载代码_QBU-95的博客-CSDN博客

        本项目的aia文件已上传至该博客,修改对应物联网项目密钥等内容

其中:

  • port:1883
  • idDevice:MQTT 连接参数clientId
  • userna:MQTT 连接参数username
  • password:MQTT 连接参数password
  • ipAdreess:MQTT 连接参数mqttHostUrl

四、Arduino

        由于时间间隔较久且经历刷机,esp8266的arduino开发时所参考的文章及链接已找不到,单找到一个项目ino文件,且由于设备上交,程序具体是否可用本人已无法验证。印象中程序参考自GitHub上项目。

#include <Arduino.h>
#include <Servo.h>
#include <Ticker.h>
#include <Crypto.h>
#include <ESP8266WiFiMulti.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
using experimental::crypto::SHA256;


// 实例化一个对象 wifiMulti
ESP8266WiFiMulti wifiMulti;

WiFiClient espClient;

PubSubClient client(espClient);

void connetMqtt();
String signHmacSha256(String deviceId, String productKey, String deviceName, String deviceSecret, uint64_t timestamp);
void callback(char *topic, byte *payload, unsigned int length);
/*物联网配置参数*/
const String productKey = "####";              //productKey
const String deviceName = "####";               //deviceName
const String deviceSecret = "#####"; //deviceSecret
const String subTopic = "/" + productKey + "/" + deviceName + "/user/get"; //subTopic
const String pubTopic = "/" + productKey + "/" + deviceName + "/user/update";//pubTopic
const String regionId = "cn-shanghai"; //
const String serverUrl = productKey + ".iot-as-mqtt." + regionId + ".aliyuncs.com";//Url
const int serverPort = 1883;  //阿里云
/*WIFI配置参数*/
const char wifiName[] = "####";//WIFI名
const char wifiPassword[] = "####";//密码
/*接收数据*/
DynamicJsonDocument doc(1024);
int fire=0; //火情信息
int mode=0,done=0;//mode 0:手动    1:自动  done:0 舵机复位 1 舵机执行
int Myclient=0;//客户端信息标志位

/*舵机配置*/
#define PWM_pin 4 
Servo myservo;// create servo object
int pos = 0;    //舵机角度
Ticker timer1; 

void setup()
{
  myservo.attach(PWM_pin);
  myservo.write(0);
  // put your setup code here, to run once:
  Serial.begin(115200);

  /* 设置周期性定时1s,回调函数为timer1_cb,并启动定时器 */

  wifiMulti.addAP(wifiName, wifiPassword);
  Serial.println("");
  Serial.println("start connecting wifi...");
  
  while (wifiMulti.run() != WL_CONNECTED)
  {
    Serial.print(".");
    delay(1000);
  }
  Serial.println("============connect wifi success============");
  Serial.print("WiFi:");
  Serial.println(WiFi.SSID()); 
  Serial.print("localIP:");
  Serial.println(WiFi.localIP()); 

  connetMqtt();
}

void connetMqtt()
{
  Serial.println("start connect mqtt ....");
  client.setKeepAlive(60); //修改保留时间
  client.setServer(serverUrl.c_str(), serverPort);

  String deviceId = String(ESP.getChipId()); //设备芯片唯一序列号
  uint64_t timestamp = micros64();

  String clientId = deviceId + "|securemode=3,signmethod=hmacsha256,timestamp=" + timestamp + "|";
  String password = signHmacSha256(deviceId, productKey, deviceName, deviceSecret, timestamp);
  String username = deviceName + "&" + productKey;
  Serial.print("clientId:");
  Serial.println(clientId);
  Serial.print("username:");
  Serial.println(username);
  Serial.print("password:");
  Serial.println(password);

  client.connect(clientId.c_str(), username.c_str(), password.c_str());

  while (!client.connected())
  {
    /* code */
    delay(2000);
    client.connect(clientId.c_str(), username.c_str(), password.c_str());
    Serial.println("try connect mqtt...");
  }
  Serial.println("ok, mqtt connected!");
  client.subscribe(subTopic.c_str());
  client.setCallback(callback);
}

String signHmacSha256(String deviceId, String productKey, String deviceName, String deviceSecret, uint64_t timestamp)
{
  const char *key = deviceSecret.c_str();
  String data = "clientId" + deviceId + "deviceName" + deviceName + "productKey" + productKey + "timestamp" + timestamp;
  Serial.print("sha256:");
  Serial.println(data);
  return SHA256::hmac(data, key, strlen(key), SHA256::NATURAL_LENGTH);
}

void callback(char *topic, byte *payload, unsigned int length)
{
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  payload[length] = '\0';
  String message = String((char *)payload);
  Serial.println(message);
  //接收数据判断
  deserializeJson(doc, message);

  Myclient = doc["client"];
  fire= doc["fire"];
    

 if(Myclient){
    mode= doc["mode"];
 }
 
  if(mode==1 && fire>=40)done=1;
    else done=0;
  done= doc["done"];
  
  if(done==1){

    myservo.write(30);
  }else myservo.write(0);
}

void loop()
{
  // put your main code here, to run repeatedly:
  if (client.connected())
  {
    client.loop(); //心跳以及消息回调等
   
  }

}

其中代码是将单片机接收到的物联网 json类 数据进行提取并判断。

  deserializeJson(doc, message);

  Myclient = doc["client"];
  fire= doc["fire"];
    


总结

        站在别人的肩膀上完成该项目

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值