forked from brunnels/ESP3D
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesp3d.ino
209 lines (195 loc) · 6.65 KB
/
esp3d.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
201
202
203
204
205
206
207
208
209
/*
This file is part of ESP3D Firmware for 3D printer.
ESP3D Firmware for 3D printer is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ESP3D Firmware for 3D printer is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this Firmware. If not, see <http://www.gnu.org/licenses/>.
This firmware is using the standard arduino IDE with module to support ESP8266:
https://github.com/esp8266/Arduino from Bootmanager
Latest version of the code and documentation can be found here :
https://github.com/luc-github/ESP3D
Main author: luc lebosse
*/
//be sure correct IDE and settings are used for ESP8266
#ifndef ARDUINO_ARCH_ESP8266
#error Oops! Make sure you have 'ESP8266' compatible board selected from the 'Tools -> Boards' menu.
#endif
#include <EEPROM.h>
#include "config.h"
#include "wifi.h"
#include "bridge.h"
#include "webinterface.h"
#include "command.h"
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#ifdef MDNS_FEATURE
#include <ESP8266mDNS.h>
#endif
#ifdef CAPTIVE_PORTAL_FEATURE
#include <DNSServer.h>
const byte DNS_PORT = 53;
DNSServer dnsServer;
#endif
#ifdef SSDP_FEATURE
#include <ESP8266SSDP.h>
#endif
#ifdef NETBIOS_FEATURE
#include <ESP8266NetBIOS.h>
#endif
void setup()
{
bool breset_config=false;
long baud_rate=0;
web_interface = NULL;
#ifdef TCP_IP_DATA_FEATURE
data_server = NULL;
#endif
// init:
#ifdef DEBUG_ESP3D
Serial.begin(DEFAULT_BAUD_RATE);
delay(2000);
LOG("\r\nDebug Serial set\r\n")
#endif
//WiFi.disconnect();
WiFi.mode(WIFI_OFF);
#ifdef RECOVERY_FEATURE
delay(8000);
//check if reset config is requested
pinMode(RESET_CONFIG_PIN, INPUT);
if (digitalRead(RESET_CONFIG_PIN)==0) {
breset_config=true; //if requested =>reset settings
}
#endif
//check if EEPROM has value
if ( CONFIG::read_buffer(EP_BAUD_RATE, (byte *)&baud_rate , INTEGER_LENGTH)&&CONFIG::read_buffer(EP_WEB_PORT, (byte *)&(wifi_config.iweb_port) , INTEGER_LENGTH)&&CONFIG::read_buffer(EP_DATA_PORT, (byte *)&(wifi_config.idata_port) , INTEGER_LENGTH)) {
//check if baud value is one of allowed ones
if ( ! (baud_rate==9600 || baud_rate==19200 ||baud_rate==38400 ||baud_rate==57600 ||baud_rate==115200 ||baud_rate==230400 ||baud_rate==250000) ) {
LOG("Error for EEPROM baud rate\r\n")
breset_config=true; //baud rate is incorrect =>reset settings
}
if (wifi_config.iweb_port<1 ||wifi_config.iweb_port>65001 || wifi_config.idata_port <1 || wifi_config.idata_port >65001) {
breset_config=true; //out of range =>reset settings
LOG("Error for EEPROM port values\r\n")
}
} else {
breset_config=true; //cannot access to config settings=> reset settings
LOG("Error no EEPROM access\r\n")
}
//reset is requested
if(breset_config) {
//update EEPROM with default settings
Serial.begin(DEFAULT_BAUD_RATE);
Serial.setRxBufferSize(SERIAL_RX_BUFFER_SIZE);
delay(2000);
Serial.println(F("M117 ESP EEPROM reset"));
#ifdef DEBUG_ESP3D
CONFIG::print_config(DEBUG_PIPE);
delay(1000);
#endif
CONFIG::reset_config();
delay(1000);
//put some default value to a void some exception at first start
WiFi.mode(WIFI_AP);
WiFi.setPhyMode(WIFI_PHY_MODE_11G);
CONFIG::esp_restart();
}
#if defined(DEBUG_ESP3D) && defined(DEBUG_OUTPUT_SERIAL)
LOG("\r\n");
delay(500);
Serial.flush();
#endif
//setup serial
Serial.begin(baud_rate);
Serial.setRxBufferSize(SERIAL_RX_BUFFER_SIZE);
delay(1000);
LOG("Serial Set\r\n");
wifi_config.baud_rate=baud_rate;
//Update is done if any so should be Ok
SPIFFS.begin();
//setup wifi according settings
if (!wifi_config.Setup()) {
Serial.println(F("M117 Safe mode 1"));
//try again in AP mode
if (!wifi_config.Setup(true)) {
Serial.println(F("M117 Safe mode 2"));
wifi_config.Safe_Setup();
}
}
delay(1000);
//start web interface
web_interface = new WEBINTERFACE_CLASS(wifi_config.iweb_port);
//here the list of headers to be recorded
const char * headerkeys[] = {"Cookie"} ;
size_t headerkeyssize = sizeof(headerkeys)/sizeof(char*);
//ask server to track these headers
web_interface->WebServer.collectHeaders(headerkeys, headerkeyssize );
#ifdef CAPTIVE_PORTAL_FEATURE
if (WiFi.getMode()!=WIFI_STA ) {
// if DNSServer is started with "*" for domain name, it will reply with
// provided IP to all DNS request
dnsServer.start(DNS_PORT, "*", WiFi.softAPIP());
}
#endif
web_interface->WebServer.begin();
#ifdef TCP_IP_DATA_FEATURE
//start TCP/IP interface
data_server = new WiFiServer (wifi_config.idata_port);
data_server->begin();
data_server->setNoDelay(true);
#endif
#ifdef MDNS_FEATURE
// Check for any mDNS queries and send responses
wifi_config.mdns.addService("http", "tcp", wifi_config.iweb_port);
#endif
#if defined(SSDP_FEATURE) || defined(NETBIOS_FEATURE)
String shost;
if (!CONFIG::read_string(EP_HOSTNAME, shost , MAX_HOSTNAME_LENGTH)) {
shost=wifi_config.get_default_hostname();
}
#endif
#ifdef SSDP_FEATURE
String stmp;
SSDP.setSchemaURL("description.xml");
SSDP.setHTTPPort( wifi_config.iweb_port);
SSDP.setName(shost.c_str());
stmp=String(ESP.getChipId());
SSDP.setSerialNumber(stmp.c_str());
SSDP.setURL("/");
SSDP.setModelName("ESP8266 01");
SSDP.setModelNumber("01");
SSDP.setModelURL("http://espressif.com/en/products/esp8266/");
SSDP.setManufacturer("Espressif Systems");
SSDP.setManufacturerURL("http://espressif.com");
SSDP.setDeviceType("upnp:rootdevice");
SSDP.begin();
#endif
#ifdef NETBIOS_FEATURE
NBNS.begin(shost.c_str());
#endif
LOG("Setup Done\r\n");
}
//main loop
void loop()
{
#ifdef CAPTIVE_PORTAL_FEATURE
if (WiFi.getMode()!=WIFI_STA ) {
dnsServer.processNextRequest();
}
#endif
//web requests
web_interface->WebServer.handleClient();
#ifdef TCP_IP_DATA_FEATURE
BRIDGE::processFromTCP2Serial();
#endif
BRIDGE::processFromSerial2TCP();
if (web_interface->restartmodule) {
CONFIG::esp_restart();
}
}