ESP3D  3.0
Firmware for ESP boards connected to 3D Printer
ESP401.cpp
Go to the documentation of this file.
1 /*
2  ESP401.cpp - ESP3D command class
3 
4  Copyright (c) 2014 Luc Lebosse. All rights reserved.
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either
9  version 2.1 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public
17  License along with this library; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 #include "../../include/esp3d_config.h"
21 #include "../commands.h"
22 #include "../esp3doutput.h"
23 #include "../settings_esp3d.h"
24 #include "../../modules/authentication/authentication_service.h"
25 #ifdef CAMERA_DEVICE
26 #include "../../modules/camera/camera.h"
27 #endif //CAMERA_DEVICE
28 #ifdef DHT_DEVICE
29 #include "../../modules/dht/dht.h"
30 #endif //DHT_DEVICE
31 #ifdef BUZZER_DEVICE
32 #include "../../modules/buzzer/buzzer.h"
33 #endif //BUZZER_DEVICE
34 #ifdef TIMESTAMP_FEATURE
35 #include "../../modules/time/time_server.h"
36 #endif //TIMESTAMP_FEATURE
37 #ifdef NOTIFICATION_FEATURE
38 #include "../../modules/notifications/notifications_service.h"
39 #endif //NOTIFICATION_FEATURE
40 #ifdef SD_DEVICE
41 #include "../../modules/filesystem/esp_sd.h"
42 #endif //SD_DEVICE
43 //Set EEPROM setting
44 //[ESP401]P=<position> T=<type> V=<value> pwd=<user/admin password>
45 bool Commands::ESP401(const char* cmd_params, level_authenticate_type auth_type, ESP3DOutput * output)
46 {
47  bool response = true;
48  String parameter;
49 #ifdef AUTHENTICATION_FEATURE
50  if (auth_type != LEVEL_ADMIN) {
51  output->printERROR("Wrong authentication!", 401);
52  return false;
53  }
54 #else
55  (void)auth_type;
56 #endif //AUTHENTICATION_FEATURE
57  //check validity of parameters
58  String spos = get_param (cmd_params, "P=");
59  String styp = get_param (cmd_params, "T=");
60  String sval = get_param (cmd_params, "V=");
61  if (spos.length() == 0) {
62  response = false;
63  }
64  if (! (styp == "B" || styp == "S" || styp == "A" || styp == "I" || styp == "F") ) {
65  response = false;
66  }
67 
68  if (response) {
69  //Byte value
70  if ((styp == "B") || (styp == "F")) {
71  if (!Settings_ESP3D::write_byte (spos.toInt(), sval.toInt())) {
72  response = false;
73  } else {
74  //dynamique refresh is better than restart the boards
75  switch(spos.toInt()) {
76  case ESP_TARGET_FW:
78  break;
79 #ifdef SD_DEVICE
80  case ESP_SD_DEVICE_TYPE:
82  break;
83  case ESP_SD_SPEED_DIV:
84  ESP_SD::setSPISpeedDivider(sval.toInt());
85  break;
86 #endif //SD_DEVICE
87 #ifdef TIMESTAMP_FEATURE
88  case ESP_INTERNET_TIME:
89  timeserver.begin();
90  break;
91 #endif //TIMESTAMP_FEATURE
92 #ifdef NOTIFICATION_FEATURE
94  notificationsservice.setAutonotification((sval.toInt() == 0)?false:true);
95  break;
96 #endif //NOTIFICATION_FEATURE
97 #ifdef DHT_DEVICE
98  case ESP_DHT_TYPE:
99  esp3d_DHT.begin();
100  break;
101 #endif //DHT_DEVICE
102 #ifdef BUZZER_DEVICE
103  case ESP_BUZZER:
104  if (sval.toInt() == 1) {
106  } else if (sval.toInt() == 0) {
107  esp3d_buzzer.end();
108  }
109  break;
110 #endif //BUZZER_DEVICE
111  default:
112  break;
113  }
114  }
115  }
116  //Integer value
117  if (styp == "I") {
118  if (!Settings_ESP3D::write_uint32 (spos.toInt(), sval.toInt())) {
119  response = false;
120  } else {
121  //dynamique refresh is better than restart the board
122  switch(spos.toInt()) {
123 #ifdef DHT_DEVICE
124  case ESP_DHT_INTERVAL:
125  esp3d_DHT.setInterval(sval.toInt());
126  break;
127 #endif //DHT_DEVICE
128 #ifdef CAMERA_DEVICE
129  case ESP_CAMERA_PORT:
130  //esp3d_camera.begin();
131  break;
132 #endif //CAMERA_DEVICE
133  default:
134  break;
135  }
136  }
137  }
138  //String value
139  if (styp == "S") {
140  if (!Settings_ESP3D::write_string (spos.toInt(), sval.c_str())) {
141  response = false;
142  } else {
143  //dynamique refresh is better than restart the board
144  switch(spos.toInt()) {
145 #ifdef AUTHENTICATION_FEATURE
146  case ESP_ADMIN_PWD:
147  case ESP_USER_PWD:
148  AuthenticationService::update();
149  break;
150 #endif //AUTHENTICATION_FEATURE
151  default:
152  break;
153  }
154  }
155  }
156 #if defined (WIFI_FEATURE)
157  //IP address
158  if (styp == "A") {
159  if (!Settings_ESP3D::write_IP_String (spos.toInt(), sval.c_str())) {
160  response = false;
161  } else {
162  //dynamique refresh is better than restart the board
163  //TBD
164  }
165  }
166 #endif //WIFI_FEATURE
167  }
168  if (!response) {
169  output->printERROR ("Incorrect command!");
170  } else {
171  output->printMSG("ok");
172  }
173 
174  return response;
175 }
Settings_ESP3D::write_byte
static bool write_byte(int pos, const uint8_t value)
Definition: settings_esp3d.cpp:749
ESP_DHT_INTERVAL
#define ESP_DHT_INTERVAL
Definition: settings_esp3d.h:56
DHT::setInterval
bool setInterval(uint interval)
Commands::get_param
const char * get_param(const char *cmd_params, const char *label)
Definition: commands.cpp:162
ESP_DHT_TYPE
#define ESP_DHT_TYPE
Definition: settings_esp3d.h:71
notificationsservice
NotificationsService notificationsservice
Definition: notifications_service.cpp:67
NotificationsService::setAutonotification
void setAutonotification(bool value)
Definition: notifications_service.h:42
ESP_USER_PWD
#define ESP_USER_PWD
Definition: settings_esp3d.h:59
Commands::ESP401
bool ESP401(const char *cmd_params, level_authenticate_type auth_level, ESP3DOutput *output)
Definition: ESP401.cpp:45
ESP_TARGET_FW
#define ESP_TARGET_FW
Definition: settings_esp3d.h:72
esp3d_DHT
DHT esp3d_DHT
ESP_CAMERA_PORT
#define ESP_CAMERA_PORT
Definition: settings_esp3d.h:90
Settings_ESP3D::GetSDDevice
static uint8_t GetSDDevice(bool fromsettings=false)
Definition: settings_esp3d.cpp:191
ESP_AUTO_NOTIFICATION
#define ESP_AUTO_NOTIFICATION
Definition: settings_esp3d.h:89
Settings_ESP3D::write_uint32
static bool write_uint32(int pos, const uint32_t value)
Definition: settings_esp3d.cpp:970
ESP_INTERNET_TIME
#define ESP_INTERNET_TIME
Definition: settings_esp3d.h:51
Settings_ESP3D::GetFirmwareTarget
static uint8_t GetFirmwareTarget(bool fromsettings=false)
Definition: settings_esp3d.cpp:183
DHT::begin
bool begin()
timeserver
TimeServer timeserver
ESP_SD_SPEED_DIV
#define ESP_SD_SPEED_DIV
Definition: settings_esp3d.h:68
BuzzerDevice::end
void end()
level_authenticate_type
level_authenticate_type
Definition: authentication_service.h:25
ESP_BUZZER
#define ESP_BUZZER
Definition: settings_esp3d.h:50
ESP3DOutput::printMSG
size_t printMSG(const char *s, bool withNL=true)
Definition: esp3doutput.cpp:190
TimeServer::begin
bool begin()
ESP3DOutput::printERROR
size_t printERROR(const char *s, int code_error=200)
Definition: esp3doutput.cpp:247
Settings_ESP3D::write_string
static bool write_string(int pos, const char *byte_buffer)
Definition: settings_esp3d.cpp:870
BuzzerDevice::begin
bool begin()
Settings_ESP3D::write_IP_String
static bool write_IP_String(int pos, const char *value)
Definition: settings_esp3d.cpp:1202
LEVEL_ADMIN
@ LEVEL_ADMIN
Definition: authentication_service.h:28
ESP_SD_DEVICE_TYPE
#define ESP_SD_DEVICE_TYPE
Definition: settings_esp3d.h:78
ESP_ADMIN_PWD
#define ESP_ADMIN_PWD
Definition: settings_esp3d.h:58
esp3d_buzzer
BuzzerDevice esp3d_buzzer
ESP3DOutput
Definition: esp3doutput.h:48
ESP_SD::setSPISpeedDivider
static bool setSPISpeedDivider(uint8_t speeddivider)
Definition: esp_sd.cpp:202