|
1 |
| -#define WIFI_SSID "" |
2 |
| -#define WIFI_PASSWORD "" |
3 |
| -#define MQTT_CLIENT_NAME "DeerEsp-001234354x32" // 多个同名设备连接同一台服务器会导致其他下线,所以起一个唯一的名字吧 |
4 |
| -#define MQTT_TOPIC "LB2312" // 这里填PushDeer的Key |
5 |
| -#define MQTT_IP "broker.emqx.io" |
6 |
| -#define MQTT_USER "" |
7 |
| -#define MQTT_PASSWORD "" |
8 |
| -#define MQTT_PORT 1883 |
9 |
| - |
10 |
| - |
11 |
| -// ====== 以下不用修改 =============== |
12 | 1 | #define DOWNLOADED_IMG "/download.jpg"
|
13 | 2 | #define AA_FONT_CUBIC "Cubic1112"
|
14 | 3 |
|
15 |
| - |
| 4 | +#include <WiFiManager.h> |
16 | 5 | #include <EspMQTTClient.h>
|
17 | 6 |
|
18 |
| -EspMQTTClient mclient( |
19 |
| - WIFI_SSID, |
20 |
| - WIFI_PASSWORD, |
21 |
| - MQTT_IP, |
22 |
| - MQTT_USER, |
23 |
| - MQTT_PASSWORD, |
24 |
| - MQTT_CLIENT_NAME, |
25 |
| - MQTT_PORT |
26 |
| -); |
| 7 | +WiFiManager wm; |
| 8 | + |
| 9 | +WiFiManagerParameter mqtt_host_field; |
| 10 | +WiFiManagerParameter mqtt_port_field; |
| 11 | +WiFiManagerParameter mqtt_topic_field; |
| 12 | +WiFiManagerParameter mqtt_client_field; |
| 13 | +WiFiManagerParameter mqtt_user_field; |
| 14 | +WiFiManagerParameter mqtt_password_field; |
| 15 | + |
| 16 | +String mqtt_host_value = ""; |
| 17 | +short mqtt_port_value = 1883; |
| 18 | +String mqtt_client_postfix = ""; |
| 19 | +String mqtt_topic_value = ""; |
| 20 | +String mqtt_user_value = ""; |
| 21 | +String mqtt_client_value = ""; |
| 22 | +String mqtt_password_value = ""; |
| 23 | + |
| 24 | + |
| 25 | +//EspMQTTClient mclient( |
| 26 | +// WIFI_SSID, |
| 27 | +// WIFI_PASSWORD, |
| 28 | +// MQTT_IP, |
| 29 | +// MQTT_USER, |
| 30 | +// MQTT_PASSWORD, |
| 31 | +// MQTT_CLIENT_NAME, |
| 32 | +// MQTT_PORT |
| 33 | +//); |
| 34 | +EspMQTTClient mclient; |
27 | 35 |
|
28 | 36 | // #include "cubic_12.h"
|
29 | 37 | // #include "SPI.h"
|
@@ -69,14 +77,81 @@ void setup() {
|
69 | 77 |
|
70 | 78 | Serial.println("TJpgDec init");
|
71 | 79 |
|
| 80 | + tft.fillScreen( TFT_BLACK ); |
| 81 | + tft.setCursor(0, 0, 1); |
| 82 | + tft.println("Connect to DeerEspWiFi, go 192.168.4.1"); |
| 83 | + |
| 84 | + WiFi.mode(WIFI_STA); |
| 85 | + wm.resetSettings(); |
| 86 | + |
| 87 | + // add a custom input field |
| 88 | + int customFieldLength = 40; |
| 89 | + |
| 90 | + new (&mqtt_host_field) WiFiManagerParameter("mqtt_host", "MQTT IP", "broker.emqx.io", customFieldLength,"placeholder=\"MQTT server IP\""); |
| 91 | + new (&mqtt_port_field) WiFiManagerParameter("mqtt_port", "MQTT Port", "1883", customFieldLength,"placeholder=\"MQTT server port, 1883 as default\""); |
| 92 | + new (&mqtt_topic_field) WiFiManagerParameter("mqtt_topic", "MQTT Topic", "LB2312", customFieldLength,"placeholder=\"MQTT base topic\""); |
| 93 | + new (&mqtt_client_field) WiFiManagerParameter("mqtt_client", "MQTT Client ID", "DeerESP0001", customFieldLength,"placeholder=\"MQTT client id, can be empty\""); |
| 94 | + new (&mqtt_user_field) WiFiManagerParameter("mqtt_user", "MQTT User", "", customFieldLength,"placeholder=\"MQTT user, can be empty\""); |
| 95 | + new (&mqtt_password_field) WiFiManagerParameter("mqtt_password", "MQTT Password", "", customFieldLength,"placeholder=\"MQTT password, can be empty\""); |
| 96 | + |
| 97 | + |
| 98 | + wm.addParameter(&mqtt_host_field); |
| 99 | + wm.addParameter(&mqtt_port_field); |
| 100 | + wm.addParameter(&mqtt_topic_field); |
| 101 | + wm.addParameter(&mqtt_client_field); |
| 102 | + wm.addParameter(&mqtt_user_field); |
| 103 | + wm.addParameter(&mqtt_password_field); |
| 104 | + |
| 105 | + wm.setSaveParamsCallback(saveParamCallback); |
| 106 | + |
| 107 | + bool res; |
| 108 | + res = wm.autoConnect("DeerEspWiFi"); // anonymous ap |
| 109 | + |
| 110 | + if(!res) { |
| 111 | + Serial.println("Failed to connect"); |
| 112 | + // ESP.restart(); |
| 113 | + } |
| 114 | + else { |
| 115 | + //if you get here you have connected to the WiFi |
| 116 | + Serial.println("connected...yeey :)"); |
| 117 | + } |
| 118 | + |
| 119 | + mclient.enableDebuggingMessages(true); |
| 120 | + mclient.setMqttClientName(mqtt_client_value.c_str()); |
| 121 | + mclient.setMqttServer(mqtt_host_value.c_str(), mqtt_user_value.c_str(), mqtt_password_value.c_str(), mqtt_port_value); |
| 122 | + |
| 123 | + |
| 124 | + |
72 | 125 | }
|
73 | 126 |
|
| 127 | +String getParam(String name){ |
| 128 | + //read parameter from server, for customhmtl input |
| 129 | + String value; |
| 130 | + if(wm.server->hasArg(name)) { |
| 131 | + value = wm.server->arg(name); |
| 132 | + } |
| 133 | + return value; |
| 134 | + } |
| 135 | + |
| 136 | + void saveParamCallback(){ |
| 137 | + mqtt_host_value = getParam("mqtt_host"); |
| 138 | + mqtt_user_value = getParam("mqtt_user"); |
| 139 | + mqtt_password_value = getParam("mqtt_password"); |
| 140 | + mqtt_topic_value = getParam("mqtt_topic"); |
| 141 | + mqtt_client_value = getParam("mqtt_client"); |
| 142 | + mqtt_port_value = getParam("mqtt_port").toInt(); |
| 143 | + Serial.println(String(mqtt_port_value)); |
| 144 | + if( mqtt_port_value < 1 ) mqtt_port_value = 1883; |
| 145 | + Serial.println("[CALLBACK] saveParamCallback fired "); |
| 146 | + } |
| 147 | + |
| 148 | + |
74 | 149 | void onConnectionEstablished()
|
75 | 150 | {
|
76 | 151 | Serial.println("connected");
|
77 |
| - tft.setTextColor(0xFFFF,0x0000);tft.setCursor(0, 0, 1);tft.println("Waiting for messages ..."); |
| 152 | + tft.fillScreen(TFT_BLACK);tft.setTextColor(0xFFFF,0x0000);tft.setCursor(0, 0, 1);tft.println("Waiting for messages ..."); |
78 | 153 |
|
79 |
| - mclient.subscribe(String(MQTT_TOPIC)+"_text", [] (const String &payload) |
| 154 | + mclient.subscribe(mqtt_topic_value+"_text", [] (const String &payload) |
80 | 155 | {
|
81 | 156 | Serial.println(payload);
|
82 | 157 |
|
@@ -113,7 +188,7 @@ void onConnectionEstablished()
|
113 | 188 |
|
114 | 189 | });
|
115 | 190 |
|
116 |
| - mclient.subscribe(String(MQTT_TOPIC)+"_bg_url", [] (const String &payload) |
| 191 | + mclient.subscribe(mqtt_topic_value+"_bg_url", [] (const String &payload) |
117 | 192 | {
|
118 | 193 | Serial.println(payload);
|
119 | 194 | bool ret = file_put_contents(payload, DOWNLOADED_IMG);
|
|
0 commit comments