ENTS-tock
Environmental NeTworked Sensor firmware on Tock OS
Loading...
Searching...
No Matches
dirtviz.cpp
Go to the documentation of this file.
1
8#include "dirtviz.hpp"
9
10#include <Arduino.h>
11#include <ArduinoLog.h>
12
13#include <iomanip>
14#include <sstream>
15#include <vector>
16
18unsigned int g_resp_timeout = 1000;
19
21const size_t g_request_size = 512;
22
23// TODO(jmadden173): update to dynamic version
25const char g_user_agent[] = "ents/2.3.0";
26
28
29Dirtviz::Dirtviz(const char* url) { SetUrl(url); }
30
32
33void Dirtviz::SetUrl(const char* url) {
34 Log.traceln("Dirtviz::SetUrl");
35 Log.traceln("Setting URL to %s", url);
36 this->url.setUrl(url);
37 Log.traceln("URL stored is %s", this->url.getUrl().c_str());
38}
39
40unsigned int Dirtviz::Check() {
41 Log.traceln("Dirtviz::Check");
42 Log.noticeln("Connecting to %s:%d", url.getHost().c_str(), url.getPort());
43
44 if (!client.connect(url.getHost().c_str(), (uint16_t)url.getPort())) {
45 return 0;
46 }
47
48 Log.traceln("Sending GET request");
49
50 // format request
51 std::ostringstream req;
52 req << "GET " << "/api/" << " HTTP/1.1" << "\r\n";
53 req << "Host: " << url.getHost().c_str() << "\r\n";
54 req << "User-Agent: " << g_user_agent << "\r\n";
55 req << "\r\n";
56
57 // send full request to server
58 int bytes_written = client.write(req.str().c_str());
59 Log.noticeln("Wrote %d bytes to server", bytes_written);
60
61 return bytes_written;
62}
63
64unsigned int Dirtviz::SendMeasurement(const uint8_t* meas, size_t meas_len) {
65 char buffer[100];
66
67 Log.noticeln("Sending measurement");
68 Log.traceln("WiFi status: %d", WiFi.status());
69
70 // connect to server
71 if (!client.connect(url.getHost().c_str(), (uint16_t)url.getPort())) {
72 Log.errorln("Connection to %s:%d failed!", url.getHost(), url.getPort());
73 }
74
75 // send data
76
77 // format request
78 std::ostringstream headers;
79 headers << "POST " << "/" << url.getPath().c_str() << " HTTP/1.1" << "\r\n";
80 headers << "Host: " << url.getHost().c_str() << "\r\n";
81 headers << "User-Agent: " << g_user_agent << "\r\n";
82 headers << "Content-Type: application/octet-stream" << "\r\n";
83 headers << "Content-Length: " << meas_len << "\r\n";
84 headers << "Connection: close" << "\r\n";
85 headers << "SensorVersion: 2" << "\r\n";
86 headers << "\r\n";
87
88 // copy stream to string
89 std::string headers_str = headers.str();
90
91 Log.verboseln("Headers:\r\n----\r\n%s\r\n----\r\n", headers_str.c_str());
92
93 // copy data into request array
94 std::vector<char> request;
95 std::copy(headers_str.begin(), headers_str.end(),
96 std::back_inserter(request));
97 std::copy(meas, meas + meas_len, std::back_inserter(request));
98
99 Log.traceln("Length of request: %d", request.size());
100 int bytes_written = client.write(request.data(), request.size());
101 Log.noticeln("Wrote %d bytes to server", bytes_written);
102
103 return bytes_written;
104}
105
107 // wait until there's bytes available with timeout
108 unsigned int timeout = millis() + g_resp_timeout;
109 while (!client.available()) {
110 // timeout
111 if (millis() > timeout) {
112 Log.warningln("Response timeout!");
113 Log.warningln("WiFi status: %d", WiFi.status());
114 HttpClient empty_resp;
115
116 client.flush();
117 client.stop();
118
119 return empty_resp;
120 }
121
122 // Log.traceln("Delaying until available");
123 delay(100);
124 }
125
126 // read response
127 std::string resp;
128 while (client.available()) {
129 char c = client.read();
130 resp += c;
131 }
132 Log.verboseln("%s", resp.c_str());
133 // close connection
134 client.flush();
135 client.stop();
136
137 // read string into an object
138 HttpClient http_client(resp);
139
140 return http_client;
141}
unsigned int Check()
Health check for API endpoint.
Definition dirtviz.cpp:40
~Dirtviz()
Frees all memory.
Definition dirtviz.cpp:31
void SetUrl(const char *url)
Set new URL string.
Definition dirtviz.cpp:33
unsigned int SendMeasurement(const uint8_t *meas, size_t meas_len)
Send serialized measurement to the API.
Definition dirtviz.cpp:64
Dirtviz(void)
Construct a new Dirtviz object.
Definition dirtviz.cpp:27
HttpClient GetResponse()
Get the response from the server.
Definition dirtviz.cpp:106
HTTP request parser library.
Definition http.hpp:28
const size_t g_request_size
Definition dirtviz.cpp:21
const char g_user_agent[]
Definition dirtviz.cpp:25
unsigned int g_resp_timeout
Definition dirtviz.cpp:18
uint8_t buffer[32]