Hi,
I’m very new to the world of Arduino, C++ and networking so please excuse my ignorance, but I hope someone will be able to point out to me what’s I’m doing wrong in a sketch I’m trying to write to post DHT22 sensor data to a php script (from where it will be inserted into a MySQL database).
The code I have so far is constructed from a couple of tutorials (ssid, passwords and domains have been masked):
#include <SoftwareSerial.h>
#include <SparkFunESP8266WiFi.h>
#include <SparkFunESP8266Client.h>
#include <DHT.h>
#define DHT1PIN 2
#define DHTTYPE DHT22
DHT dht1(DHT1PIN, DHTTYPE);
char mySSID[] = "ssid";
char myPSK[] = "ssidpassword";
const char destServer[] = "domain.com";
const char server[] = "http://www.domain.com";
float humidityData;
float temperatureData;
ESP8266Client client;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
dht1.begin();
if (esp8266.begin())
Serial.println("ESP8266 ready to go!");
else
Serial.println("Unable to communicate with the ESP8266");
int retVal;
retVal = esp8266.connect("ssid", "ssidpassword");
if (retVal < 0)
{
Serial.print(F("Error connecting: "));
Serial.println(retVal);
}
retVal = client.connect(server, 80);
if (retVal > 0)
Serial.println("Successfully connected");
IPAddress myIP = esp8266.localIP();
Serial.print(F("My IP is: ")); Serial.println(myIP);
Serial.print("Pinging ");
Serial.println(destServer);
}
void loop() {
// put your main code here, to run repeatedly:
humidityData = dht1.readHumidity();
temperatureData = dht1.readTemperature();
Sending_To_phpmyadmindatabase();
delay(10000); // interval
Serial.print("ping ");
Serial.println(esp8266.ping(destServer));
delay(1000);
}
void Sending_To_phpmyadmindatabase() //CONNECTING WITH MYSQL
{
if (client.connect("http://www.domain.com",80)) {
Serial.println("connected");
// Make a HTTP request:
Serial.print("POST /data/post.php?humidity=");
client.print("POST /data/post.php?humidity=");
Serial.print(humidityData);
client.print(humidityData);
client.print("&temperature=");
Serial.print("&temperature=");
client.print(temperatureData);
Serial.println(temperatureData);
client.print(" "); //SPACE BEFORE HTTP/1.1
client.println("HTTP/1.1");
client.println("Host: www.domain.com");
client.println("Content-Type: application/json");
client.println("Content-Length: 0");
client.println();
} else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
}
I’ve confirmed that my server is able to accept POST requests, I’ve checked that the post.php script is working and inserting the query into the database. The serial monitor prints exactly what I would expect to see as the query:
POST /data/post.php?humidity=61.00&temperature=30.00
The issue really seems to be with this Sketch not passing the query to the post.php and I’ve spent almost two days trying to figure it out!
Any help at all would be massively appreciated!