diff --git a/src/Axon.cpp b/src/Axon.cpp index 70da85a..1d6a638 100644 --- a/src/Axon.cpp +++ b/src/Axon.cpp @@ -17,6 +17,8 @@ */ #include "Axon.h" +#include +#include using namespace ECG ; @@ -114,7 +116,7 @@ bool Axon::connectToWiFi() { // Case first connection if ( _hasBegunWiFi == false) { - WiFi.begin(Keys::WiFiSSID.c_str(), Keys::WiFiPassword.c_str()) ; + WiFi.begin(Keys::WiFiSSID.c_str(), Keys::WiFiPassword.c_str()) ; _hasBegunWiFi = true ; // The device is not properly connected to the network unless the WiFi.status() @@ -185,11 +187,12 @@ bool Axon::connectToWiFi() { bool Axon::callAPI() { + //_client.loadCertificate(Config::APICertificateFingerprint.c_str(), Config::APICertificateFingerprint.length()) ; // First, we must establish a connection to an API Serial.printf("Connecting to %s on port %d... \n", Config::APIHost.c_str(), Config::APIPort) ; if ( !_client.connect(Config::APIHost, Config::APIPort) ) { - Serial.printf("Connection failed!\n") ; + Serial.printf("Connection failed!: %s\n", std::strerror(errno)) ; return false ; } @@ -208,8 +211,6 @@ bool Axon::callAPI() { // The client now reads the response sent by the server - //TODO: actually save instead of dumping - // Stores each line read from server for each read from the socket String line ; @@ -265,10 +266,13 @@ bool Axon::callAPI() { if (responseCode == "200") { // Ignore first line - _client.readStringUntil('\n') ; + //_payload = _client.readStringUntil('\n') ; + //Serial.printf("First line: %s",_payload.c_str()) ; + // Save second line _payload = _client.readStringUntil('\n') ; + //Serial.printf("Second line: %s",_payload.c_str()) ; // Ignore the rest _client.readString() ; @@ -297,17 +301,19 @@ bool Axon::parseJson_manualFallback() { // methods of class String // First we check to see if the target JSON key is in the retrieved string + int length = sizeof(Config::targetKey)/sizeof(String); + Serial.printf("Searching for key (%s) in data (%s)...\n", - Config::targetKey.c_str(), _payload.c_str()) ; + Config::targetKey[length - 1].c_str(), _payload.c_str()) ; - uint16_t indexOfKey = _payload.indexOf(Config::targetKey) ; + uint16_t indexOfKey = _payload.indexOf(Config::targetKey[length - 1]) ; if ( indexOfKey == -1 ) { Serial.printf("could not find target key manually\n") ; return false ; } Serial.printf("index of key is:%d\n",indexOfKey) ; - uint16_t lengthOfKey = Config::targetKey.length() ; + uint16_t lengthOfKey = Config::targetKey[length - 1].length() ; // If the target key is present, the data, if retrieved, will be at the index of the target key // plus the length of the key plus two more characters (A '\"' and a ',') @@ -374,8 +380,10 @@ bool Axon::parseJson() { // Case: payload exists // We use the ArduinoJson library + // calculated bufferSize using Arduino DynamicJsonBuffer jsonBuffer ; JsonObject& dataRoot = jsonBuffer.parseObject(_payload) ; + int temp; // Check for parsing failure if (dataRoot.success() == false) { @@ -401,8 +409,17 @@ bool Axon::parseJson() { // If there was no error, get the value corresponding to the key specified in config and save it // TODO: method to get nested values else { - String temp = dataRoot[Config::targetKey] ; - _targetValue = temp ; + JsonArray& data = dataRoot[Config::targetKey[0]] ; + int length = sizeof(Config::targetKey)/sizeof(String); + for (int i = 0; i < length; i++) + { + if (std::isdigit(*Config::targetKey[i].c_str())) { + JsonObject& data_object = data[i]; + temp = data_object[Config::targetKey[i + 1]]; + i++; + } + } + _targetValue = temp; Serial.printf("ArduinoJson parse found value: %s\n", _targetValue.c_str()) ; return true ; } diff --git a/src/Axon.h b/src/Axon.h index 4733658..3575e83 100644 --- a/src/Axon.h +++ b/src/Axon.h @@ -24,7 +24,7 @@ // Debugging options #define SHOW_WIFI_DIAGNISTICS 0 #define SHOW_HTTP_HEADERS 0 -#define SHOW_PAYLOAD 0 +#define SHOW_PAYLOAD 1 #define SHOW_SERVO_MOVES 0 // Define LED codes by color diff --git a/src/Config.h b/src/Config.h index 9dac2f4..34c5bc2 100644 --- a/src/Config.h +++ b/src/Config.h @@ -23,17 +23,19 @@ namespace Config { // The domain name of an API -const String APIHost = "isenseproject.org" ; +const String APIHost = "192.168.1.7" ; // The path the the version of the API to be targeted -const String APIPath = "/api/v1" ; +const String APIPath = "/api/UCAPS" ; + +const String APICertificateFingerprint = "94 BA D8 CA D7 EE 77 AC D1 3D F8 FF 09 E9 72 D6 FE 6A 7D 8C" ; // The path to the particular endpoint to be targeted within the API // /projects/2156 on the iSENSE API is Plinko! -const String APIEndpoint = "/projects/2156" ; +const String APIEndpoint = "/Parking/AvailableParking" ; // Port to use in connection to API -const uint16_t APIPort = 80 ; +const uint16_t APIPort = 3000 ; /* TODO: @@ -42,13 +44,13 @@ const uint16_t APIPort = 80 ; */ // FIXME: This may not be the best way to do this -const String targetKey = "dataSetCount" ; +const String targetKey[] = {"data", "0", "AvailableSpaces" } ; // The expected range of the retrieved value. The servo arm will be adjusted to show how far // the retrieved values is between these values. If the value is outside this range, the servo // arm will be moved to either extreme position (0 for <= low bound, 180 for >= high bound) -const double displayLowBound = 1600; -const double displayHighBound = 1700.0 ; +const double displayLowBound = 0.0; +const double displayHighBound = 223.0 ; } // namespace Config diff --git a/src/Keys.h.template b/src/Keys.h.template deleted file mode 100644 index 275d735..0000000 --- a/src/Keys.h.template +++ /dev/null @@ -1,31 +0,0 @@ -/* - Arms for iSENSE - Copyright (C) 2018 Engaging Computing Group - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - -// Rename this file to Keys.h and add your WiFi credentials below to enable networking functionality - -#ifndef KEYS_H -#define KEYS_H - -namespace Keys { - -const String WiFiSSID = "ENTER-SSID-HERE" ; -const String WiFiPassword = "ENTER-PASSWORD-HERE" ; - -} // namespace Keys - -#endif // KEYS_H \ No newline at end of file