I am using a magnetic float switch sensor to monitor water level in a tank. I am using a ESP8266 board.
I am trying to send the data collected by it to a php page in a host called "weris.link". I've checked the WiFi ssid and password for a myriad of times, yet somehow the connection to server still failed.
Did i commit a mistake ?
Here's my code :
#include <ESP8266WiFi.h>
#define FLOAT_SENSOR D2 // the number of the pushbutton pin
#define LED D8 // the number of the LED pin
const char* server = "weris.link"; // alamat server
const char* ssid = "aBee"; // SSID router wifi
const char* password = "xxxxx"; // password wifi
const char* SensorID = "Water_Level_Kelompok_1.Unsrat"; // ID sensor
const char* count1= "Water Tank Full";
const char* count2= "Water Level Low";
const char* recommend1= "Turn Off Flow";
const char* recommend2= "Turn on Flow";
WiFiClient client;
void setup()
{
Serial.begin(115200);
// initialize the LED pin as an output:
pinMode(LED, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(FLOAT_SENSOR, INPUT_PULLUP);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi Connected");
}
void loop()
{
if(digitalRead(FLOAT_SENSOR) == LOW)
{
// turn LED on:
digitalWrite(LED, HIGH);
Serial.println(count1);
}
else
{
// turn LED off:
digitalWrite(LED, LOW);
Serial.println(count2);
}
// mulai Kirim data sensor
if (client.connect(server,80 && LED == HIGH)) {
Serial.print("Posting data...");
digitalWrite(BUILTIN_LED, LOW);
Serial.println("Tank Full: " + String(count1) + ", Tank Level Below Normal: " + String(count2));
// dengan GET /path file php penangkap data
client.println("GET /iot/terima.php?data1=" + String(count1) + "&data2=" + String(recommend1) + "&sid=" +SensorID+ " HTTP/1.1");
client.println("HOST: weris.link");
client.println("Connection: close");
client.println();
client.stop();
Serial.println();
digitalWrite(BUILTIN_LED, HIGH);
}
else if(client.connect(server,80 && LED == LOW)) {
client.println("GET /iot/terima.php?data1=" + String(count2) + "&data2=" + String(recommend2) + "&sid=" +SensorID+ " HTTP/1.1");
client.println("HOST: weris.link");
client.println("Connection: close");
client.println();
client.stop();
Serial.println();
digitalWrite(BUILTIN_LED, HIGH);
}
else {
Serial.println("Koneksi ke server gagal");
}
delay(5000);
}
Below is the screen capture from the serial monitor