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 (
|
||||
HTTPClient* http,
|
||||
HTTPClient &http,
|
||||
int httpResponseCode
|
||||
);
|
||||
|
||||
void deserializeActuatorStatus (
|
||||
HTTPClient* http,
|
||||
HTTPClient &http,
|
||||
int httpResponseCode
|
||||
);
|
||||
|
||||
void deserializeDevice (
|
||||
HTTPClient* http,
|
||||
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,9 +1,8 @@
|
||||
#include <Arduino.h>
|
||||
|
||||
#include "json.hpp"
|
||||
#include "rest.hpp"
|
||||
#include "wifi.hpp"
|
||||
#include "test.hpp"
|
||||
#include "mqtt.hpp"
|
||||
#include "JsonTools.hpp"
|
||||
#include "RestClient.hpp"
|
||||
#include "WifiConnection.hpp"
|
||||
#include "MqttClient.hpp"
|
||||
|
||||
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"
|
||||
|
||||
String response;
|
||||
#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)
|
||||
{
|
||||
@@ -58,7 +56,7 @@ String serializeDevice(int sensorId, int deviceId, String sensorType, int status
|
||||
return output;
|
||||
}
|
||||
|
||||
void deserializeSensorValue (HTTPClient http, int httpResponseCode)
|
||||
void deserializeSensorValue (HTTPClient &http, int httpResponseCode)
|
||||
{
|
||||
|
||||
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)
|
||||
@@ -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)
|
||||
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;
|
||||
|
||||
int setup_wifi()
|
||||
int setupWifi()
|
||||
{
|
||||
Serial.println();
|
||||
Serial.print("Connecting to ");
|
||||
Serial.println(SSID);
|
||||
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.begin(SSID, PASSWORD);
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#include "main.hpp"
|
||||
|
||||
const uint32_t deviceId = getChipID();
|
||||
String response;
|
||||
HTTPClient httpClient;
|
||||
|
||||
uint32_t getChipID()
|
||||
{
|
||||
@@ -15,10 +17,14 @@ void setup() {
|
||||
Serial.begin(9600);
|
||||
|
||||
// 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() {
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
#include "mqtt.hpp"
|
||||
@@ -1,5 +0,0 @@
|
||||
#include "rest.hpp"
|
||||
|
||||
HTTPClient httpClient;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user