質問編集履歴
1
誤字
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
esp 8266 wroom 02
|
1
|
+
esp 8266 wroom 02 Arduino 上で MQTT を実施する際バッファ最大値を超え LmacRxBlk:1 というエラーが頻発する
|
test
CHANGED
@@ -40,208 +40,210 @@
|
|
40
40
|
|
41
41
|
``````ここに言語を入力
|
42
42
|
|
43
|
+
#include <MQTT.h>
|
44
|
+
|
45
|
+
#include <PubSubClient.h>
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
#include <ESP8266WiFi.h>
|
50
|
+
|
51
|
+
#include <PubSubClient.h>
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
#define DF_LED_12 12 // LED
|
56
|
+
|
57
|
+
#define DF_LED_14 14 // LED
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
// アクセスポイント接続情報
|
62
|
+
|
63
|
+
const char *wifi_ssid = "xxxxx"; // SSID
|
64
|
+
|
65
|
+
const char *wifi_pass = "xxxxx"; // パスワード
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
// MQTT接続情報
|
70
|
+
|
71
|
+
const char *mqtt_host = "xxxxx"; // 接続先のホスト名 2あるのでどちらか
|
72
|
+
|
73
|
+
const char *mqtt_user = "xxxxx"; // ユーザー名
|
74
|
+
|
75
|
+
const char *mqtt_pass = "xxxxx"; // パスワード
|
76
|
+
|
77
|
+
const char *mqtt_topic = "xxxxx/office/tokyo/3F/temp"; // アクセス先トピック
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
const char *mqtt_client = "xxxxx";
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
int count = 0;
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
WiFiClient wclient;
|
90
|
+
|
91
|
+
PubSubClient client(wclient, mqtt_host);
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
#define BUFFER_SIZE 100
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
void callback(const MQTT::Publish& pub) {
|
100
|
+
|
101
|
+
// Serial.print(pub.topic());
|
102
|
+
|
103
|
+
// Serial.print(" => ");
|
104
|
+
|
105
|
+
if (pub.has_stream()) {
|
106
|
+
|
107
|
+
uint8_t buf[BUFFER_SIZE];
|
108
|
+
|
109
|
+
int read;
|
110
|
+
|
111
|
+
while (read = pub.payload_stream() ->read(buf, BUFFER_SIZE)){
|
112
|
+
|
113
|
+
Serial.write(buf, read);
|
114
|
+
|
115
|
+
}
|
116
|
+
|
117
|
+
pub.payload_stream() ->stop();
|
118
|
+
|
119
|
+
Serial.println("");
|
120
|
+
|
121
|
+
} else
|
122
|
+
|
123
|
+
Serial.println(pub.payload_string());
|
124
|
+
|
125
|
+
}
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
void setup() {
|
130
|
+
|
131
|
+
Serial.begin(115200);
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
pinMode(DF_LED_12, OUTPUT); // LED
|
136
|
+
|
137
|
+
pinMode(DF_LED_14, OUTPUT); // LED
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
delay(10);
|
142
|
+
|
143
|
+
Serial.println();
|
144
|
+
|
145
|
+
Serial.println();
|
146
|
+
|
147
|
+
}
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
void loop() {
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
if (WiFi.status() != WL_CONNECTED) {
|
156
|
+
|
157
|
+
Serial.print("Connecting to ");
|
158
|
+
|
159
|
+
Serial.print(wifi_ssid);
|
160
|
+
|
161
|
+
Serial.println("...");
|
162
|
+
|
163
|
+
WiFi.begin(wifi_ssid, wifi_pass);
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
|
168
|
+
|
169
|
+
return;
|
170
|
+
|
171
|
+
}
|
172
|
+
|
173
|
+
Serial.println("WiFi connected");
|
174
|
+
|
175
|
+
}
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
//MQTTサーバーへ接続する
|
180
|
+
|
181
|
+
if (WiFi.status() == WL_CONNECTED) {
|
182
|
+
|
183
|
+
if (!client.connected()) {
|
184
|
+
|
185
|
+
Serial.println("Connecting to MQTT server");
|
186
|
+
|
187
|
+
if (client.connect(MQTT::Connect(mqtt_client).set_auth(mqtt_user, mqtt_pass))) {
|
188
|
+
|
189
|
+
Serial.println("Connected to MQTT server");
|
190
|
+
|
191
|
+
client.set_callback(callback);
|
192
|
+
|
193
|
+
client.subscribe(mqtt_topic);
|
194
|
+
|
195
|
+
|
196
|
+
|
43
|
-
|
197
|
+
} else {
|
198
|
+
|
199
|
+
Serial.println("Could not connect to MQTT server");
|
200
|
+
|
201
|
+
}
|
202
|
+
|
203
|
+
}
|
204
|
+
|
205
|
+
Serial.print("---------->>>>>> Publishing: ");
|
206
|
+
|
207
|
+
String message = String("count ") + count;
|
208
|
+
|
209
|
+
Serial.println(message);
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
client.publish(mqtt_topic, message);
|
214
|
+
|
215
|
+
count++;
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
digitalWrite(DF_LED_12, HIGH);
|
220
|
+
|
221
|
+
delay(200);
|
222
|
+
|
223
|
+
digitalWrite(DF_LED_12, LOW);
|
224
|
+
|
225
|
+
delay(100);
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
if (client.connected()) {
|
230
|
+
|
231
|
+
client.loop();
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
}
|
236
|
+
|
237
|
+
}
|
238
|
+
|
239
|
+
delay(10000);
|
240
|
+
|
241
|
+
}
|
44
242
|
|
45
243
|
```
|
46
244
|
|
47
|
-
|
245
|
+
|
48
|
-
|
49
|
-
|
246
|
+
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
247
|
+
|
54
|
-
|
55
|
-
#include <PubSubClient.h>
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
#define DF_LED_12 12 // LED
|
60
|
-
|
61
|
-
#define DF_LED_14 14 // LED
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
// アクセスポイント接続情報
|
66
|
-
|
67
|
-
const char *wifi_ssid = "xxxxx"; // SSID
|
68
|
-
|
69
|
-
const char *wifi_pass = "xxxxx"; // パスワード
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
// MQTT接続情報
|
74
|
-
|
75
|
-
const char *mqtt_host = "xxxxx"; // 接続先のホスト名 2あるのでどちらか
|
76
|
-
|
77
|
-
const char *mqtt_user = "xxxxx"; // ユーザー名
|
78
|
-
|
79
|
-
const char *mqtt_pass = "xxxxx"; // パスワード
|
80
|
-
|
81
|
-
const char *mqtt_topic = "xxxxx/office/tokyo/3F/temp"; // アクセス先トピック
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
const char *mqtt_client = "xxxxx";
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
int count = 0;
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
WiFiClient wclient;
|
94
|
-
|
95
|
-
PubSubClient client(wclient, mqtt_host);
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
#define BUFFER_SIZE 100
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
void callback(const MQTT::Publish& pub) {
|
104
|
-
|
105
|
-
// Serial.print(pub.topic());
|
106
|
-
|
107
|
-
// Serial.print(" => ");
|
108
|
-
|
109
|
-
if (pub.has_stream()) {
|
110
|
-
|
111
|
-
uint8_t buf[BUFFER_SIZE];
|
112
|
-
|
113
|
-
int read;
|
114
|
-
|
115
|
-
while (read = pub.payload_stream() ->read(buf, BUFFER_SIZE)){
|
116
|
-
|
117
|
-
Serial.write(buf, read);
|
118
|
-
|
119
|
-
}
|
120
|
-
|
121
|
-
pub.payload_stream() ->stop();
|
122
|
-
|
123
|
-
Serial.println("");
|
124
|
-
|
125
|
-
} else
|
126
|
-
|
127
|
-
Serial.println(pub.payload_string());
|
128
|
-
|
129
|
-
}
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
void setup() {
|
134
|
-
|
135
|
-
Serial.begin(115200);
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
pinMode(DF_LED_12, OUTPUT); // LED
|
140
|
-
|
141
|
-
pinMode(DF_LED_14, OUTPUT); // LED
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
delay(10);
|
146
|
-
|
147
|
-
Serial.println();
|
148
|
-
|
149
|
-
Serial.println();
|
150
|
-
|
151
|
-
}
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
void loop() {
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
if (WiFi.status() != WL_CONNECTED) {
|
160
|
-
|
161
|
-
Serial.print("Connecting to ");
|
162
|
-
|
163
|
-
Serial.print(wifi_ssid);
|
164
|
-
|
165
|
-
Serial.println("...");
|
166
|
-
|
167
|
-
WiFi.begin(wifi_ssid, wifi_pass);
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
|
172
|
-
|
173
|
-
return;
|
174
|
-
|
175
|
-
}
|
176
|
-
|
177
|
-
Serial.println("WiFi connected");
|
178
|
-
|
179
|
-
}
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
//MQTTサーバーへ接続する
|
184
|
-
|
185
|
-
if (WiFi.status() == WL_CONNECTED) {
|
186
|
-
|
187
|
-
if (!client.connected()) {
|
188
|
-
|
189
|
-
Serial.println("Connecting to MQTT server");
|
190
|
-
|
191
|
-
if (client.connect(MQTT::Connect(mqtt_client).set_auth(mqtt_user, mqtt_pass))) {
|
192
|
-
|
193
|
-
Serial.println("Connected to MQTT server");
|
194
|
-
|
195
|
-
client.set_callback(callback);
|
196
|
-
|
197
|
-
client.subscribe(mqtt_topic);
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
} else {
|
202
|
-
|
203
|
-
Serial.println("Could not connect to MQTT server");
|
204
|
-
|
205
|
-
}
|
206
|
-
|
207
|
-
}
|
208
|
-
|
209
|
-
Serial.print("---------->>>>>> Publishing: ");
|
210
|
-
|
211
|
-
String message = String("count ") + count;
|
212
|
-
|
213
|
-
Serial.println(message);
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
client.publish(mqtt_topic, message);
|
218
|
-
|
219
|
-
count++;
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
digitalWrite(DF_LED_12, HIGH);
|
224
|
-
|
225
|
-
delay(2000);
|
226
|
-
|
227
|
-
digitalWrite(DF_LED_12, LOW);
|
228
|
-
|
229
|
-
delay(100);
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
if (client.connected()) {
|
234
|
-
|
235
|
-
client.loop();
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
}
|
240
|
-
|
241
|
-
}
|
242
|
-
|
243
|
-
delay(10000);
|
244
|
-
|
245
|
-
}
|
246
248
|
|
247
249
|
```
|