-
Notifications
You must be signed in to change notification settings - Fork 0
/
3d-printer-mqtt.ino
80 lines (64 loc) · 1.66 KB
/
3d-printer-mqtt.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ESP8266httpUpdate.h>
#include "SimpleTimer.h"
const char* ssid = "********";
const char* password = "********";
const char* mqtt_server = "********";
const String chipId = String(ESP.getChipId());
const String TOPIC_ENDER3_RAW = "ender3/raw";
SimpleTimer timer;
WiFiClient espClient;
PubSubClient mqtt_client(espClient);
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
delay(10);
// We start by connecting to a WiFi network
Serial.print("M117 Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
Serial.print("M117 IP: ");
Serial.println(WiFi.localIP());
mqtt_client.setServer(mqtt_server, 1883);
timer.setInterval(2000, handleMQTT);
timer.setInterval(15500, updatePrintStatus);
}
void handleMQTT() {
if (!mqtt_client.connected()) {
reconnect();
}
mqtt_client.loop();
}
void updatePrintStatus() {
Serial.println("M31");
Serial.println("M27");
}
void reconnect() {
int tries = 0;
// Loop until we're reconnected
while (!mqtt_client.connected() and tries < 1) {
tries = tries + 1;
Serial.println("M117 MQTT connecting...");
// Attempt to connect
if (mqtt_client.connect(chipId.c_str())) {
Serial.println("M117 MQTT connected");
mqtt_client.loop();
} else {
Serial.print("M117 failed, rc=");
Serial.print(mqtt_client.state());
delay(5000);
}
}
}
String raw;
void loop() {
while(Serial.available()) {
raw = Serial.readString();
mqtt_client.publish(TOPIC_ENDER3_RAW.c_str(), raw.c_str());
}
timer.run();
}