wip: POST
This commit is contained in:
parent
5d40ad3acb
commit
c498808b74
@ -76,7 +76,7 @@ unsigned int PIN_5 = 20;
|
|||||||
unsigned int PIN_6 = 33;
|
unsigned int PIN_6 = 33;
|
||||||
unsigned int PIN_7 = 32;
|
unsigned int PIN_7 = 32;
|
||||||
unsigned int PIN_8 = 35;
|
unsigned int PIN_8 = 35;
|
||||||
unsigned int pin_list[2] = {
|
unsigned int pin_list[8] = {
|
||||||
PIN_1,
|
PIN_1,
|
||||||
PIN_2,
|
PIN_2,
|
||||||
PIN_3,
|
PIN_3,
|
||||||
@ -210,6 +210,7 @@ void run_scheduler(){
|
|||||||
|
|
||||||
|
|
||||||
// HTTP
|
// HTTP
|
||||||
|
// void deserializeSchedule(WiFiClient &client, int schedule[24]) {
|
||||||
void deserializeSchedule(WiFiClient &client, int schedule[24]) {
|
void deserializeSchedule(WiFiClient &client, int schedule[24]) {
|
||||||
client.readBytes((char*)schedule, 24 * sizeof(int));
|
client.readBytes((char*)schedule, 24 * sizeof(int));
|
||||||
}
|
}
|
||||||
@ -241,13 +242,24 @@ void setup(){
|
|||||||
if (pref_init == false) {
|
if (pref_init == false) {
|
||||||
Serial.println("preferences not exist");
|
Serial.println("preferences not exist");
|
||||||
preferences.putBool("test", true);
|
preferences.putBool("test", true);
|
||||||
preferences.putBytes(RELAY_1_SCHEDULE, scheduler_1_default, sizeof(scheduler_1_default));
|
preferences.putBytes(RELAY_1_SCHEDULE, scheduler_default_deactivate, sizeof(scheduler_1_default));
|
||||||
preferences.putBytes(RELAY_2_SCHEDULE, scheduler_2_default, sizeof(scheduler_2_default));
|
preferences.putBytes(RELAY_2_SCHEDULE, scheduler_default_deactivate, sizeof(scheduler_2_default));
|
||||||
/// ... more to come
|
preferences.putBytes(RELAY_3_SCHEDULE, scheduler_default_deactivate, sizeof(scheduler_2_default));
|
||||||
|
preferences.putBytes(RELAY_4_SCHEDULE, scheduler_default_deactivate, sizeof(scheduler_2_default));
|
||||||
|
preferences.putBytes(RELAY_5_SCHEDULE, scheduler_default_deactivate, sizeof(scheduler_2_default));
|
||||||
|
preferences.putBytes(RELAY_6_SCHEDULE, scheduler_default_deactivate, sizeof(scheduler_2_default));
|
||||||
|
preferences.putBytes(RELAY_7_SCHEDULE, scheduler_default_deactivate, sizeof(scheduler_2_default));
|
||||||
|
preferences.putBytes(RELAY_8_SCHEDULE, scheduler_default_deactivate, sizeof(scheduler_2_default));
|
||||||
}
|
}
|
||||||
bool test = preferences.getBool("test");
|
bool test = preferences.getBool("test");
|
||||||
preferences.getBytes(RELAY_1_SCHEDULE, scheduler_1, preferences.getBytesLength(RELAY_1_SCHEDULE));
|
preferences.getBytes(RELAY_1_SCHEDULE, scheduler_1, preferences.getBytesLength(RELAY_1_SCHEDULE));
|
||||||
preferences.getBytes(RELAY_2_SCHEDULE, scheduler_2, preferences.getBytesLength(RELAY_2_SCHEDULE));
|
preferences.getBytes(RELAY_2_SCHEDULE, scheduler_2, preferences.getBytesLength(RELAY_2_SCHEDULE));
|
||||||
|
preferences.getBytes(RELAY_3_SCHEDULE, scheduler_2, preferences.getBytesLength(RELAY_3_SCHEDULE));
|
||||||
|
preferences.getBytes(RELAY_4_SCHEDULE, scheduler_2, preferences.getBytesLength(RELAY_4_SCHEDULE));
|
||||||
|
preferences.getBytes(RELAY_5_SCHEDULE, scheduler_2, preferences.getBytesLength(RELAY_5_SCHEDULE));
|
||||||
|
preferences.getBytes(RELAY_6_SCHEDULE, scheduler_2, preferences.getBytesLength(RELAY_6_SCHEDULE));
|
||||||
|
preferences.getBytes(RELAY_7_SCHEDULE, scheduler_2, preferences.getBytesLength(RELAY_7_SCHEDULE));
|
||||||
|
preferences.getBytes(RELAY_8_SCHEDULE, scheduler_2, preferences.getBytesLength(RELAY_8_SCHEDULE));
|
||||||
preferences.end();
|
preferences.end();
|
||||||
Serial.println("Setup::PREFERENCES::end");
|
Serial.println("Setup::PREFERENCES::end");
|
||||||
|
|
||||||
@ -308,7 +320,9 @@ void loop(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
String request = client.readStringUntil('\r');
|
String request = client.readStringUntil('\r');
|
||||||
client.flush();
|
|
||||||
|
Serial.print("request: ");
|
||||||
|
Serial.println(request);
|
||||||
|
|
||||||
// GET request
|
// GET request
|
||||||
if (request.indexOf("GET /api/schedule/") >= 0) {
|
if (request.indexOf("GET /api/schedule/") >= 0) {
|
||||||
@ -344,25 +358,47 @@ void loop(){
|
|||||||
// POST request
|
// POST request
|
||||||
int slashIndex = request.indexOf("POST /api/schedule/");
|
int slashIndex = request.indexOf("POST /api/schedule/");
|
||||||
if (slashIndex != -1) {
|
if (slashIndex != -1) {
|
||||||
Serial.println("slashIndex " + slashIndex);
|
Serial.print("slashIndex: ");
|
||||||
int nextSlashIndex = slashIndex + 17;
|
Serial.println(slashIndex);
|
||||||
|
int nextSlashIndex = slashIndex + 19;
|
||||||
|
|
||||||
// Extract the plug ID
|
// Extract the plug ID
|
||||||
String plugIdStr = request.substring(slashIndex + 1, nextSlashIndex);
|
String plugIdStr = request.substring(nextSlashIndex, nextSlashIndex + 1);
|
||||||
Serial.println("plugIdStr " + plugIdStr);
|
Serial.print("plugIdStr ");
|
||||||
int plugId = plugIdStr.toInt();
|
Serial.println(plugIdStr);
|
||||||
|
int plugId = plugIdStr.toInt() - 1;
|
||||||
delay(500);
|
delay(500);
|
||||||
|
|
||||||
// Read the next line which should have the POST body/content
|
// Read the next line which should have the POST body/content
|
||||||
String postBody = client.readStringUntil('\n');
|
String preBody ;
|
||||||
|
while ( preBody = client.readStringUntil('\n') ){
|
||||||
|
|
||||||
|
Serial.print("Read preBody: ");
|
||||||
|
Serial.println(preBody);
|
||||||
|
if( prebody == ""){
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
String postBody = client.readString();
|
||||||
|
Serial.print("Read postBody: ");
|
||||||
|
Serial.println(postBody);
|
||||||
|
|
||||||
// Update the schedule for the specified plug
|
// Update the schedule for the specified plug
|
||||||
if (plugId >= 0 && plugId < 8) {
|
if (plugId >= 0 && plugId < 8) {
|
||||||
// deserializeSchedule(&client, scheduler_list[plugId]);
|
// deserializeSchedule(&client, scheduler_list[plugId]);
|
||||||
|
char *buffer ;
|
||||||
|
// size_t count = client.readBytes(buffer, 24 * sizeof(int));
|
||||||
|
|
||||||
|
|
||||||
Serial.println("Schedule updated for plug " + plugIdStr);
|
Serial.println("Schedule updated for plug " + plugIdStr);
|
||||||
} else {
|
} else {
|
||||||
Serial.println("Invalid plug ID");
|
Serial.println("Invalid plug ID");
|
||||||
}
|
}
|
||||||
|
client.println("HTTP/1.1 200 OK");
|
||||||
|
client.println("Content-Type: text/plain");
|
||||||
|
client.println("Connection: close");
|
||||||
|
client.println();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
26
scheduler.h
26
scheduler.h
@ -39,6 +39,32 @@ unsigned int scheduler_1_default[24] = {
|
|||||||
EVERY_2_INV,
|
EVERY_2_INV,
|
||||||
EVERY_2,
|
EVERY_2,
|
||||||
};
|
};
|
||||||
|
unsigned int scheduler_default_deactivate[24] = {
|
||||||
|
A00,
|
||||||
|
A00,
|
||||||
|
A00,
|
||||||
|
A00,
|
||||||
|
A00,
|
||||||
|
A00,
|
||||||
|
A00,
|
||||||
|
A00,
|
||||||
|
A00,
|
||||||
|
A00,
|
||||||
|
A00,
|
||||||
|
A00,
|
||||||
|
A00,
|
||||||
|
A00,
|
||||||
|
A00,
|
||||||
|
A00,
|
||||||
|
A00,
|
||||||
|
A00,
|
||||||
|
A00,
|
||||||
|
A00,
|
||||||
|
A00,
|
||||||
|
A00,
|
||||||
|
A00,
|
||||||
|
A00,
|
||||||
|
};
|
||||||
unsigned int scheduler_2_default[24] = {
|
unsigned int scheduler_2_default[24] = {
|
||||||
EVERY_2,
|
EVERY_2,
|
||||||
EVERY_2_INV,
|
EVERY_2_INV,
|
||||||
|
Loading…
Reference in New Issue
Block a user