1
0

http requests from esp32 to api

This commit is contained in:
Jose
2025-04-09 14:19:52 +02:00
parent 0c03c052b4
commit 245f213ad9
15 changed files with 72 additions and 37 deletions

11
hardware/.vscode/settings.json vendored Normal file
View File

@@ -0,0 +1,11 @@
{
"files.associations": {
"array": "cpp",
"*.tcc": "cpp",
"memory": "cpp",
"istream": "cpp",
"functional": "cpp",
"tuple": "cpp",
"utility": "cpp"
}
}

View File

@@ -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
); );

View 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);

View File

@@ -0,0 +1,7 @@
#include <WiFi.h>
#include <PubSubClient.h>
#define SSID "iPhone de Álvaro"
#define PASSWORD "alvarito123"
int setupWifi();

View File

@@ -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();

View File

@@ -1,4 +0,0 @@
#include <HTTPClient.h>
#define SERVER_IP "192.168.48.151"
#define SERVER_PORT 80

View File

@@ -1,7 +0,0 @@
#include <WiFi.h>
#include <PubSubClient.h>
#define SSID "Redmi Note 14 Pro 5G"
#define PASSWORD "aitv5949"
int setup_wifi();

View File

@@ -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)

View File

@@ -0,0 +1 @@
#include "MqttClient.hpp"

View 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();
}

View File

@@ -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);

View File

@@ -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() {
} }

View File

@@ -1 +0,0 @@
#include "mqtt.hpp"

View File

@@ -1,5 +0,0 @@
#include "rest.hpp"
HTTPClient httpClient;