I finally finished building out my new environmental controllers. I am currently using Mega 2560s so these ESP8266s will give me the data collection that I've been wanting for some time now.
I've been learning how to work with php scripts but it hasn't been the easiest for me to grasp. After working through a bunch of mistakes I have it so I can see it on my network. I currently have it set as an access point and when I connect to it with my laptop, it gives me an IP address and I can ping the ESP.
On the PC I have XAMPP setup with Apache and MySQL. Both services are installed and running fine.There are no other apps using the ports(80, 443 and 3306). I've read through the log files, but nothing pops out at me. I created a user in PMA that has full rights to the db and I can login with it just fine in console.
#include <Arduino.h>
#include <Wire.h>
#include <LCD_I2C.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include "Adafruit_SHT31.h"
//#include "ESP8266WebServer.h"
//#include "ESPAsyncWebServer.h"
bool enableHeater = false;
uint8_t loopCnt = 0;
Adafruit_SHT31 sht31 = Adafruit_SHT31();
//#define Addr 0x20
//#define Addr 0x44
//I2C Addresses 0x20(I2C Expander), 0x27(LCD), 0x44(SHT31)
LCD_I2C lcd(0x27, 16, 2); // Default address of most PCF8574 modules, change accordingly
//AsyncWebServer server(80);
// Set AP credentials
#define AP_SSID "Flower_1"
#define AP_PASS "flower123"
#define RELAY_ON 0
#define RELAY_OFF 1
//#define Relay_1 1
//#define Relay_2 2
//#define Relay_3 3
//#define Relay_4 4
//IPAddress local_ip(10, 0, 0, 240);
////IPAddress gateway(10, 0, 0, 240);
//IPAddress gateway(10, 0, 0, 1);
//IPAddress subnet(255, 255, 255, 0);
// Declare global variables which will be uploaded to server
String sendval, sendval2, postData;
void setup() {
Wire.begin();
Serial.begin(115200);
lcd.begin(false);
lcd.backlight();
if (! sht31.begin(0x44)) {
Serial.println("Couldn't find SHT31");
while (1) delay(1);
}
if (sht31.isHeaterEnabled())
delay(300);
//digitalWrite(Relay_1, RELAY_OFF);
//digitalWrite(Relay_2, RELAY_OFF);
//digitalWrite(Relay_3, RELAY_OFF);
//digitalWrite(Relay_4, RELAY_OFF);
//pinMode(Relay_1, OUTPUT);
//pinMode(Relay_2, OUTPUT);
//pinMode(Relay_3, OUTPUT);
//pinMode(Relay_4, OUTPUT);
//delay(300);
WiFi.mode(WIFI_AP);
//name for the access point and 8 character password
WiFi.softAP("flower1", "flower123");
delay(200);
//while (WiFi.softAPgetStationNum() != 1) { //loop here while no AP is connected to this station
//Serial.print(".");
//delay(100);
//}
// Begin WiFi
WiFi.begin(WIFI_SSID, WIFI_PASS);
// Connecting to WiFi...
Serial.print("Connecting to ");
Serial.print(WIFI_SSID);
// Connected to WiFi
Serial.println();
Serial.print("Connected! IP address: ");
Serial.println(WiFi.localIP());
Serial.println();
Serial.print("Setting soft-AP configuration ... ");
Serial.println(WiFi.softAPConfig(local_ip, gateway, subnet) ? "Ready" : "Failed!");
Serial.print("Setting soft-AP ... ");
Serial.println(WiFi.softAP("Flower_1", "flower123") ? "Ready" : "Failed!");
Serial.print("Soft-AP IP address = ");
Serial.println(WiFi.softAPIP());
delay(500);
Serial.println();
Serial.println("Connected!");
Serial.print("IP address for network ");
Serial.print(WIFI_SSID);
Serial.print(" : ");
Serial.println(WiFi.localIP());
Serial.print("IP address for network ");
Serial.print(AP_SSID);
Serial.print(" : ");
Serial.print(WiFi.softAPIP());
}
void loop() {
float temperature = sht31.readTemperature();
temperature = (temperature * 1.8) + 32;
float humidity = sht31.readHumidity();
// Toggle heater enabled state every 30 seconds
// An ~3.0 degC temperature increase can be noted when heater is enabled
if (++loopCnt == 30) {
enableHeater = !enableHeater;
sht31.heater(enableHeater);
Serial.print("Heater Enabled State: ");
if (sht31.isHeaterEnabled())
Serial.println("ENABLED");
else
Serial.println("DISABLED");
loopCnt = 0;
}
Serial.print("Relative Humidity : ");
Serial.print(humidity);
Serial.println(" %RH");
Serial.print("Temperature in Fahrenheit : ");
Serial.print(temperature);
Serial.println(" F");
delay(500);
lcd.setCursor(0, 0);
lcd.print("Temp - ");
lcd.println(temperature);
lcd.print(" F");
lcd.setCursor(0, 1);
lcd.print("Hum - ");
lcd.println(humidity);
lcd.print(" %");
delay(100);
//WiFiClient wifiClient;
HTTPClient http;
sendval = float(temperature);
sendval2 = float(humidity);
postData = "sendval=" + sendval + "&sendval2=" + sendval2;
http.begin("http://192.168.4.2/dbwrite.php");
http.addHeader("Content-Type", "application/x-www-form-urlencoded"); //Specify content-type header
int httpCode = http.POST(postData); // Send POST request to php file and store server response code in variable named httpCode
Serial.println("Values are, sendval = " + sendval + " and sendval2 = " + sendval2 );
// if connection established then do this
if (httpCode == 200) {
Serial.println("Values uploaded successfully."); Serial.println(httpCode);
String webpage = http.getString(); // Get html webpage output and store it in a string
Serial.println(webpage + "\n");
} else {
// if failed to connect then return and restart
Serial.println(httpCode);
Serial.println("Failed to upload values. \n");
http.end();
return;
}
This is the serial output
Relative Humidity : 41.31 %RH
Temperature in Fahrenheit : 72.73 F
Values are, sendval = 72.73 and sendval2 = 41.31
-1
Failed to upload values.
Thanks for reading. I appreciate any assistance or direction.

