|
|

楼主 |
发表于 2024-4-20 02:10:44
|
显示全部楼层
- /*!
- * MindPlus
- * mpython
- *
- */
- #include <MPython.h>
- #include <DFRobot_Iot.h>
- #include <Maqueen_Motor.h>
- #include <DFRobot_IRremote.h>
- // 动态变量
- volatile float mind_n_L, mind_n_R;
- // 函数声明
- void onButtonAPressed();
- void onButtonBPressed();
- void DF_MQTT(String mind_s_WIFI, String mind_s_PWD, float mind_n_N);
- void DF_RUN(String mind_s_TZhiLing, float mind_n_NSuDu);
- void DF_XunXian(float mind_n_NSuDu);
- void onIRReceive(uint8_t data);
- void DF_MQTTBanBen(float mind_n_N);
- void DF_HongWaiZhuanMa(float mind_n_NBianMa, float mind_n_NSuDu);
- void obloqMqttEventTrxy91(String& message);
- // 静态常量
- const String topics[5] = {"37ck/419","","","",""};
- const MsgHandleCb msgHandles[5] = {obloqMqttEventTrxy91,NULL,NULL,NULL,NULL};
- // 创建对象
- DFRobot_Iot myIot;
- Maqueen_Motor motor_mq;
- IRremote_Receive remoteReceive_P16;
- // 主程序开始
- void setup() {
- mPython.begin();
- myIot.setMqttCallback(msgHandles);
- buttonA.setPressedCallback(onButtonAPressed);
- buttonB.setPressedCallback(onButtonBPressed);
- remoteReceive_P16.begin(P16);
- remoteReceive_P16.setCallback(onIRReceive);
- DF_MQTT("37ck", "20200808", 1);
- }
- void loop() {
- }
- // 自定义函数
- void DF_MQTT(String mind_s_WIFI, String mind_s_PWD, float mind_n_N) {
- display.setCursorLine(1);
- display.printLine("WIFI...");
- myIot.wifiConnect(mind_s_WIFI, mind_s_PWD);
- while (!myIot.wifiStatus()) {yield();}
- display.setCursorLine(1);
- display.printLine(myIot.getWiFiLocalIP());
- display.setCursorLine(2);
- display.printLine(myIot.getDNS());
- DF_MQTTBanBen(mind_n_N);
- }
- void DF_RUN(String mind_s_TZhiLing, float mind_n_NSuDu) {
- if (((String(mind_s_TZhiLing).indexOf(String("前进")) != -1))) {
- motor_mq.motorRun(motor_mq.LEFT,motor_mq.CW,mind_n_NSuDu);
- motor_mq.motorRun(motor_mq.RIGHT,motor_mq.CW,mind_n_NSuDu);
- }
- else if (((String(mind_s_TZhiLing).indexOf(String("右转")) != -1))) {
- motor_mq.motorRun(motor_mq.LEFT,motor_mq.CW,mind_n_NSuDu);
- motor_mq.motorRun(motor_mq.RIGHT,motor_mq.CW,0);
- }
- else if (((String(mind_s_TZhiLing).indexOf(String("左转")) != -1))) {
- motor_mq.motorRun(motor_mq.LEFT,motor_mq.CW,0);
- motor_mq.motorRun(motor_mq.RIGHT,motor_mq.CW,mind_n_NSuDu);
- }
- else if (((String(mind_s_TZhiLing).indexOf(String("后退")) != -1))) {
- motor_mq.motorRun(motor_mq.LEFT,motor_mq.CCW,mind_n_NSuDu);
- motor_mq.motorRun(motor_mq.RIGHT,motor_mq.CCW,mind_n_NSuDu);
- }
- else if (((String(mind_s_TZhiLing).indexOf(String("开灯")) != -1))) {
- rgb.write(-1, 0x0000FF);
- }
- else if (((String(mind_s_TZhiLing).indexOf(String("关灯")) != -1))) {
- rgb.write(-1, 0x000000);
- }
- else {
- motor_mq.motorRun(motor_mq.LEFT,motor_mq.CW,0);
- motor_mq.motorRun(motor_mq.RIGHT,motor_mq.CW,0);
- }
- }
- void DF_XunXian(float mind_n_NSuDu) {
- mind_n_L = digitalRead(P13);
- mind_n_R = digitalRead(P14);
- display.setCursorLine(2);
- display.printLine(mind_n_L);
- display.setCursorLine(3);
- display.printLine(mind_n_R);
- if (((mind_n_L==0) && (mind_n_R==0))) {
- DF_RUN("前进", mind_n_NSuDu);
- }
- else if (((mind_n_L==1) && (mind_n_R==0))) {
- DF_RUN("右转", mind_n_NSuDu);
- }
- else if (((mind_n_L==0) && (mind_n_R==1))) {
- DF_RUN("左转", mind_n_NSuDu);
- }
- else if (((mind_n_L==1) && (mind_n_R==1))) {
- DF_RUN("后退", mind_n_NSuDu);
- }
- else {
- }
- }
- void DF_MQTTBanBen(float mind_n_N) {
- display.setCursorLine(3);
- display.printLine(mind_n_N);
- if ((mind_n_N==2)) {
- myIot.init("i.37ck.cn","37ck","7344603147218602","dfrobot", topics, 1883);
- }
- else {
- myIot.init("i.37ck.cn","37ck","40792077618307276","20200808", topics, 1884);
- }
- myIot.connect();
- display.setCursorLine(1);
- display.printLine("MQTT...");
- while (!myIot.connected()) {yield();}
- display.setCursorLine(1);
- display.printLine("MQTT OK");
- display.setCursorLine(4);
- display.printLine("版本切换完成");
- }
- void DF_HongWaiZhuanMa(float mind_n_NBianMa, float mind_n_NSuDu) {
- if ((mind_n_NBianMa==47)) {
- DF_RUN("前进", mind_n_NSuDu);
- }
- else if ((mind_n_NBianMa==119)) {
- DF_RUN("右转", mind_n_NSuDu);
- }
- else if ((mind_n_NBianMa==247)) {
- DF_RUN("左转", mind_n_NSuDu);
- }
- else if ((mind_n_NBianMa==143)) {
- DF_RUN("后退", mind_n_NSuDu);
- }
- else if ((mind_n_NBianMa==79)) {
- DF_RUN("停止", 1);
- }
- }
- // 事件回调函数
- void onButtonAPressed() {
- myIot.publish(topic_0, "开灯", 1);
- delay(2000);
- myIot.publish(topic_0, "关灯", 1);
- }
- void onButtonBPressed() {
- myIot.publish(topic_0, "开灯");
- delay(2000);
- myIot.publish(topic_0, "关灯");
- }
- void onIRReceive(uint8_t data) {
- display.setCursorLine(2);
- display.printLine(data);
- DF_HongWaiZhuanMa(data, 160);
- }
- void obloqMqttEventTrxy91(String& message) {
- display.setCursorLine(4);
- display.printLine(message);
- if (((String(message).indexOf(String("MQTT1")) != -1))) {
- DF_MQTT("37ck", "20200808", 1);
- }
- else if (((String(message).indexOf(String("MQTT2")) != -1))) {
- DF_MQTT("37ck", "20200808", 2);
- }
- else if (((String(message).indexOf(String("巡线")) != -1))) {
- DF_XunXian(150);
- }
- else {
- DF_RUN(message, 160);
- }
- }
复制代码 |
|