GET
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
#include <WiFi.h> #include <Wire.h> const char* ssid = "SSID"; const char* password = ""; const char * hostDomain = "api.openweathermap.org"; const int hostPort = 80; WiFiClient client; void setup() { Serial.begin(9600); delay(100); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } void requestURL(const char * host, uint8_t port) { Serial.println("Connecting to domain: " + String(host)); if (!client.connect(host, port)) { Serial.println("connection failed"); return; } Serial.println("Connected!"); client.println("GET /data/2.5/weather?id=6167865&APPID=9cXXXXXX0c150XXXX768ab17960aaadf HTTP/1.1"); client.print("Host: api.openweathermap.org\r\n"); client.print("Connection: close\r\n\r\n"); unsigned long timeout = millis(); while (client.available() == 0) { if (millis() - timeout > 1000) { Serial.println(">>> Client Timeout !"); client.stop(); return; } } // Read all the lines of the reply from server and print them to Serial while (client.available()) { String line = client.readStringUntil('\r'); Serial.print(line); } Serial.println(); Serial.println("closing connection"); client.stop(); } void loop() { Serial.println(); requestURL(hostDomain, hostPort); delay(30000); } |
[collapse]
JSON
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
#include <WiFi.h> #include <Wire.h> #include <ArduinoJson.h> const char* ssid = "SSID"; const char* password = ""; const char * hostDomain = "api.openweathermap.org"; const int hostPort = 80; String line; WiFiClient client; void setup() { Serial.begin(9600); delay(100); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } void requestURL(const char * host, uint8_t port) { Serial.println("Connecting to domain: " + String(host)); if (!client.connect(host, port)) { Serial.println("connection failed"); return; } Serial.println("Connected!"); client.println("GET /data/2.5/weather?id=6167865&APPID=XXXXXXXXXXXXXX5f5768ab17960aaadf&units=metric HTTP/1.1"); client.print("Host: api.openweathermap.org\r\n"); client.print("Connection: close\r\n\r\n"); unsigned long timeout = millis(); while (client.available() == 0) { if (millis() - timeout > 1000) { Serial.println(">>> Client Timeout !"); client.stop(); return; } } // Read all the lines of the reply from server and print them to Serial while (client.available()) { line = client.readStringUntil('\r'); } Serial.println(); Serial.println("closing connection"); client.stop(); } void parseJSON(String weather) { StaticJsonBuffer<3000> jsonBuffer; JsonObject& root = jsonBuffer.parseObject(weather); if (!root.success()) { Serial.print("ParseObject() failed"); } String name = root["name"]; Serial.print("name: "); Serial.println(name); float temp = root["main"]["temp"]; Serial.print("temp: "); Serial.print(temp); Serial.println(" C"); float temp_min = root["main"]["temp_min"]; Serial.print("temp_min: "); Serial.print(temp_min); Serial.println(" C"); float temp_max = root["main"]["temp_max"]; Serial.print("temp_max: "); Serial.print(temp_max); Serial.println(" C"); float pressure = root["main"]["pressure"]; Serial.print("pressure: "); Serial.print(pressure); Serial.println(" mmHg"); float humidity = root["main"]["humidity"]; Serial.print("humidity: "); Serial.print(humidity); Serial.println(" %"); } void loop() { Serial.println(); requestURL(hostDomain, hostPort); Serial.print(line); Serial.println(); Serial.println("-------------"); Serial.println(); parseJSON(line); delay(30000); } |
[collapse]