-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdps5005.ino
176 lines (152 loc) · 4.62 KB
/
dps5005.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
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <FS.h>
#include <cstdlib>
#include "dps.hpp"
#include "settings.h"
//SSID and Password of your WiFi router
const char* ssid = WIFI_SSID;
const char* password = WIFI_PASSWORD;
ESP8266WebServer server(80); //Server on port 80
File fsUploadFile; //holds the current upload
#define LED_PIN 2
const char* status_fmt =
"{\"uset\":%d,"
"\"iset\":%d,"
"\"uout\":%d,"
"\"iout\":%d,"
"\"power\":%d,"
"\"uin\":%d,"
"\"lock\":%d,"
"\"protect\":%d,"
"\"cvcc\":%d,"
"\"onoff\":%d}";
void handleStatus() {
digitalWrite(LED_PIN, LOW);
char buff[256];
dps_status dps;
if (dps_read_status(&dps)) {
sprintf(buff, status_fmt,
dps.uset, dps.iset, dps.uout, dps.iout,
dps.power, dps.uin, dps.lock, dps.protect,
dps.cvcc, dps.onoff);
String data(buff);
server.send(200, "application/json", data);
} else {
server.send(500, "application/json", "{}");
}
}
void handleVoltage() {
digitalWrite(LED_PIN, LOW);
String value = server.arg("v");
if (value.length() > 0) {
int ival = atoi(value.c_str());
if (ival >= MIN_VOLTAGE && ival < MAX_VOLTAGE) {
dps_set_voltage((uint16_t) ival);
server.send(200, "application/json", {});
return;
}
}
server.send(400, "application/json", "{}");
}
void handleCurrent() {
digitalWrite(LED_PIN, LOW);
String value = server.arg("v");
if (value.length() > 0) {
int ival = atoi(value.c_str());
if (ival >= MIN_CURRENT && ival < MAX_CURRENT) {
dps_set_current((uint16_t) ival);
server.send(200, "application/json", {});
return;
}
}
server.send(400, "application/json", "{}");
}
void handleOnOff() {
digitalWrite(LED_PIN, LOW);
String value = server.arg("v");
bool onoff_state = value == "1";
dps_set_onoff(onoff_state);
server.send(200, "application/json", {});
}
String getContentType(String filename){
if(filename.endsWith(".html")) return "text/html";
else if(filename.endsWith(".css")) return "text/css";
else if(filename.endsWith(".js")) return "application/javascript";
else return "text/plain";
}
void handleFiles() {
digitalWrite(LED_PIN, LOW);
String path = server.uri();
if(path.endsWith("/")) path += "index.html";
String contentType = getContentType(path);
if(SPIFFS.exists(path)){
if (path != "/index.html") {
server.sendHeader("Cache-Control", "public, max-age=31536000, immutable");
server.sendHeader("Last-Modified", "Mon, 30 Oct 2017 01:11:56 GMT");
server.sendHeader("Expires", "06 Aug 2026 12:30:45 GMT");
}
File file = SPIFFS.open(path, "r");
size_t sent = server.streamFile(file, contentType);
file.close();
} else {
server.send(404, "text/html", "not found " + path);
}
}
void handleDeploy() {
HTTPUpload& upload = server.upload();
if(upload.status == UPLOAD_FILE_START){
fsUploadFile = SPIFFS.open("/index.html", "w");
} else if(upload.status == UPLOAD_FILE_WRITE){
if(fsUploadFile)
fsUploadFile.write(upload.buf, upload.currentSize);
} else if(upload.status == UPLOAD_FILE_END){
if(fsUploadFile)
fsUploadFile.close();
}
}
void setup(void){
Serial.begin(9600);
pinMode(LED_PIN, OUTPUT); // Initialize the LED_BUILTIN pin as an output
SPIFFS.begin();
WiFi.begin(ssid, password); //Connect to your WiFi router
Serial.println("Connecting to wifi...");
// Wait for connection
int wait_counter = 0;
digitalWrite(LED_PIN, HIGH);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
digitalWrite(LED_PIN, ++wait_counter % 2 ? HIGH : LOW);
Serial.print(".");
}
digitalWrite(LED_PIN, LOW);
//If connection successful show IP address in serial monitor
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP()); //IP address assigned to your ESP
server.on("/status", handleStatus);
server.on("/uset", handleVoltage);
server.on("/iset", handleCurrent);
server.on("/onoff", handleOnOff);
server.on("/deploy", HTTP_POST, [](){ server.send(200, "text/plain", ""); }, handleDeploy);
server.onNotFound(handleFiles);
server.begin(); //Start server
Serial.println("HTTP server started");
if (!MDNS.begin(MDSN_NAME)) {
Serial.println("Error setting up MDNS responder!");
}
Serial.println("mDNS responder started");
// Add service to MDNS-SD
MDNS.addService("http", "tcp", 80);
delay(500);
Serial.swap();
delay(500);
}
void loop(void) {
server.handleClient(); //Handle client requests
digitalWrite(LED_PIN, HIGH);
}