/*
Web client
This sketch connects to a website (http://www.google.com)
using the WiFi module.
This example is written for a network using WPA encryption. For
WEP or WPA, change the WiFi.begin() call accordingly.
This example is written for a network using WPA encryption. For
WEP or WPA, change the WiFi.begin() call accordingly.
created 13 July 2010
by dlf (Metodo2 srl)
modified 31 May 2012
by Tom Igoe
Find the full UNO R4 WiFi Network documentation here:
https://docs.arduino.cc/tutorials/uno-r4-wifi/wifi-examples#wi-fi-web-client
*/
#include <WiFi.h>
#include <HTTPClient.h>
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
const char *ssid= "Wokwi-GUEST"; // your network SSID (name)
const char *pass = ""; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key index number (needed only for WEP)
int status = WL_IDLE_STATUS;
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
//char server[] = "http://192.168.0.168"; // name address for Google (using DNS)
//IPAddress server(192, 168, 0, 168);
const char* serverUrl = "https://b76b-109-255-165-121.ngrok-free.app/add_data";
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
WiFiClient client;
/* -------------------------------------------------------------------------- */
void printWifiStatus() {
/* -------------------------------------------------------------------------- */
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
}
/* -------------------------------------------------------------------------- */
void setup() {
/* -------------------------------------------------------------------------- */
//Initialize serial and wait for port to open:
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
printWifiStatus();
}
/* just wrap the received data up to 80 columns in the serial print*/
/* -------------------------------------------------------------------------- */
void read_response() {
/* -------------------------------------------------------------------------- */
uint32_t received_data_num = 0;
while (client.available()) {
/* actual data reception */
char c = client.read();
/* print data to serial port */
Serial.print(c);
/* wrap data to 80 columns*/
received_data_num++;
if(received_data_num % 80 == 0) {
Serial.println();
}
}
}
/*
void sendDataToServer() {
// Send POST request
Serial.println("\nStarting connection to server...");
if (client.connect(serverUrl, 80)) {
Serial.println("Connected to server");
int plantID = 3;
int moistureLevel = 45;
String lastWateredTime = "2024-10-30 20:07";
// Create JSON data
String jsonData = String("{\"plantID\":") + plantID +
String(",\"moistureLevel\":") + moistureLevel +
String(",\"lastWateredTime\":\"") + lastWateredTime + "\"}";
// Make a POST request
client.println("POST /add_data HTTP/1.1");
client.println("Host: http://1900-109-255-165-121.ngrok-free.app/"); // Replace with your server address
client.println("Content-Type: application/json");
client.print("Content-Length: ");
client.println(jsonData.length());
client.println("Connection: close");
client.println();
client.println(jsonData);
} else {
Serial.println("Connection failed");
}
}*/
void sendDataToServer() {
int plantID = 4;
int moistureLevel = 192;
String lastWateredTime = "2024-10-30 13:00";
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(serverUrl);
http.addHeader("Content-Type", "application/json"); // Set the content-type header
String postData = "{";
postData += "\"plantID\": " + String(plantID) + ",";
postData += "\"moistureLevel\": " + String(moistureLevel) + ",";
postData += "\"lastWateredTime\": \"" + lastWateredTime + "\"";
postData += "}";
int httpResponseCode = http.POST(postData);
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println("Response: " + response); // Print response from server
} else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
http.end(); // Free resources
} else {
Serial.println("WiFi not connected");
}
}
/* -------------------------------------------------------------------------- */
void loop() {
/* -------------------------------------------------------------------------- */
sendDataToServer();
read_response();
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting from server.");
client.stop();
// do nothing forevermore:
while (true);
}
}