#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "KONSELING";
const char* password = "";
String serverName = "http://192.168.80.101:5000";
WiFiClient espClient;
void setup_wifi() { //perintah koneksi wifi
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA); //setting wifi chip sebagai station/client
WiFi.begin(ssid, password); //koneksi ke jaringan wifi
while (WiFi.status() != WL_CONNECTED) { //perintah tunggu esp32 sampi terkoneksi ke wifi
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32-S2!!!");
setup_wifi();
}
void loop() {
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
HTTPClient http;
String url = serverName + "/submit" + "?temp=24.37";
http.begin(url);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
String payload = http.getString();
Serial.println(payload);
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
}
void send_using_post(){
HTTPClient http;
String url = serverName + "/post";
http.begin(url.c_str());
String httpRequestData = "{\"hum\": 80.4,\"temp\": 31.2}";
int httpResponseCode = http.POST(httpRequestData);
if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
String payload = http.getString();
Serial.println(payload);
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
}