/*This code assumes you have connected the RX pin of the ESP8266 module to pin 2 (ESP8266_RX) of the Arduino Uno and the TX pin of the ESP8266 module to pin 3 (ESP8266_TX) of the Arduino Uno. Also, replace "yourserver.com" with the address of your server.
On the server side, you would need to have a script or service listening for incoming HTTP requests and processing the data sent by the Arduino via the ESP8266 module.
Remember, this is a basic example, and you may need to adjust it based on your specific requirements and the ESP8266 module's configuration.*/
#include <SoftwareSerial.h>
#define ESP8266_RX 2 // Arduino RX pin connected to ESP8266 TX pin
#define ESP8266_TX 3 // Arduino TX pin connected to ESP8266 RX pin
SoftwareSerial esp8266(ESP8266_RX, ESP8266_TX);
void setup() {
Serial.begin(9600);
esp8266.begin(9600); // Initialize ESP8266 communication
delay(1000); // Allow time for ESP8266 to initialize
}
void loop() {
// Collect sensor data
float temperature = readTemperature(); // Function to read temperature sensor value
float humidity = readHumidity(); // Function to read humidity sensor value
// Send data to ESP8266
sendDataToESP8266(temperature, humidity);
delay(5000); // Adjust delay as needed
}
void sendDataToESP8266(float temperature, float humidity) {
// Construct HTTP request
String request = "GET /update?temperature=";
request += temperature;
request += "&humidity=";
request += humidity;
request += " HTTP/1.1\r\n";
request += "Host: yourserver.com\r\n";
request += "Connection: close\r\n\r\n";
// Send HTTP request to ESP8266
esp8266.println("AT+CIPSTART=\"TCP\",\"yourserver.com\",80");
delay(1000);
if(esp8266.find("OK")) {
Serial.println("Connected to server");
esp8266.println("AT+CIPSEND=" + String(request.length()));
delay(1000);
if(esp8266.find(">")) {
Serial.println("Sending data to server");
esp8266.print(request);
if(esp8266.find("SEND OK")) {
Serial.println("Data sent successfully");
} else {
Serial.println("Failed to send data");
}
}
}
}
float readTemperature() {
// Function to read temperature sensor value
// Replace with your temperature sensor reading code
return 25.0; // Example temperature value
}
float readHumidity() {
// Function to read humidity sensor value
// Replace with your humidity sensor reading code
return 50.0; // Example humidity value
}