forked from bcjmlegacy/ESP8266-MQTT-Thermal-Printer
-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.ino
200 lines (171 loc) · 5.06 KB
/
main.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include "config.h"
#include "Adafruit_Thermal.h"
#include "SoftwareSerial.h"
#define RX_PIN 5 // RX GPIO-Pin of your Microcontroller
#define TX_PIN 4 // TX GPIO-Pin of your Microcontroller
#define MAX_TOPIC_LENGTH 50
#define MAX_PAYLOAD_LENGTH 10
char mqtt_id[24];
char mqtt_topic[50];
uint32_t lastTimeItHappened = millis() + papercheck_milliseconds;
WiFiClient client;
PubSubClient mqtt(client);
SoftwareSerial mySerial(RX_PIN, TX_PIN);
Adafruit_Thermal printer(&mySerial);
void callback(char* topic, byte* payload, unsigned int length) {
// set textlineheight
if (strcmp(topic,mqtt_listen_topic_textlineheight)==0){
//this topic expects integer!
int payload_int = mqtt_row_spacing;
for (int i=0;i<length;i++) {
char c = payload[i];
if (c >= '0' && c <= '9')
payload_int = payload_int*10 + c - '0'; //encode to interger
}
printer.setLineHeight(payload_int);
}
// set text size (S | M | L)
if (strcmp(topic,mqtt_listen_topic_textsize)==0){
char c = 'S';
for (int i=0;i<length;i++) {
c = payload[i];
}
printer.setSize(c);
}
// topic to inverse the text (0 | 1)
if (strcmp(topic,mqtt_listen_topic_textinverse)==0){
char c = '0';
for (int i=0;i<length;i++) {
c = payload[i];
}
if (c == '1') {
printer.inverseOn();
} else {
printer.inverseOff();
}
}
// topic to justify the text (L | C | R)
if (strcmp(topic,mqtt_listen_topic_textjustify)==0){
char c = 'L';
for (int i=0;i<length;i++) {
c = payload[i];
}
printer.justify(c);
}
// topic to bold the text (0 | 1)
if (strcmp(topic,mqtt_listen_topic_textbold)==0){
char c = '0';
for (int i=0;i<length;i++) {
c = payload[i];
}
if (c == '1') {
printer.boldOn();
} else {
printer.boldOff();
}
}
// topic to underline the text (0 | 1)
if (strcmp(topic,mqtt_listen_topic_textunderline)==0){
char c = '0';
for (int i=0;i<length;i++) {
c = payload[i];
}
if (c == '1') {
printer.underlineOn();
} else {
printer.underlineOff();
}
}
// topic to print barcode
if (strcmp(topic,mqtt_listen_topic_barcode)==0){
uint8_t barcode_type = 0;
char barcode_value[255] = ""; // some borcodes allows only 255 digits(!) but not for our 58mm printer ;)
int y = 0;
bool barcodeseperatorfound = false;
for (int i=0;i<length;i++) {
if (!barcodeseperatorfound){
if (payload[i] == '|'){
barcodeseperatorfound = true;
continue;
}
char c = payload[i];
if (c >= '0' && c <= '9') {
barcode_type = barcode_type*10 + c - '0';//encode to interger
}
} else {
barcode_value[y++] = (char)payload[i];
}
}
printer.printBarcode(barcode_value, (uint8_t) barcode_type);
}
// topic to print text
if (strcmp(topic,mqtt_listen_topic_text2print)==0){
printer.print(F("Message arrived:\n"));
for (int i=0;i<length;i++) {
printer.print((char)payload[i]);
}
printer.print(F("\n"));
}
}
void setup() {
mySerial.begin(baud);
printer.begin();
printer.setDefault();
printer.setSize(mqtt_text_size);
printer.setLineHeight(mqtt_row_spacing);
printer.feed(1);
printer.println(F("Printer starting"));
wifi_station_set_hostname(my_id);
printer.print(F("Hostname: "));
printer.println(my_id);
WiFi.mode(WIFI_STA);
mqtt.setServer(mqtt_server, mqtt_port);
mqtt.setCallback(callback);
}
void loop() {
if (WiFi.status() != WL_CONNECTED) {
printer.println(F("Connecting to WiFi..."));
WiFi.begin(ssid, password);
unsigned long begin_started = millis();
while (WiFi.status() != WL_CONNECTED) {
delay(10);
if (millis() - begin_started > 60000) {
ESP.restart();
}
}
printer.println(F("WiFi connected!"));
}
if (!mqtt.connected()) {
if (mqtt.connect(mqtt_id, mqtt_user, mqtt_pass)) {
printer.println(F("MQTT connected"));
printer.feed(1);
mqtt.subscribe(mqtt_listen_topic_text2print);
mqtt.subscribe(mqtt_listen_topic_textsize);
mqtt.subscribe(mqtt_listen_topic_textlineheight);
mqtt.subscribe(mqtt_listen_topic_textinverse);
mqtt.subscribe(mqtt_listen_topic_textjustify);
mqtt.subscribe(mqtt_listen_topic_textbold);
mqtt.subscribe(mqtt_listen_topic_textunderline);
mqtt.subscribe(mqtt_listen_topic_barcode);
} else {
printer.println(F("MQTT connection failed"));
printer.feed(1);
delay(2000);
return;
}
}
//check the paperload
if ((millis() - lastTimeItHappened >= papercheck_milliseconds)){
bool bPaperCheck = printer.hasPaper();
delay(100);
if (bPaperCheck) {
mqtt.publish(mqtt_listen_topic_papercheck, "yes");
} else {
mqtt.publish(mqtt_listen_topic_papercheck, "no");
}
lastTimeItHappened = millis();
}
mqtt.loop();
}