-1

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

Here is my picture

3

This line:

if (client.connect(server,80 && LED == HIGH)) {

is definitely not going to do anything useful. Nor is this line:

else if(client.connect(server,80 && LED == LOW)) {

In both lines you're take the logical AND of the number 80 and the result of testing the LED.

Put the closing parenthesis in correct place:

if (client.connect(server,80) && LED == HIGH) {

and

else if(client.connect(server,80) && LED == LOW) {

Written this way you're still connecting twice if the LED is LOW. The code would be better written:

if (client.connect(server,80)) {
    if(LED == HIGH) {
      do stuff
    } else {
      do other stuff
    }
}

Even better would be to have the do stuff and do other stuff sections just set some variables and then write the code to talk to the web server once, using those variables. Having a single instance of the code will help avoid bugs in the future if you ever need to change it - the DRY (Don't Repeat Yourself) principle of programming.

Also, I hope you didn't just publish your wifi password here. If you did, you should change it.

  • 1
    LED is a pin number not a pin state. – gre_gor Nov 21 '18 at 0:50
  • good point! I'm sure that @Kenneth W. A. can take it as an exercise for the reader to correct that part of his code :) – John Romkey Nov 21 '18 at 4:21
  • Thank you very much, i really appreciate the help, restructured my code, it's working now – Kenneth W. A Nov 25 '18 at 10:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.