http requests from esp32 to api
This commit is contained in:
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"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -31,16 +31,16 @@ String serializeDevice (
|
|||||||
);
|
);
|
||||||
|
|
||||||
void deserializeSensorValue (
|
void deserializeSensorValue (
|
||||||
HTTPClient* http,
|
HTTPClient &http,
|
||||||
int httpResponseCode
|
int httpResponseCode
|
||||||
);
|
);
|
||||||
|
|
||||||
void deserializeActuatorStatus (
|
void deserializeActuatorStatus (
|
||||||
HTTPClient* http,
|
HTTPClient &http,
|
||||||
int httpResponseCode
|
int httpResponseCode
|
||||||
);
|
);
|
||||||
|
|
||||||
void deserializeDevice (
|
void deserializeDevice (
|
||||||
HTTPClient* http,
|
HTTPClient &http,
|
||||||
int httpResponseCode
|
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,9 +1,8 @@
|
|||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
|
||||||
#include "json.hpp"
|
#include "JsonTools.hpp"
|
||||||
#include "rest.hpp"
|
#include "RestClient.hpp"
|
||||||
#include "wifi.hpp"
|
#include "WifiConnection.hpp"
|
||||||
#include "test.hpp"
|
#include "MqttClient.hpp"
|
||||||
#include "mqtt.hpp"
|
|
||||||
|
|
||||||
uint32_t getChipID();
|
uint32_t getChipID();
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
#include <HTTPClient.h>
|
|
||||||
|
|
||||||
#define SERVER_IP "192.168.48.151"
|
|
||||||
#define SERVER_PORT 80
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
#include <WiFi.h>
|
|
||||||
#include <PubSubClient.h>
|
|
||||||
|
|
||||||
#define SSID "Redmi Note 14 Pro 5G"
|
|
||||||
#define PASSWORD "aitv5949"
|
|
||||||
|
|
||||||
int setup_wifi();
|
|
||||||
@@ -1,6 +1,4 @@
|
|||||||
#include "json.hpp"
|
#include "JsonTools.hpp"
|
||||||
|
|
||||||
String response;
|
|
||||||
|
|
||||||
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 serializeSensorValue(int sensorId, int deviceId, String sensorType, String unit, int sensorStatus, float temperature, float humidity, float carbonMonoxide, float lat, float lon, long timestamp)
|
||||||
{
|
{
|
||||||
@@ -58,7 +56,7 @@ String serializeDevice(int sensorId, int deviceId, String sensorType, int status
|
|||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
void deserializeSensorValue (HTTPClient http, int httpResponseCode)
|
void deserializeSensorValue (HTTPClient &http, int httpResponseCode)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (httpResponseCode > 0)
|
if (httpResponseCode > 0)
|
||||||
@@ -101,7 +99,7 @@ void deserializeSensorValue (HTTPClient http, int httpResponseCode)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void deserializeActuatorStatus (HTTPClient http, int httpResponseCode)
|
void deserializeActuatorStatus (HTTPClient &http, int httpResponseCode)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (httpResponseCode > 0)
|
if (httpResponseCode > 0)
|
||||||
@@ -140,7 +138,7 @@ void deserializeActuatorStatus (HTTPClient http, int httpResponseCode)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void deserializeDevice (HTTPClient http, int httpResponseCode)
|
void deserializeDevice (HTTPClient &http, int httpResponseCode)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (httpResponseCode > 0)
|
if (httpResponseCode > 0)
|
||||||
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,13 +1,12 @@
|
|||||||
#include <wifi.hpp>
|
#include <WifiConnection.hpp>
|
||||||
|
|
||||||
WiFiClient wifiClient;
|
WiFiClient wifiClient;
|
||||||
|
|
||||||
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,6 +1,8 @@
|
|||||||
#include "main.hpp"
|
#include "main.hpp"
|
||||||
|
|
||||||
const uint32_t deviceId = getChipID();
|
const uint32_t deviceId = getChipID();
|
||||||
|
String response;
|
||||||
|
HTTPClient httpClient;
|
||||||
|
|
||||||
uint32_t getChipID()
|
uint32_t getChipID()
|
||||||
{
|
{
|
||||||
@@ -15,12 +17,16 @@ void setup() {
|
|||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
|
|
||||||
// WiFi Connection
|
// WiFi Connection
|
||||||
if(setup_wifi() != 0)
|
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 "mqtt.hpp"
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
#include "rest.hpp"
|
|
||||||
|
|
||||||
HTTPClient httpClient;
|
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user