|
| 1 | +// 登录控制台与查看文档请访问官网:http://www.miniiot.top |
| 2 | + |
| 3 | +// 当前程序版本(仅用于后台展示) |
| 4 | +#define APP_VERSION "2504231612" |
| 5 | + |
| 6 | +// 默认WIFI配置(出厂设置) |
| 7 | +#define DEFAULT_WIFI_SSID "Tenda_375160章" |
| 8 | +#define DEFAULT_WIFI_PASSWORD "87472998" |
| 9 | + |
| 10 | +// 使用ip测试地址(预留私有部署与调试使用,不必理会) |
| 11 | +// #define MiniIot_MQTT_HOST_IS_IP |
| 12 | +// #define MiniIot_MQTT_HOST "192.168.0.192" |
| 13 | +// #define MiniIot_HTTP_HOST "192.168.0.192" |
| 14 | + |
| 15 | +// 后台服务(用于上位机调试,开发测试中,建议关闭) |
| 16 | +// #define MiniIot_Admin_Service |
| 17 | + |
| 18 | +// 打印日志(建议调试完成后注释掉) |
| 19 | +#define MiniIot_DEBUG_LOG |
| 20 | + |
| 21 | +// 导入miniiot |
| 22 | +#include <MiniIot.h> |
| 23 | +// 定时任务(与miniiot无关,用于实现长按恢复出厂设置) |
| 24 | +#include <Ticker.h> |
| 25 | + |
| 26 | +// 复用Boot按键用于恢复出厂设置 |
| 27 | +#ifdef ESP8266 |
| 28 | + #define SYS_RST_IO 0 |
| 29 | +#endif |
| 30 | + |
| 31 | +#ifdef ESP32 |
| 32 | + #define SYS_RST_IO 9 |
| 33 | +#endif |
| 34 | + |
| 35 | +// 恢复出厂设置计数(秒) |
| 36 | +int sys_rst_count = 0; |
| 37 | + |
| 38 | +int num = 0; |
| 39 | + |
| 40 | +Ticker TickerRST; |
| 41 | + |
| 42 | + |
| 43 | +// 按5秒开发板上的按钮恢复出厂设置 |
| 44 | +void SysRstfun() |
| 45 | +{ |
| 46 | + if (digitalRead(SYS_RST_IO) == LOW) |
| 47 | + { |
| 48 | + sys_rst_count++; |
| 49 | + } |
| 50 | + else |
| 51 | + { |
| 52 | + sys_rst_count = 0; |
| 53 | + } |
| 54 | + if (sys_rst_count >= 5) |
| 55 | + { |
| 56 | + // 格式化存储 |
| 57 | + MiniIot.RESET(); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | +void addNum(){ |
| 64 | + // 上报属性 |
| 65 | + MiniIot.propertyPost("num_1", num); |
| 66 | + num++; |
| 67 | +} |
| 68 | + |
| 69 | +// 业务回调函数 |
| 70 | +void ServiceCallbackFunction(JsonObject dataObj) |
| 71 | +{ |
| 72 | + // 所有参数默认都是以String类型返回,需要自行转换成目标类型 |
| 73 | + // json库使用ArduinoJson |
| 74 | + String serviceName = dataObj["serviceName"].as<String>(); |
| 75 | + |
| 76 | + // Ping |
| 77 | + if(serviceName == "miniiot_ping"){ |
| 78 | + addNum(); |
| 79 | + } |
| 80 | + |
| 81 | + // 测试服务(解析参数) |
| 82 | + if(serviceName == "test"){ |
| 83 | + Serial.print("参数a1:");Serial.println(dataObj["serviceParams"]["a1"].as<String>()); |
| 84 | + Serial.print("参数a2:");Serial.println(dataObj["serviceParams"]["a2"].as<String>()); |
| 85 | + Serial.print("参数a3:");Serial.println(dataObj["serviceParams"]["a3"].as<String>()); |
| 86 | + Serial.print("参数a4:");Serial.println(dataObj["serviceParams"]["a4"].as<String>()); |
| 87 | + Serial.print("参数a5:");Serial.println(dataObj["serviceParams"]["a5"].as<String>()); |
| 88 | + } |
| 89 | + |
| 90 | + Serial.printf("剩余内存: %dB\n", ESP.getFreeHeap()); |
| 91 | +} |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | +void setup() |
| 96 | +{ |
| 97 | + Serial.begin(115200); |
| 98 | + |
| 99 | + pinMode(SYS_RST_IO, INPUT_PULLUP); |
| 100 | + |
| 101 | + // 绑定定时任务回调函数 |
| 102 | + TickerRST.attach(1, SysRstfun); |
| 103 | + |
| 104 | + // 使用产品密钥初始化MiniIot,(产品ID,产品密钥) |
| 105 | + // 设备不存在会自动注册添加设备 |
| 106 | + MiniIot.begin("ZnnQmFTH", "h5Tmn1l3m2S9DY2I"); |
| 107 | + |
| 108 | + // 使用设备密钥初始化MiniIot,(产品ID,设备ID,设备密钥) |
| 109 | + // 必须现在平台上添加设备 |
| 110 | + // MiniIot.begin("ZnnQmFTH", "Ab79b2a5a3", "h5Tmn1l3m2S9DY2I"); |
| 111 | + |
| 112 | + // 绑定回调函数 |
| 113 | + MiniIot.attach(ServiceCallbackFunction); |
| 114 | + |
| 115 | + Serial.printf("剩余内存: %dB\n", ESP.getFreeHeap()); |
| 116 | + Serial.print("cpu运行频率:");Serial.println(ESP.getCpuFreqMHz()); |
| 117 | + Serial.print("当前固件大小:");Serial.println(ESP.getSketchSize()); |
| 118 | + Serial.print("剩余固件空间:");Serial.println(ESP.getFreeSketchSpace()); |
| 119 | + |
| 120 | +} |
| 121 | + |
| 122 | +void loop() |
| 123 | +{ |
| 124 | + MiniIot.loop(); |
| 125 | + |
| 126 | + // 服务器连接成功 |
| 127 | + if(MiniIot.running()){ |
| 128 | + // 不能直接使用delay延时 |
| 129 | + MiniIot.delay(3000); |
| 130 | + |
| 131 | + Serial.printf("剩余内存: %dB\n", ESP.getFreeHeap()); |
| 132 | + } |
| 133 | +} |
0 commit comments