Commit a8c63d07 authored by PhiBo's avatar PhiBo 🚀

Use Json parser to submit/parse register information

parent b151ac24
#include <ArduinoJson.h>
#include "sensor_node.h" #include "sensor_node.h"
void handleAPIHostname() void handleAPIHostname()
...@@ -88,70 +89,80 @@ void handlePassword() ...@@ -88,70 +89,80 @@ void handlePassword()
void handleRegister() void handleRegister()
{ {
WiFiClient *client;
char hostname[NODE_EEPROM_API_HOSTNAME_MAX_LENGTH + 1];
StaticJsonBuffer<200> jsonBuffer;
char buffer[201];
uint8_t i;
String email = server->arg("email"); String email = server->arg("email");
String name = server->arg("name"); String name = server->arg("name");
// ToDo: read from eeprom
char host[] = "192.168.0.1";
int httpPort = 80;
if(email.length() == 0) { if(email.length() == 0) {
server->send(400, "text/plain", "E-Mail not given"); server->send(400, "text/plain", "E-Mail not given");
return; return;
} }
String message = ""; client = connectSensorAPI();
message += "email:" + email + "\r\n";
if (name.length()) {
message += "name:" + name + "\r\n";
}
if (!client->connect(host, httpPort)) { if (client == NULL) {
Serial.println("connection failed"); Serial.println("connection failed");
server->send(500, "text/plain", "Unable to connect to remote server"); server->send(500, "text/plain", "Unable to connect to remote server");
return; return;
} }
JsonObject& root = jsonBuffer.createObject();
root["email"] = email;
root["name"] = name;
root.printTo(buffer, 200);
getAPIHostnameOrDefault(&hostname[0], NODE_EEPROM_API_HOSTNAME_MAX_LENGTH);
client->print("POST /sensors HTTP/1.1\r\n"); client->print("POST /sensors HTTP/1.1\r\n");
client->print("Host: "); client->print("Host: ");
client->print(host); client->print(hostname);
client->print("\r\n"); client->print("\r\n");
client->print("X-Sensor-Version: 1\r\n"); client->print("X-Sensor-Version: 1\r\n");
client->print("Content-Type: text/plain\r\n"); client->print("Content-Type: text/plain\r\n");
client->print("Content-Length: "); client->print("Content-Length: ");
client->print(message.length()); client->print(strlen(buffer));
client->print("\r\n"); client->print("\r\n");
client->print("\r\n"); client->print("\r\n");
client->print(message); client->print(buffer);
String sensor_id = ""; String sensor_id = "";
String sensor_key = ""; String sensor_key = "";
unsigned long start_time = millis(); unsigned long start_time = millis();
i = 0;
char c;
while(1) { while(1) {
if(client->available()) { if(client->available()) {
String line = client->readStringUntil('\n'); c = client->read();
if(line.startsWith("apikey:")) { buffer[i] = c;
sensor_key = line.substring(7); i++;
} if(c == '}') {
if(line.startsWith("id:")) { break;
sensor_id = line.substring(3);
} }
Serial.println(line);
} else { } else {
delay(100); delay(100);
} }
if(sensor_id.length() > 0 && sensor_key.length() > 0) { if(millis() - start_time > 5000) {
break; server->send(400, "text/plain", "API Timeout");
} }
if(millis() - start_time > 10000) { if(i >= 200) {
break; server->send(400, "text/plain", "Memory Limit");
} }
} }
client->stop(); client->stop();
Serial.println(sensor_id); JsonObject& root2 = jsonBuffer.parseObject(buffer);
Serial.println(sensor_key); const char* uuid = root2["uuid"];
const char* key = root2["key"];
Serial.println(uuid);
Serial.println(key);
// ToDo: RPC call
server->send(200, "text/plain", "Registred"); server->send(200, "text/plain", "Registred");
} }
......
...@@ -40,8 +40,6 @@ void setup() { ...@@ -40,8 +40,6 @@ void setup() {
EEPROM.begin(1024); EEPROM.begin(1024);
client = new WiFiClient();
if(node_mode == NODE_MODE_ACTIVE) { if(node_mode == NODE_MODE_ACTIVE) {
WiFi.mode(WIFI_STA); WiFi.mode(WIFI_STA);
//connectWiFiClient(20); //connectWiFiClient(20);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment