#include <WiFi.h>
#include <HTTPClient.h>
#include <DHTesp.h>
#define DHTPIN 19
DHTesp dht;
String server="https://bipfunctionns2024.azurewebsites.net/api/"; // Cloud server address
const char *api_key = "ji84HUd93hvnjHIEidfhiD93iash8ahbjjfdKJ389f74dsjkrvHJE=="; // authorization key
#define BUF_SIZE 200
char buffer[BUF_SIZE];
const char *payload =
"{ \
\"sender\": \"tibor\", \
\"recipient\": \"telemetry-tibor\", \
\"messageText\": \"temperature: %.1f C humidity: %.1f%%\" \
}";
int lastmillis;
int lastmillis2;
void send_message(float temperature, float humidity) {
HTTPClient http;
String url = server + "Message";
Serial.println(url);
http.begin(url);
http.addHeader("x-functions-key", api_key, true, true);
Serial.print("[HTTPS] POST...\n");
// start connection and send HTTP request
snprintf(buffer, BUF_SIZE, payload, temperature, (char)176, humidity);
Serial.println(buffer);
int httpCode = http.POST(buffer);
// httpCode will be negative on error
if(httpCode > 0) {
// HTTP request has been sent and the server response header has been received
Serial.printf("[HTTPS] POST... code: %d\n", httpCode);
if(httpCode == HTTP_CODE_OK) {
Serial.println("POST was successful. This is the response:");
String response = http.getString();
Serial.println(response);
}
} else {
Serial.printf("[HTTPS] POST... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
dht.setup(DHTPIN, DHTesp::DHT22);
Serial.println("Cloud send temperature");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) { // Wait for connection
if ((millis()-lastmillis)>500) {
Serial.println("Connecting to WiFi..");
lastmillis=millis();
}
}
Serial.print("Connected to the WiFi AP. My address is: ");
Serial.println(WiFi.localIP());
lastmillis2=millis();
TempAndHumidity th = dht.getTempAndHumidity();
send_message(th.temperature, th.humidity);
}
void loop() {
if ((millis()-lastmillis2)>20000) {
lastmillis2=millis();
TempAndHumidity th = dht.getTempAndHumidity();
send_message(th.temperature, th.humidity);
}
}