· 4 years ago · May 15, 2021, 01:08 PM
1
2#include "mbed.h"
3#include <cstdint>
4#define JSON_NOEXCEPTION
5#include "DFRobot_RGBLCD.h"
6#include "json.hpp"
7#include "cert.h"
8#include "wifi.h"
9#include "HTS221Sensor.h"
10using json = nlohmann::json;
11
12// This rendition changes static char buffer from 4096 to 1024, and increases thread stack size.
13
14int main() {
15 // Get pointer to default network interface
16 NetworkInterface *network = NetworkInterface::get_default_instance();
17
18 if (!network) {
19 printf("Failed to get default network interface\n");
20 while (1);
21 }
22
23 // Connect to network
24 printf("Connecting to the network...\n");
25 nsapi_size_or_error_t result = network->connect();
26
27 // Check if the connection is successful
28 if (result != 0) {
29 printf("Failed to connect to network: %d\n", result);
30 while (1);
31 }
32
33 printf("Connection to network successful!\n");
34
35 // Create and allocate resources for socket.
36 // TLSSocket is used for HTTPS/SSL
37 TLSSocket *socket = new TLSSocket();
38
39 // Set the root certificate of the web site.
40 // See include/cert.h for how to download the cert.
41 result = socket->set_root_ca_cert(cert);
42 if (result != 0) {
43 printf("Error: socket->set_root_ca_cert() returned %d\n", result);
44 return result;
45 }
46
47 socket->open(network);
48
49 // Create destination address
50 SocketAddress address;
51
52 // Get IP address of host by name
53 result = network->gethostbyname("api.ipgeolocation.io", &address);
54
55 // Check result
56 if (result != 0) {
57 printf("Failed to get IP address of host: %d\n", result);
58 while (1);
59 }
60
61 printf("Got address of host\n");
62
63 // Set server port number, 443 for HTTPS/SSL
64 address.set_port(443);
65
66 // Connect to server at the given address
67 result = socket->connect(address);
68
69 // Check result
70 if (result != 0) {
71 printf("Failed to connect to server: %d\n", result);
72 while (1);
73 }
74
75 printf("Connection to server successful!\n");
76
77 // Create HTTP request
78 char request[] = "GET /timezone?apiKey=90d731ff3697493bab2121880ae99811 HTTP/1.1\r\n"
79 "Host: api.ipgeolocation.io\r\n"
80 "Connection: close\r\n"
81 "\r\n";
82
83 // Send request
84 result = send_request(socket, request);
85
86 // Check result
87 if (result != 0) {
88 printf("Failed to send request: %d\n", result);
89 while (1);
90 }
91
92 // We need to read the response into memory. The destination is called a buffer.
93 // If you make this buffer static it will be placed in BSS and won't use stack memory.
94 static char buffer[1024]; //lowering buffer from 4096 to 1024, increasing thread stack size
95 result = read_response(socket, buffer, sizeof(buffer));
96
97 // Check result
98 if (result != 0) {
99 printf("Failed to read response: %d\n", result);
100
101 while (1);
102 }
103
104 socket->close();
105 delete socket;
106 // Find the start and end of the JSON data.
107 // If the json response is an array you need to replace this with [ and ]
108 char* json_begin = strchr(buffer, '{');
109 char* json_end = strrchr(buffer, '}');
110
111 // Check if we actually got JSON in the response
112 if (json_begin == nullptr || json_end == nullptr) {
113 printf("Failed to find JSON in response\n");
114 while (1);
115 }
116
117 // End the string after the end of the JSON data in case the response contains trailing data
118 json_end[1] = 0;
119
120 printf("JSON response:\n");
121 printf("%s\n", json_begin);
122
123 // Parse response as JSON, starting from the first {
124 json document = json::parse(json_begin);
125
126 // Get IP address from JSON object
127 std::string latitude;
128 std::string longitude;
129 std::string time_24;
130 document["time_24"].get_to(time_24);
131 document["geo"]["latitude"].get_to(latitude);
132 document["geo"]["longitude"].get_to(longitude);
133
134 //--------------------------------------------------------------------- New connection, old socket has to be deleted and new socket "socket2" is made
135 // Set certificate for weather api "cert2"
136 TLSSocket *socket2 = new TLSSocket();
137 result = socket2->set_root_ca_cert(cert2);
138 if (result != 0) {
139 printf("Error: socket->set_root_ca_cert() returned %d\n", result);
140 return result;
141 }
142 socket2->open(network);
143 SocketAddress address2;
144
145 // Get IP address of (WEATHER)
146 result = network->gethostbyname("api.openweathermap.org", &address2);
147
148 // Check result
149 if (result != 0) {
150 printf("Failed to get IP address of host: %d\n", result);
151 while (1);
152 }
153
154 printf("Got address of host\n");
155
156 // Set server port number, 443 for HTTPS/SSL
157 address2.set_port(443);
158
159 // Connect to server at the given address
160 result = socket2->connect(address2);
161
162 if (result != 0) {
163 printf("Failed to connect to server: %d\n", result);
164 while (1);
165 }
166
167 // API key for weatherapi : 2179f8cb6e1174d4b0952b3d9af4f7b4
168 // Create HTTP request
169 char temp_get[500];
170
171 sprintf(temp_get, "GET /data/2.5/weather?lat=%s&lon=%s&appid=2179f8cb6e1174d4b0952b3d9af4f7b4 HTTP/1.1\r\n"
172 "Host: api.openweathermap.org\r\n"
173 "Connection: close\r\n"
174 "\r\n", latitude.c_str(), longitude.c_str());
175
176 result = send_request(socket2, temp_get);
177
178 // Check result
179 if (result != 0) {
180 printf("Failed to send request: %d\n", result);
181 while (1);
182 }
183
184 // We need to read the response into memory. The destination is called a buffer.
185 // If you make this buffer static it will be placed in BSS and won't use stack memory.
186 result = read_response(socket2, buffer, sizeof(buffer));
187
188 // Check result
189 if (result != 0) {
190 printf("Failed to read response: %d\n", result);
191
192 while (1);
193 }
194
195
196 socket2->close();
197
198 // Check if we actually got JSON in the response
199 if (json_begin == nullptr || json_end == nullptr) {
200 printf("Failed to find JSON in response\n");
201 while (1);
202 }
203
204 // End the string after the end of the JSON data in case the response contains trailing data
205 json_end[1] = 0;
206
207 printf("JSON response:\n");
208 printf("%s\n", json_begin);
209
210 // Parse response as JSON, starting from the first {
211 json document2 = json::parse(json_begin);
212
213 while (true) {
214 ThisThread::sleep_for(1s);
215 }
216}
217