#include <WiFi.h>
#include <HTTPClient.h>
void setup(void)
{
Serial.begin(115200);
// No connection to wifi, pins issue
WiFi.begin("Wokwi-GUEST", "", 6);
Serial.print("Connecting to WiFi ");
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.println(" Connected!");
}
String httpGETRequest(const char* serverName) {
HTTPClient http;
// Your Domain name with URL path or IP address with path
http.begin(serverName);
// Send HTTP GET request
// Read the id in the django using request.header["Id"] in a view
http.addHeader("id","001");
int httpResponseCode = http.GET();
String payload = "--";
if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
payload = http.getString();
Serial.println(payload);
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
return payload;
}
String httpPOSTRequest(const char* serverName) {
HTTPClient http;
// Your Domain name with URL path or IP address with path
http.begin(serverName);
// Send HTTP GET request
// Read the id in the django using request.header["Id"] in a view
http.addHeader("id","001");
int httpResponseCode = http.POST("the data");
String payload = "--";
if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
payload = http.getString();
Serial.println(payload);
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
return payload;
}
void loop(void)
{
httpPOSTRequest("http://censissh08.pythonanywhere.com/add-record/");// - Doesn't work on localhost
while(1);
}