#if defined(ESP32)
#include <WiFi.h>
#include <WiFiClientSecure.h>
// #include <HTTPClient.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
// #include <ESP8266HTTPClient.h>
#elif defined(CONFIG_PLATFORM_8721D)
#include <WiFi.h>
#include <WiFiClient.h>
// #include <HttpClient.h>
#endif
#define ON_Board_LED 2 //--> Defining an On Board LED, used for indicators when the process of connecting to a wifi router
char ssid[] = "Wokwi-GUEST"; //"********"; //--> Your wifi name or SSID.
char password[] = ""; //"*********"; //--> Your wifi password.
#define host "script.google.com" // API URL
#define port 443 //https
String GSCRIPT_ID = "AKfycbxMjwPXNzdXrWv_sHVLu1oURQuoPP9DQRWAPdYRxRiV7aZFmOG5wuQT_VODvmT7nfUf"; //--> spreadsheet script ID
// Create a URI for the request
String url = "/macros/s/" + GSCRIPT_ID + "/exec";
void wifiConnect(char* Ssid, char* Password);
void printWifiStatus(void);
void sendMessage(String message);
void setup(void) {
// start serial port:
Serial.begin(115200);
pinMode(ON_Board_LED, OUTPUT); //--> On Board LED port Direction output
digitalWrite(ON_Board_LED, HIGH); //--> Turn off Led On Board
Serial.println("Configuring access point...");
wifiConnect(ssid, password);
// you're connected now, so print out the status:
printWifiStatus();
}
void loop(void) {
// Replace with your sensor data
float temp = random(20, 30); // Simulated temperature
float hum = random(40, 60); // Simulated humidity
String message = "{\"temperature\": " + String(temp) + ", \"humidity\": " + String(hum) + "}";
sendMessage(message);
delay(10000);
}
void wifiConnect(char* Ssid, char* Password){
#if !defined CONFIG_PLATFORM_8721D
WiFi.mode(WIFI_STA);
#endif
WiFi.disconnect();
delay(100);
// Attempt to connect to Wifi network:
Serial.print("Attempting to connect to SSID: ");
Serial.println(Ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
WiFi.begin(Ssid, Password);
while( WiFi.status() != WL_CONNECTED) {
// wait 0.5 seconds for connection:
delay(500);
Serial.print(".");
}
Serial.println("");
//--Wait for connection
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
//--Make the On Board Flashing LED on the process of connecting to the wifi router.
digitalWrite(ON_Board_LED, LOW);
delay(250);
digitalWrite(ON_Board_LED, HIGH);
delay(250);
//--
}
}
void printWifiStatus(void) {
// print the SSID of the network you're attached to:
Serial.print("\r\nSSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
Serial.println("WiFi connected");
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.println("signal strength (RSSI):" + String(rssi) + " dBm");
}
void sendMessage(String message) {
#if (defined ESP32) || (defined ESP8266)
WiFiClientSecure client;
client.setInsecure();
#elif defined(CONFIG_PLATFORM_8721D)
WiFiSSLClient client; // Initialize the Wifi client library
#endif
// if there's a successful connection:
if (!client.connect(host, port)) {
Serial.println("Connection failed");
return;
}
String LEN = String(message.length());
Serial.print("Host : "); Serial.println(host); Serial.println();
Serial.println("message=" + message);
Serial.println("message.length()=" + LEN);
String request =
/* Post url */
"POST " + url + " HTTP/1.1\r\n" +
/* Headers */
"Host: " + host + "\r\n" +
"Connection: close\r\n" +
"Content-Length: " + LEN + "\r\n" +
"Content-Type: application/json\r\n\r\n" +
/* Body */
message + "\r\n";
// This will send the request to the server
client.print(request);
Serial.println(request);
delay(50);
String response = client.readString();
Serial.println(String(response) + "Updated!"); //Display the result of responsing
}
//==============================================================================