MG Mixly模块

作者:头像   段 森宝   |   贡献者:   hardihuang   

基础模块 #

1.模拟输出

2.蜂鸣器

3.THD11温湿度传感器

4.微动开关:代码内自带输入上拉

5.GY-906温度传感器

6.心率传感器

7.原生态for循坏

8.LD3320语音模块

arduino 代码

#include <ld3320.h>

VoiceRecognition Voice;
/*
ld3320引脚连接图
VCC  --> 3.3V
GND  --> GND
MISO --> D12
MOSI --> D11
SCK  --> D13
CS   --> D4
RST  --> D9
IRQ  --> D2
WR   --> GND
*/

void setup(){
  Voice.init(); //初始化VoiceRecognition模块
  Voice.addCommand("kai",1);   //添加指令,参数(指令内容,指令标签(可重复))
  Voice.addCommand("guan",0);   //添加指令,参数(指令内容,指令标签(可重复))
  Voice.start();   //开始识别
  pinMode(2, OUTPUT);
}

void loop(){
  /*
  模拟开关效果,可以用led查看效果
  引脚2为led灯的引脚
  */

  if (0 == Voice.read()) {
    digitalWrite(2,LOW);

  } else if (1 == Voice.read()) {
    digitalWrite(2,HIGH);
  }

}

9.数码管0-9数字显示switch函数

舵机 #

电机驱动 #

arduino 代码

void mago_setMotor(int motorId, int motorDir) {
 int directionPin1, directionPin2;
 if (motorId == 1) {         // motor A
 directionPin2 = 3;
 directionPin1 = 2;
 } else if (motorId == 2) {  // motor B
 directionPin2 = 5;
 directionPin1 = 4;
 } else if (motorId == 3) {  // motor C
 directionPin2 = 6;
 directionPin1 = 7;
 } else if (motorId == 4) {  // motor D
 directionPin2 = 8;
 directionPin1 = 9;
 } else {
 return;
 }
 if(motorDir == 1) {
 digitalWrite(directionPin1, HIGH);
 digitalWrite(directionPin2, LOW);
 } else if(motorDir == 0) {
 digitalWrite(directionPin1, HIGH);
 digitalWrite(directionPin2, LOW);
 }
 }

void mago_setMotor_stop(int motorId, int motorDir) {
int directionPin1, directionPin2;
if (motorId == 1) {         // motor A
directionPin2 = 3;
directionPin1 = 2;
} else if (motorId == 2) {  // motor B
directionPin2 = 5;
directionPin1 = 4;
} else if (motorId == 3) {  // motor C
directionPin2 = 6;
directionPin1 = 7;
} else if (motorId == 4) {  // motor D
directionPin2 = 8;
directionPin1 = 9;
} else {
return;
}
digitalWrite(directionPin1, LOW);
digitalWrite(directionPin2, LOW);
}

void setup(){
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
}

void loop(){
  mago_setMotor(1,1);

  mago_setMotor_stop(1,0);

}

AI LAB #

1.esp8266AP模式初始化

版型选择arduino esp8266

上传代码后,esp8266会发射一个ESP开头的热点打开手机wifi

连接ESP开头的热点会跳入一个配置WiFi的页面(每个esp8266的后缀数字都不一样,但是前缀”ESP”都是一样的)

点击配置WiFi,点击你所知道的WiFi,输入密码

现在esp8266已连接网络了

AP模式的优势是可以随时配置WiFi,而不需要改代码

2.api接口获取网络数据(以网络时间为例)

搜索引擎搜索”时间api”

我们选择”苏宁”的api:http://quan.suning.com/getSysTime.do

点击该网址我们得到一串JSON数据{“sysTime2″:”2021-04-07 17:30:25″,”sysTime1″:”20210407173025”}.我们现在需要将该数据传输到esp8266中

API-url状态码:正确的状态码是”200″,返回其他的都是不正常的.我们可以打开浏览器的F12查看

这样就获取网络时间了

arduino 代码

#define BLINKER_ESP_SMARTCONFIG
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
#include <FlagHTTPClient.h>
#include <ArduinoJson.h>
#include <FlagJSON.h>

void configModeCallback (WiFiManager *myWiFiManager) {
Serial.println(WiFi.softAPIP());
Serial.println(myWiFiManager->getConfigPortalSSID());
}

FlagHTTPClient _httpClient;
int _httpGET(String url) {
_httpClient.end();
_httpClient.begin(url);
_httpClient.setTimeout(30000);
return _httpClient.GET();
}

JsonVariant _jsonRoot;
DynamicJsonBuffer _jsonBuffer;

void setup(){
  Serial.begin(115200);
  WiFiManager wifiManager;
  wifiManager.setAPCallback(configModeCallback);
  if(!wifiManager.autoConnect()) {
  Serial.println("failed to connect and hit timeout");
  ESP.reset();
  delay(1000);
}
Serial.println("connected...yeey");

  Serial.begin(9600);
}

void loop(){
  Serial.println(_httpGET("http://quan.suning.com/getSysTime.do"));
  Serial.println(_httpClient.getString());
  _jsonBuffer.clear();
  _jsonRoot = _jsonBuffer.parse(_httpClient.getString());
  Serial.println(_jsonRoot.success());
  Serial.println(flagJSON.getJSONDataAsString(_jsonRoot,"sysTime2"));
  delay(10000);

}

MQTT #

1.发布主题

arduino 代码

#define BLINKER_ESP_SMARTCONFIG
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
WiFiClient client;

void configModeCallback (WiFiManager *myWiFiManager) {
Serial.println(WiFi.softAPIP());
Serial.println(myWiFiManager->getConfigPortalSSID());
}
Adafruit_MQTT_Client mqtt(&client, "106.52.84.175", 1883, "", "");
void MQTT_connect();
void MQTT_connect() {
  int8_t ret;
  if (mqtt.connected()) {
    return;
  }
  Serial.print("Connecting to MQTT... ");
  uint8_t retries = 3;
  while ((ret = mqtt.connect()) != 0) {
    Serial.println(mqtt.connectErrorString(ret));
    Serial.println("Retrying MQTT connection in 5 seconds...");
    mqtt.disconnect();
    delay(5000);
    retries--;
    if (retries == 0) {
      while (1);
    }
  }
  Serial.println("MQTT Connected!");
}

Adafruit_MQTT_Publish light = Adafruit_MQTT_Publish(&mqtt, "iot/light");

void setup(){
  Serial.begin(115200);
  WiFiManager wifiManager;
  wifiManager.setAPCallback(configModeCallback);
  if(!wifiManager.autoConnect()) {
  Serial.println("failed to connect and hit timeout");
  ESP.reset();
  delay(1000);
}
Serial.println("connected...yeey");

}

void loop(){
  MQTT_connect();
  light.publish(1);

}

2.订阅主题

arduino 代码

#define BLINKER_ESP_SMARTCONFIG
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
WiFiClient client;

void configModeCallback (WiFiManager *myWiFiManager) {
Serial.println(WiFi.softAPIP());
Serial.println(myWiFiManager->getConfigPortalSSID());
}
Adafruit_MQTT_Client mqtt(&client, "106.52.84.175", 1883, "", "");
void MQTT_connect();
void MQTT_connect() {
  int8_t ret;
  if (mqtt.connected()) {
    return;
  }
  Serial.print("Connecting to MQTT... ");
  uint8_t retries = 3;
  while ((ret = mqtt.connect()) != 0) {
    Serial.println(mqtt.connectErrorString(ret));
    Serial.println("Retrying MQTT connection in 5 seconds...");
    mqtt.disconnect();
    delay(5000);
    retries--;
    if (retries == 0) {
      while (1);
    }
  }
  Serial.println("MQTT Connected!");
}

Adafruit_MQTT_Subscribe light = Adafruit_MQTT_Subscribe(&mqtt,"iot/light");

void setup(){
  Serial.begin(115200);
  WiFiManager wifiManager;
  wifiManager.setAPCallback(configModeCallback);
  if(!wifiManager.autoConnect()) {
  Serial.println("failed to connect and hit timeout");
  ESP.reset();
  delay(1000);
}
Serial.println("connected...yeey");

  Serial.begin(9600);
  mqtt.subscribe(&light);
}

void loop(){
  MQTT_connect();
  Adafruit_MQTT_Subscribe *subscription2110;
  while ((subscription2110 = mqtt.readSubscription(5000))) {
    if (subscription2110 ==&light) {
      Serial.println((char *)light.lastread);

    }
  }

}

需要注意:发布和订阅的项目ID和主题需一致

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注