diff --git a/examples/DataSend/DataSend.ino b/examples/DataSend/DataSend.ino index b756fd2..4bbdda8 100644 --- a/examples/DataSend/DataSend.ino +++ b/examples/DataSend/DataSend.ino @@ -1,7 +1,7 @@ #include #include -PxServ db; +PxServ pxserv; #define WIFI_SSID "" // Wifi Adı #define WIFI_PASS "" // Wifi Şifresi @@ -16,16 +16,20 @@ void setup() Serial.print("."); } Serial.println("Baglandi"); - db.login(""); // APIKEY + pxserv.login(""); // PxServ API Key } void loop() { - db.set("msg", "Hello World"); + int status1 = pxserv.set("msg", "Hello Earth"); - String msg = db.get("msg"); + String msg = pxserv.get("msg"); + int status2 = pxserv.del("msg"); + + Serial.println("Status 1 : " + String(status1)); + Serial.println("Status 2 : " + String(status2)); Serial.println("Mesaj : " + String(msg)); delay(1000); diff --git a/library.properties b/library.properties index 3338b27..09c24cb 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=PxServ -version=1.0.0 +version=1.0.1 author=mustafa_kok maintainer=PxServ sentence=Thanks to PxServ, get the opportunity to add power to IoT projects that need things like sending data and reading data over WiFi! @@ -7,4 +7,3 @@ paragraph=Thanks to PxServ, get the opportunity to add power to IoT projects tha category=* url=https://github.com/pxsty0/pxserv.lib architectures=* -depends=ArduinoJson diff --git a/src/PxServ.cpp b/src/PxServ.cpp index 1a6a5f4..5e866c3 100644 --- a/src/PxServ.cpp +++ b/src/PxServ.cpp @@ -16,7 +16,7 @@ int PxServ::set(String key, String value) HTTPClient https; String token = PxServ::token; - if (https.begin(*client, "http://api.pxserv.net/api/plain/set")) + if (https.begin(*client, "https://api.pxserv.net/api/plain/set")) { https.addHeader("Content-Type", "application/json"); @@ -28,8 +28,27 @@ int PxServ::set(String key, String value) httpCode == HTTP_CODE_MOVED_PERMANENTLY || httpCode == HTTP_CODE_NO_CONTENT) { + String payload = https.getString(); + if (payload.startsWith("PXSERV+ERR=")) + { + payload.replace("PXSERV+ERR=", ""); + String errCode = payload; - status = 200; + if (errCode == "INVALID_AUTH") + status = 401; + else if (errCode == "INVALID_PARAMS") + status = 204; + } + else if (payload.startsWith("PXSERV+DATA=")) + { + payload.replace("PXSERV+DATA=", ""); + String dataCode = payload; + + if (dataCode == "OK") + status = 200; + } + else + status = 400; } else { @@ -44,22 +63,23 @@ int PxServ::set(String key, String value) } else { - status = 2; + status = 1; } } else { - status = 3; + status = 1; } delete client; return status; } String PxServ::get(String key) { + WiFiClientSecure *client = new WiFiClientSecure; int status = 200; - String data = "!"; + String data = "PXSERV+DATA=NOT_READING"; if (client) { @@ -68,7 +88,7 @@ String PxServ::get(String key) HTTPClient https; String token = PxServ::token; - if (https.begin(*client, "https://cdn.anicordapp.net/api/plain/get")) + if (https.begin(*client, "https://api.pxserv.net/api/plain/get")) { https.addHeader("Content-Type", "application/json"); @@ -80,8 +100,28 @@ String PxServ::get(String key) httpCode == HTTP_CODE_MOVED_PERMANENTLY || httpCode == HTTP_CODE_NO_CONTENT) { - data = https.getString(); - status = 200; + String payload = https.getString(); + if (payload.startsWith("PXSERV+ERR=")) + { + payload.replace("PXSERV+ERR=", ""); + String errCode = payload; + + if (errCode == "INVALID_AUTH") + status = 401; + else if (errCode == "INVALID_PARAMS") + status = 204; + else if (errCode == "INVALID_DATA_KEY") + status = 206; + } + else if (payload.startsWith("PXSERV+DATA=")) + { + payload.replace("PXSERV+DATA=", ""); + data = payload; + + status = 200; + } + else + status = 400; } else { @@ -96,14 +136,84 @@ String PxServ::get(String key) } else { - status = 2; + status = 1; } } else { - status = 3; + status = 1; } delete client; return data; } -int PxServ::del(String key){}; \ No newline at end of file +int PxServ::del(String key) +{ + + WiFiClientSecure *client = new WiFiClientSecure; + int status = 200; + if (client) + { + client->setInsecure(); + + HTTPClient https; + + String token = PxServ::token; + if (https.begin(*client, "https://api.pxserv.net/api/plain/delete")) + { + https.addHeader("Content-Type", "application/json"); + + int httpCode = https.POST("{\"token\":\"" + token + + "\",\"key\":\"" + key + "\"}"); + if (httpCode > 0) + { + if (httpCode == HTTP_CODE_OK || + httpCode == HTTP_CODE_MOVED_PERMANENTLY || + httpCode == HTTP_CODE_NO_CONTENT) + { + String payload = https.getString(); + if (payload.startsWith("PXSERV+ERR=")) + { + payload.replace("PXSERV+ERR=", ""); + String errCode = payload; + + if (errCode == "INVALID_AUTH") + status = 401; + else if (errCode == "INVALID_PARAMS") + status = 204; + else if (errCode == "INVALID_DATA_KEY") + status = 206; + } + else if (payload.startsWith("PXSERV+DATA=")) + { + payload.replace("PXSERV+DATA=", ""); + String dataCode = payload; + + if (dataCode == "OK") + status = 200; + } + else + status = 400; + } + else + { + status = 1; + } + https.end(); + } + else + { + status = 1; + } + } + else + { + status = 1; + } + } + else + { + status = 1; + } + delete client; + return status; +}; \ No newline at end of file diff --git a/src/PxServ.h b/src/PxServ.h index bed8c3c..72fa930 100644 --- a/src/PxServ.h +++ b/src/PxServ.h @@ -9,7 +9,7 @@ #include #include #else -#warning "Kütüphane Sadece Deneyap Kartlar İle Çalışmaktadır" +#warning "Kütüphane Sadece Deneyap Geliştirme Kartları ve ESP32 ler ile Çalışmaktadır" #include #include #include