只需一步,快速开始
微信扫一扫,快速登录
默认版
举报
yptsai7 发表于 2025-6-7 19:13 请问有支持ESP32(4MB)的小智AI?谢谢。
yptsai7 发表于 2025-6-9 09:25 请问可有播放WS2812的例子?谢谢。
yptsai7 发表于 2025-6-12 20:55 由于积木中没有命令为字串?我简单写个测试,部分码如下: void handleIotControl(const std::string& name ...
yptsai7 发表于 2025-6-7 19:08 请问可有MQTT的例子?谢谢。
yptsai7 发表于 2025-6-18 10:50 希望编程狮roco2的CoAI能增添:激活事件回调型别
本版积分规则 发表回复 回帖并转播 回帖后跳转到最后一页
管理员
2025-5-29
2025-5-22
2025-3-14
社区共建 资源共享
点评
举报
举报
仅支持s3
举报
点评
举报
由于积木中没有命令为字串?我简单写个测试,部分码如下:
void handleIotControl(const std::string& name, const std::string& function,
const std::map<std::string, ai_vox::iot::Value>& params) {
if (name == "strip") {
if (function == "Setstate") {
std::string color_str = std::get<std::string>(params.at("color"));
if (color_str.find("红色") != std::string::npos) {
strip.setLedColorData(0,0xFF0000);
} else if (color_str.find("绿色") != std::string::npos) {
strip.setLedColorData(0,0x00FF00);
} else if (color_str.find("蓝色") != std::string::npos) {
strip.setLedColorData(0,0x0000FF);
} else {
// 處理 HEX 顏色碼
}
strip.show();
g_strip_entity->UpdateState("state",color_str);
}
}
}
void setupIotDevices() {
{
std::vector<ai_vox::iot::Property> strip_props{
{"state", "灯条的颜色", ai_vox::iot::ValueType::kString}
};
std::vector<ai_vox::iot::Function> strip_funcs{
{"Setstate", "设置灯条的颜色", {
{"color", "设置灯条的颜色", ai_vox::iot::ValueType::kString}
}}
};
g_strip_entity = std::make_shared<ai_vox::iot::Entity>("strip", "灯条的颜色", strip_props, strip_funcs);
g_strip_entity->UpdateState("state", "");
g_ai_vox->registerIotDevice(g_strip_entity);
}
}
举报
在做这个库
举报
参考网路资料MQTT例子:https://wokwi.com/projects/357290123425943553
改写部分代码如下:
void handleIotControl(const std::string& name, const std::string& function,
const std::map<std::string, ai_vox::iot::Value>& params) {
if (name == "led") {
if (function == "TurnOn") {
MQTTClient.publish(MQTTPubTopic, "ON");
digitalWrite(21, LOW);
g_led_entity->UpdateState("state",true);
}
if (function == "TurnOff") {
MQTTClient.publish(MQTTPubTopic, "OFF");
digitalWrite(21, HIGH);
g_led_entity->UpdateState("state",false);
}
}
}
void setup() {
Serial.begin(115200);
AiVoxCore::AudioConfig audio_config{kMicPinBclk, kMicPinWs, kMicPinDin,kSpeakerPinBclk, kSpeakerPinWs, kSpeakerPinDout};
g_ai_vox = new AiVoxCore(audio_config, kTriggerPin, kWifiSsid, kWifiPassword);
OLEDDisplay::Init(kI2cPinSda, kI2cPinScl, kDisplayWidth, kDisplayHeight, kDisplayMirrorX, kDisplayMirrorY, kI2cAddress);
g_ai_vox->onStateChange(handleStateChange);
g_ai_vox->onChatMessage(handleChatMessage);
g_ai_vox->onEmotion(handleEmotion);
pinMode(21, OUTPUT);
digitalWrite(21,LOW);
g_ai_vox->onIotControl(handleIotControl);
setupIotDevices();
g_ai_vox->begin();
delay(5000);
//开始MQTT连线
MQTTConnecte();
digitalWrite(21, HIGH);
}
void loop() {
if (!MQTTClient.connected()) { MQTTConnecte();} // 如果 MQTT 连线中断,则重启 MQTT 连线
MQTTClient.loop();//更新订阅状态
delay(50);
repeat();
}
void repeat() {
g_ai_vox->update();
}
//开始MQTT连线
void MQTTConnecte() {
MQTTClient.setServer(MQTTServer, MQTTPort);
//以下开始处理订阅主题
//MQTTClient.setCallback(MQTTCallback); //收到订阅资料时,要处理MQTTCALLBACK
while (!MQTTClient.connected()) {
//以乱数为ClietID
String MQTTClientid = "esp32-" + String(random(1000000, 9999999));//建立一个邮差,为了要不一样,所以使用乱数
if (MQTTClient.connect(MQTTClientid.c_str(), MQTTUser, MQTTPassword)) {
//连结成功,显示「已连线」。
Serial.println("MQTT已连线");
MQTTClient.publish(MQTTPubTopic, "Hello");
} else {
//若连线不成功,则显示错误讯息,并重新连线
Serial.print("MQTT连线失败,状态码=");
Serial.println(MQTTClient.state());
Serial.println("五秒后重新连线");
delay(5000);
}
}
}
举报
举报
应用在哪些情况?
点评
举报