me la pela
This commit is contained in:
@@ -6,34 +6,35 @@ import io.vertx.core.impl.logging.LoggerFactory;
|
|||||||
public class Constants {
|
public class Constants {
|
||||||
public static final String APP_NAME = "ContaminUS";
|
public static final String APP_NAME = "ContaminUS";
|
||||||
public static final String API_PREFIX = "/api/v1";
|
public static final String API_PREFIX = "/api/v1";
|
||||||
|
public static final String RAW_API_PREFIX = "/api/raw/v1";
|
||||||
public static Logger LOGGER = LoggerFactory.getLogger(Constants.APP_NAME);
|
public static Logger LOGGER = LoggerFactory.getLogger(Constants.APP_NAME);
|
||||||
|
|
||||||
|
|
||||||
/* API Endpoints */
|
/* API Endpoints */
|
||||||
public static final String GET_GROUPS = API_PREFIX + "/groups";
|
public static final String GET_GROUPS = RAW_API_PREFIX + "/groups";
|
||||||
public static final String POST_GROUPS = API_PREFIX + "/groups";
|
public static final String POST_GROUPS = RAW_API_PREFIX + "/groups";
|
||||||
public static final String PUT_GROUP_BY_ID = API_PREFIX + "/groups/:groupId";
|
public static final String PUT_GROUP_BY_ID = RAW_API_PREFIX + "/groups/:groupId";
|
||||||
|
|
||||||
public static final String GET_DEVICES = API_PREFIX + "/devices";
|
public static final String GET_DEVICES = RAW_API_PREFIX + "/devices";
|
||||||
public static final String POST_DEVICES = API_PREFIX + "/devices";
|
public static final String POST_DEVICES = RAW_API_PREFIX + "/devices";
|
||||||
public static final String PUT_DEVICE_BY_ID = API_PREFIX + "/devices/:deviceId";
|
public static final String PUT_DEVICE_BY_ID = RAW_API_PREFIX + "/devices/:deviceId";
|
||||||
|
|
||||||
public static final String GET_SENSORS = API_PREFIX + "/sensors";
|
public static final String GET_SENSORS = RAW_API_PREFIX + "/sensors";
|
||||||
public static final String POST_SENSORS = API_PREFIX + "/sensors";
|
public static final String POST_SENSORS = RAW_API_PREFIX + "/sensors";
|
||||||
public static final String PUT_SENSOR_BY_ID = API_PREFIX + "/sensors/:sensorId";
|
public static final String PUT_SENSOR_BY_ID = RAW_API_PREFIX + "/sensors/:sensorId";
|
||||||
|
|
||||||
public static final String GET_ACTUATORS = API_PREFIX + "/actuators";
|
public static final String GET_ACTUATORS = RAW_API_PREFIX + "/actuators";
|
||||||
public static final String POST_ACTUATORS = API_PREFIX + "/actuators";
|
public static final String POST_ACTUATORS = RAW_API_PREFIX + "/actuators";
|
||||||
public static final String PUT_ACTUATOR_BY_ID = API_PREFIX + "/actuators/:actuatorId";
|
public static final String PUT_ACTUATOR_BY_ID = RAW_API_PREFIX + "/actuators/:actuatorId";
|
||||||
|
|
||||||
public static final String GET_CO_BY_DEVICE_VIEW = API_PREFIX + "/v_co_by_device";
|
public static final String GET_CO_BY_DEVICE_VIEW = RAW_API_PREFIX + "/v_co_by_device";
|
||||||
|
|
||||||
public static final String GET_GPS_BY_DEVICE_VIEW = API_PREFIX + "/v_gps_by_device";
|
public static final String GET_GPS_BY_DEVICE_VIEW = RAW_API_PREFIX + "/v_gps_by_device";
|
||||||
public static final String GET_LATEST_VALUES_VIEW = API_PREFIX + "/v_latest_values";
|
public static final String GET_LATEST_VALUES_VIEW = RAW_API_PREFIX + "/v_latest_values";
|
||||||
public static final String GET_POLLUTION_MAP_VIEW = API_PREFIX + "/v_pollution_map";
|
public static final String GET_POLLUTION_MAP_VIEW = RAW_API_PREFIX + "/v_pollution_map";
|
||||||
public static final String GET_SENSOR_HISTORY_BY_DEVICE_VIEW = API_PREFIX + "/v_sensor_history_by_device";
|
public static final String GET_SENSOR_HISTORY_BY_DEVICE_VIEW = RAW_API_PREFIX + "/v_sensor_history_by_device";
|
||||||
public static final String GET_SENSOR_VALUES_VIEW = API_PREFIX + "/v_sensor_values";
|
public static final String GET_SENSOR_VALUES_VIEW = RAW_API_PREFIX + "/v_sensor_values";
|
||||||
public static final String GET_WEATHER_BY_DEVICE_VIEW = API_PREFIX + "/v_weather_by_device";
|
public static final String GET_WEATHER_BY_DEVICE_VIEW = RAW_API_PREFIX + "/v_weather_by_device";
|
||||||
|
|
||||||
/* Bussiness Logic API */
|
/* Bussiness Logic API */
|
||||||
public static final String GET_GROUP_BY_ID = API_PREFIX + "/groups/:groupId";
|
public static final String GET_GROUP_BY_ID = API_PREFIX + "/groups/:groupId";
|
||||||
|
|||||||
11
hardware/.vscode/settings.json
vendored
Normal file
11
hardware/.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
{
|
||||||
|
"files.associations": {
|
||||||
|
"array": "cpp",
|
||||||
|
"*.tcc": "cpp",
|
||||||
|
"memory": "cpp",
|
||||||
|
"istream": "cpp",
|
||||||
|
"functional": "cpp",
|
||||||
|
"tuple": "cpp",
|
||||||
|
"utility": "cpp"
|
||||||
|
}
|
||||||
|
}
|
||||||
46
hardware/include/JsonTools.hpp
Normal file
46
hardware/include/JsonTools.hpp
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
#include <ArduinoJson.h>
|
||||||
|
#include <HTTPClient.h>
|
||||||
|
|
||||||
|
String serializeSensorValue (
|
||||||
|
int sensorId,
|
||||||
|
int deviceId,
|
||||||
|
String sensorType,
|
||||||
|
String unit,
|
||||||
|
int sensorStatus,
|
||||||
|
float temperature,
|
||||||
|
float humidity,
|
||||||
|
float carbonMonoxide,
|
||||||
|
float lat,
|
||||||
|
float lon,
|
||||||
|
long timestamp
|
||||||
|
);
|
||||||
|
|
||||||
|
String serializeActuatorStatus (
|
||||||
|
int actuatorId,
|
||||||
|
int deviceId,
|
||||||
|
int status,
|
||||||
|
long timestamp
|
||||||
|
);
|
||||||
|
|
||||||
|
String serializeDevice (
|
||||||
|
int sensorId,
|
||||||
|
int deviceId,
|
||||||
|
String sensorType,
|
||||||
|
int status,
|
||||||
|
long timestamp
|
||||||
|
);
|
||||||
|
|
||||||
|
void deserializeSensorValue (
|
||||||
|
HTTPClient &http,
|
||||||
|
int httpResponseCode
|
||||||
|
);
|
||||||
|
|
||||||
|
void deserializeActuatorStatus (
|
||||||
|
HTTPClient &http,
|
||||||
|
int httpResponseCode
|
||||||
|
);
|
||||||
|
|
||||||
|
void deserializeDevice (
|
||||||
|
HTTPClient &http,
|
||||||
|
int httpResponseCode
|
||||||
|
);
|
||||||
4
hardware/include/RestClient.hpp
Normal file
4
hardware/include/RestClient.hpp
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
#include <HTTPClient.h>
|
||||||
|
|
||||||
|
void getRequest(HTTPClient &httpClient, const String url, String &response);
|
||||||
|
void postRequest(HTTPClient &httpClient, const String url, const String &payload, String &response);
|
||||||
7
hardware/include/WifiConnection.hpp
Normal file
7
hardware/include/WifiConnection.hpp
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
#include <WiFi.h>
|
||||||
|
#include <PubSubClient.h>
|
||||||
|
|
||||||
|
#define SSID "iPhone de Álvaro"
|
||||||
|
#define PASSWORD "alvarito123"
|
||||||
|
|
||||||
|
int setupWifi();
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
<<<<<<< HEAD
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
|
||||||
#include "json.hpp"
|
#include "json.hpp"
|
||||||
@@ -10,3 +11,13 @@
|
|||||||
#define SERVER_IP "192.168.1.178"
|
#define SERVER_IP "192.168.1.178"
|
||||||
#define REST_PORT 80
|
#define REST_PORT 80
|
||||||
#define MQTT_PORT 1883
|
#define MQTT_PORT 1883
|
||||||
|
=======
|
||||||
|
#include <Arduino.h>
|
||||||
|
|
||||||
|
#include "JsonTools.hpp"
|
||||||
|
#include "RestClient.hpp"
|
||||||
|
#include "WifiConnection.hpp"
|
||||||
|
#include "MqttClient.hpp"
|
||||||
|
|
||||||
|
uint32_t getChipID();
|
||||||
|
>>>>>>> main
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
#include <HTTPClient.h>
|
|
||||||
179
hardware/src/JsonTools.cpp
Normal file
179
hardware/src/JsonTools.cpp
Normal file
@@ -0,0 +1,179 @@
|
|||||||
|
#include "JsonTools.hpp"
|
||||||
|
|
||||||
|
String serializeSensorValue(int sensorId, int deviceId, String sensorType, String unit, int sensorStatus, float temperature, float humidity, float carbonMonoxide, float lat, float lon, long timestamp)
|
||||||
|
{
|
||||||
|
DynamicJsonDocument doc(2048);
|
||||||
|
|
||||||
|
doc["sensorId"] = sensorId;
|
||||||
|
doc["deviceId"] = deviceId;
|
||||||
|
doc["sensorType"] = sensorType;
|
||||||
|
doc["unit"] = unit;
|
||||||
|
doc["sesnsorStatuts"] = sensorStatus;
|
||||||
|
doc["temperature"] = temperature;
|
||||||
|
doc["humidity"] = humidity;
|
||||||
|
doc["carbonMonoxide"] = carbonMonoxide;
|
||||||
|
doc["lat"] = lat;
|
||||||
|
doc["lon"] = lon;
|
||||||
|
doc["timestamp"] = timestamp;
|
||||||
|
|
||||||
|
String output;
|
||||||
|
serializeJson(doc, output);
|
||||||
|
Serial.println(output);
|
||||||
|
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
String serializeActuatorStatus (int actuatorId, int deviceId, int status, long timestamp)
|
||||||
|
{
|
||||||
|
DynamicJsonDocument doc(2048);
|
||||||
|
|
||||||
|
doc["actuatorId"] = actuatorId;
|
||||||
|
doc["deviceId"] = deviceId;
|
||||||
|
doc["status"] = status;
|
||||||
|
doc["timestamp"] = timestamp;
|
||||||
|
|
||||||
|
String output;
|
||||||
|
serializeJson(doc, output);
|
||||||
|
Serial.println(output);
|
||||||
|
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
String serializeDevice(int sensorId, int deviceId, String sensorType, int status, long timestamp)
|
||||||
|
{
|
||||||
|
DynamicJsonDocument doc(2048);
|
||||||
|
|
||||||
|
doc["sensorId"] = sensorId;
|
||||||
|
doc["deviceId"] = deviceId;
|
||||||
|
doc["sensorType"] = sensorType;
|
||||||
|
doc["status"] = status;
|
||||||
|
doc["timestamp"] = timestamp;
|
||||||
|
|
||||||
|
String output;
|
||||||
|
serializeJson(doc, output);
|
||||||
|
Serial.println(output);
|
||||||
|
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
void deserializeSensorValue (HTTPClient &http, int httpResponseCode)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (httpResponseCode > 0)
|
||||||
|
{
|
||||||
|
Serial.print("HTTP Response code: ");
|
||||||
|
Serial.println(httpResponseCode);
|
||||||
|
String responseJson = http.getString();
|
||||||
|
DynamicJsonDocument doc(ESP.getMaxAllocHeap());
|
||||||
|
DeserializationError error = deserializeJson(doc, responseJson);
|
||||||
|
|
||||||
|
if (error)
|
||||||
|
{
|
||||||
|
Serial.print(F("deserializeJson() failed: "));
|
||||||
|
Serial.println(error.f_str());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
JsonArray array = doc.as<JsonArray>();
|
||||||
|
for (JsonObject sensor : array)
|
||||||
|
{
|
||||||
|
int sensorId = sensor["sensorId"];
|
||||||
|
int deviceId = sensor["deviceId"];
|
||||||
|
String sensorType = sensor["sensorType"];
|
||||||
|
String unit = sensor["unit"];
|
||||||
|
int sesnsorStatuts = sensor["sesnsorStatuts"];
|
||||||
|
float temperature = sensor["temperature"];
|
||||||
|
float humidity = sensor["humidity"];
|
||||||
|
float carbonMonoxide = sensor["carbonMonoxide"];
|
||||||
|
float lat = sensor["lat"];
|
||||||
|
float lon = sensor["lon"];
|
||||||
|
long timestamp = sensor["timestamp"];
|
||||||
|
|
||||||
|
Serial.println(("Sensor deserialized: [sensorId: " + String(sensorId) + ", deviceId: " + String(deviceId) + ", sensorType: " + sensorType + ", unit: " + unit +", sesnsorStatuts: " + String(sesnsorStatuts) +", temperature: " + String(temperature) +", humidity: " + String(humidity) +", carbonMonoxide: " + String(carbonMonoxide) +", lat: " + String(lat) +", lon: " + String(lon) +", timestamp: " + String(timestamp) + "]").c_str());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Serial.print("Error code: ");
|
||||||
|
Serial.println(httpResponseCode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void deserializeActuatorStatus (HTTPClient &http, int httpResponseCode)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (httpResponseCode > 0)
|
||||||
|
{
|
||||||
|
Serial.print("HTTP Response code: ");
|
||||||
|
Serial.println(httpResponseCode);
|
||||||
|
String responseJson = http.getString();
|
||||||
|
DynamicJsonDocument doc(ESP.getMaxAllocHeap());
|
||||||
|
DeserializationError error = deserializeJson(doc, responseJson);
|
||||||
|
|
||||||
|
if (error)
|
||||||
|
{
|
||||||
|
Serial.print(F("deserializeJson() failed: "));
|
||||||
|
Serial.println(error.f_str());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
JsonArray array = doc.as<JsonArray>();
|
||||||
|
for (JsonObject actuator : array)
|
||||||
|
{
|
||||||
|
int actuadorId = actuator["actuadorId"];
|
||||||
|
int deviceId = actuator["deviceId"];
|
||||||
|
int statuts = actuator["statuts"];
|
||||||
|
long timestamp = actuator["timestamp"];
|
||||||
|
|
||||||
|
Serial.println(("Actuador deserialized: [actuadorId: " + String(actuadorId) +
|
||||||
|
", deviceId: " + String(deviceId) +
|
||||||
|
", statuts: " + String(statuts) +
|
||||||
|
", timestamp: " + String(timestamp) + "]").c_str());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Serial.print("Error code: ");
|
||||||
|
Serial.println(httpResponseCode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void deserializeDevice (HTTPClient &http, int httpResponseCode)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (httpResponseCode > 0)
|
||||||
|
{
|
||||||
|
Serial.print("HTTP Response code: ");
|
||||||
|
Serial.println(httpResponseCode);
|
||||||
|
String responseJson = http.getString();
|
||||||
|
DynamicJsonDocument doc(ESP.getMaxAllocHeap());
|
||||||
|
DeserializationError error = deserializeJson(doc, responseJson);
|
||||||
|
|
||||||
|
if (error)
|
||||||
|
{
|
||||||
|
Serial.print(F("deserializeJson() failed: "));
|
||||||
|
Serial.println(error.f_str());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
JsonArray array = doc.as<JsonArray>();
|
||||||
|
for (JsonObject device : array)
|
||||||
|
{
|
||||||
|
int sensorId = device["sensorId"];
|
||||||
|
int deviceId = device["deviceId"];
|
||||||
|
String sensorType = device["sensorType"];
|
||||||
|
long timestamp = device["timestamp"];
|
||||||
|
|
||||||
|
Serial.println(("Sensor deserialized: [sensorId: " + String(sensorId) +
|
||||||
|
", deviceId: " + String(deviceId) +
|
||||||
|
", sensorType: " + sensorType +
|
||||||
|
", timestamp: " + String(timestamp) + "]").c_str());
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Serial.print("Error code: ");
|
||||||
|
Serial.println(httpResponseCode);
|
||||||
|
}
|
||||||
|
}
|
||||||
1
hardware/src/MqttClient.cpp
Normal file
1
hardware/src/MqttClient.cpp
Normal file
@@ -0,0 +1 @@
|
|||||||
|
#include "MqttClient.hpp"
|
||||||
27
hardware/src/RestClient.cpp
Normal file
27
hardware/src/RestClient.cpp
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
#include "RestClient.hpp"
|
||||||
|
|
||||||
|
|
||||||
|
void getRequest(HTTPClient &httpClient, const String url, String &response)
|
||||||
|
{
|
||||||
|
httpClient.begin(url);
|
||||||
|
int httpCode = httpClient.GET();
|
||||||
|
if (httpCode > 0) {
|
||||||
|
response = httpClient.getString();
|
||||||
|
} else {
|
||||||
|
response = "Error: " + String(httpCode);
|
||||||
|
}
|
||||||
|
httpClient.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
void postRequest(HTTPClient &httpClient, const String url, String &payload, String &response)
|
||||||
|
{
|
||||||
|
httpClient.begin(url);
|
||||||
|
httpClient.addHeader("Content-Type", "application/json");
|
||||||
|
int httpCode = httpClient.POST(payload);
|
||||||
|
if (httpCode > 0) {
|
||||||
|
response = httpClient.getString();
|
||||||
|
} else {
|
||||||
|
response = "Error: " + String(httpCode);
|
||||||
|
}
|
||||||
|
httpClient.end();
|
||||||
|
}
|
||||||
@@ -1,14 +1,12 @@
|
|||||||
#include <wifi.hpp>
|
#include <WifiConnection.hpp>
|
||||||
|
|
||||||
WiFiClient espClient;
|
WiFiClient wifiClient;
|
||||||
PubSubClient client(espClient);
|
|
||||||
|
|
||||||
int setup_wifi()
|
int setupWifi()
|
||||||
{
|
{
|
||||||
Serial.println();
|
Serial.println();
|
||||||
Serial.print("Connecting to ");
|
Serial.print("Connecting to ");
|
||||||
Serial.println(SSID);
|
Serial.println(SSID);
|
||||||
|
|
||||||
WiFi.mode(WIFI_STA);
|
WiFi.mode(WIFI_STA);
|
||||||
WiFi.begin(SSID, PASSWORD);
|
WiFi.begin(SSID, PASSWORD);
|
||||||
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
#include "json.hpp"
|
|
||||||
@@ -1,12 +1,30 @@
|
|||||||
#include "main.hpp"
|
#include "main.hpp"
|
||||||
|
|
||||||
|
const uint32_t deviceId = getChipID();
|
||||||
|
String response;
|
||||||
|
HTTPClient httpClient;
|
||||||
|
|
||||||
|
uint32_t getChipID()
|
||||||
|
{
|
||||||
|
uint32_t chipId;
|
||||||
|
for (int i = 0; i < 17; i = i + 8) {
|
||||||
|
chipId |= ((ESP.getEfuseMac() >> (40 - i)) & 0xff) << i;
|
||||||
|
}
|
||||||
|
return chipId;
|
||||||
|
}
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
|
|
||||||
if(setup_wifi() != 0)
|
// WiFi Connection
|
||||||
|
if(setupWifi() != 0)
|
||||||
{
|
{
|
||||||
Serial.print("Error connecting to WiFI");
|
Serial.print("Error connecting to WiFi");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// test get
|
||||||
|
getRequest(httpClient, "http://172.20.10.7:8082/api/v1/sensors/1/values", response);
|
||||||
|
deserializeSensorValue(httpClient, httpClient.GET());
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
#include "test.hpp"
|
|
||||||
Reference in New Issue
Block a user