#include <SoftwareSerial.h>
// Define ESP8266 communication pins
#define ESP8266_RX 2
#define ESP8266_TX 3
// Create a SoftwareSerial object
SoftwareSerial esp8266(ESP8266_RX, ESP8266_TX);
// Replace with your Wi-Fi credentials
const char* ssid = "Tumhary leya nhi hy ";
const char* password = "87654321";
// Replace with your Google Sheets script URL
const char* google_sheets_url = "https://script.google.com/macros/s/YOUR_NEW_DEPLOYMENT_URL/exec"; // Update this URL
// Replace with the data you want to send
const char* data_to_send = "resistor=999"; // Ensure the data is formatted correctly for POST
void setup() {
Serial.begin(115200);
esp8266.begin(115200);
// Initialize ESP8266
esp8266.println("AT+RST");
delay(2000);
// Connect to Wi-Fi
esp8266.println("AT+CWMODE=1");
delay(1000);
esp8266.print("AT+CWJAP="");
esp8266.print(ssid);
esp8266.print("","");
esp8266.print(password);
esp8266.println(""");
delay(5000);
// Send data to Google Sheets
sendDataToSheets();
}
void loop() {
// You can add code here to send data periodically or based on sensor readings
delay(5000);
sendDataToSheets();
}
void sendDataToSheets() {
// Start TCP connection
esp8266.print("AT+CIPSTART="TCP","script.google.com",80\r\n");
delay(2000);
// Create the HTTP request (POST method)
String httpRequest = String("POST /macros/s/YOUR_NEW_DEPLOYMENT_URL/exec HTTP/1.1\r\n") + // Update this URL
String("Host: script.google.com\r\n") +
String("Connection: close\r\n") +
String("Content-Type: application/x-www-form-urlencoded\r\n") +
String("Content-Length: ") + String(strlen(data_to_send)) + "\r\n\r\n") +
String(data_to_send);
// Send the HTTP request length
esp8266.print("AT+CIPSEND=");
esp8266.print(httpRequest.length());
delay(1000);
// Send the HTTP request
esp8266.print(httpRequest);
delay(2000);
// Read response from server
while (esp8266.available()) {
Serial.write(esp8266.read());
}
// Close connection
esp8266.println("AT+CIPCLOSE");
delay(1000);
Serial.println("Data sent to Google Sheets");
}