|
| 1 | +/********************************************************/ |
| 2 | +/* ATMOSCAN */ |
| 3 | +/* */ |
| 4 | +/* Author: Marc Finns 2017 */ |
| 5 | +/* */ |
| 6 | +/********************************************************/ |
| 7 | + |
| 8 | +#include <ArduinoJson.h> |
| 9 | +#include "ESP8266WiFi.h" |
| 10 | +#include <NtpClientLib.h> // http://github.com/gmag11/NtpClient |
| 11 | +#include <JsonStreamingParser.h> |
| 12 | +#include <Syslog.h> // https://github.com/arcao/ESP8266_Syslog |
| 13 | +#include "Wsclient.h" |
| 14 | +#include "TimeSpace.h" |
| 15 | +#include "GlobalDefinitions.h" |
| 16 | + |
| 17 | +extern struct Configuration config; |
| 18 | +extern Syslog syslog; |
| 19 | + |
| 20 | +// ********************************************************** |
| 21 | +// Step 3 - Geocoding |
| 22 | +// Get Location name from latitude and longitude |
| 23 | +// ********************************************************** |
| 24 | + |
| 25 | +bool Geocode::acquire(double latitude, double longitude) |
| 26 | +{ |
| 27 | + |
| 28 | +#ifdef DEBUG_SYSLOG |
| 29 | + String myBuffer; |
| 30 | +#endif |
| 31 | + |
| 32 | + //Connect to the client and make the api call |
| 33 | + if (httpConnect()) |
| 34 | + { |
| 35 | + // Concatenate url and key |
| 36 | + String url = FPSTR(geoCodeURL); |
| 37 | + url += F("&lat="); |
| 38 | + url += String(latitude, 8); |
| 39 | + url += F("&lng="); |
| 40 | + url += String(longitude, 8); |
| 41 | + url += F("&username=" ); |
| 42 | + url += config.geonames_user; |
| 43 | + |
| 44 | + // Serial.println("URL = " + url); |
| 45 | + |
| 46 | +#ifdef DEBUG_SYSLOG |
| 47 | + syslog.log(LOG_DEBUG, "URL = " + url); |
| 48 | +#endif |
| 49 | + |
| 50 | + if (httpGet(url) && skipResponseHeaders()) |
| 51 | + { |
| 52 | + // Invalidate current values |
| 53 | + locality = FPSTR(empty); |
| 54 | + country = FPSTR(empty); |
| 55 | + countryCode = FPSTR(empty); |
| 56 | + |
| 57 | + // Allow for slow server... |
| 58 | + int retryCounter = 0; |
| 59 | + while (!client.available()) |
| 60 | + { |
| 61 | + delay(1000); |
| 62 | + retryCounter++; |
| 63 | + if (retryCounter > 10) |
| 64 | + { |
| 65 | + return false; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + while (client.available()) |
| 70 | + { |
| 71 | + JsonStreamingParser parser; |
| 72 | + parser.setListener(this); |
| 73 | + char c; |
| 74 | + |
| 75 | + while (client.available()) |
| 76 | + { |
| 77 | + c = client.read(); |
| 78 | + |
| 79 | +#ifdef DEBUG_SERIAL |
| 80 | + Serial.print(c); |
| 81 | +#endif |
| 82 | + |
| 83 | +#ifdef DEBUG_SYSLOG |
| 84 | + myBuffer = myBuffer + String(c); |
| 85 | +#endif |
| 86 | + |
| 87 | + parser.parse(c); |
| 88 | + |
| 89 | + // Improves reliability from ESP version 2.4.0 |
| 90 | + yield(); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + else |
| 95 | + // Get failed |
| 96 | + return false; |
| 97 | + } |
| 98 | + else |
| 99 | + // Could not connect |
| 100 | + return false; |
| 101 | + |
| 102 | + disconnect(); |
| 103 | + |
| 104 | +#ifdef DEBUG_SYSLOG |
| 105 | + syslog.log(LOG_DEBUG, "RESPONSE = " + myBuffer); |
| 106 | +#endif |
| 107 | + |
| 108 | + if (country == "" || countryCode == "") |
| 109 | + return false; |
| 110 | + else |
| 111 | + return true; |
| 112 | +} |
| 113 | + |
| 114 | +void Geocode::whitespace(char c) { |
| 115 | +#ifdef DEBUG_LOG |
| 116 | + Serial.println("whitespace"); |
| 117 | +#endif |
| 118 | +} |
| 119 | + |
| 120 | +void Geocode::startDocument() { |
| 121 | +#ifdef DEBUG_LOG |
| 122 | + Serial.println("start document"); |
| 123 | +#endif |
| 124 | +} |
| 125 | + |
| 126 | +void Geocode::key(String key) |
| 127 | +{ |
| 128 | +#ifdef DEBUG_LOG |
| 129 | + Serial.println("key: " + key); |
| 130 | +#endif |
| 131 | + currentKey = key; |
| 132 | +} |
| 133 | + |
| 134 | +void Geocode::value(String value) |
| 135 | +{ |
| 136 | +#ifdef DEBUG_LOG |
| 137 | + Serial.println("value: " + value); |
| 138 | +#endif |
| 139 | + if (currentKey == F("name")) |
| 140 | + { |
| 141 | + locality = value; |
| 142 | + } |
| 143 | + else if (currentKey == F("countryName")) |
| 144 | + { |
| 145 | + country = value; |
| 146 | + } |
| 147 | + else if (currentKey == F("countryCode")) |
| 148 | + { |
| 149 | + countryCode = value; |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | + |
| 154 | +void Geocode::endArray() { |
| 155 | +#ifdef DEBUG_LOG |
| 156 | + Serial.println("end array. "); |
| 157 | +#endif |
| 158 | +} |
| 159 | + |
| 160 | +void Geocode::endObject() { |
| 161 | +#ifdef DEBUG_LOG |
| 162 | + Serial.println("end object. "); |
| 163 | +#endif |
| 164 | +} |
| 165 | + |
| 166 | +void Geocode::endDocument() { |
| 167 | +#ifdef DEBUG_LOG |
| 168 | + Serial.println("end document. "); |
| 169 | +#endif |
| 170 | +} |
| 171 | + |
| 172 | +void Geocode::startArray() { |
| 173 | +#ifdef DEBUG_LOG |
| 174 | + Serial.println("start array. "); |
| 175 | +#endif |
| 176 | +} |
| 177 | + |
| 178 | +void Geocode::startObject() { |
| 179 | +#ifdef DEBUG_LOG |
| 180 | + Serial.println("start object. "); |
| 181 | +#endif |
| 182 | +} |
| 183 | + |
| 184 | +String Geocode::getLocality() |
| 185 | +{ |
| 186 | + return locality; |
| 187 | +} |
| 188 | + |
| 189 | +String Geocode::getCountry() |
| 190 | +{ |
| 191 | + return country; |
| 192 | +} |
| 193 | + |
| 194 | +String Geocode::getCountryCode() |
| 195 | +{ |
| 196 | + return countryCode; |
| 197 | +} |
| 198 | + |
0 commit comments