Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By isan98
#84852 In this code, I want to know when the connection is disconnected for write LOW on the pin

digitalWrite(motor, LOW);

When the connection is disconnected, How can I do it?

Code: Select all#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ArduinoJson.h>


#define motor  4                //D2


DynamicJsonBuffer jsonBuffer;

const char *ssid      = "ssid";
const char *password  = "password";

int sensorValue0 = 0;       
       
String sensor_values;

ESP8266WebServer server(80);


void handleSentVar() {
 
  if (server.hasArg("sensor_reading"))
  {
    sensor_values = server.arg("sensor_reading");
    Serial.print("**********************sensorValue:");
    Serial.println(sensor_values);
  }
  JsonObject& root = jsonBuffer.parseObject(sensor_values);

      sensorValue0          = root["sensor0_reading"].as<int>();



  Serial.print("sensorValue0:");
  Serial.println(sensorValue0);


  toggle_motors();

  server.send(200, "text/html", "Data received");
}


void setup() {
  Serial.begin(9600);
  WiFi.softAP(ssid, password);
  IPAddress myIP = WiFi.softAPIP();

  pinMode(motor, OUTPUT);


 
  //toggle_motors();                 //turn off all leds as all the sensor values are zero
 
  server.on("/data/", HTTP_GET, handleSentVar); // when the server receives a request with /data/ in the string then run the handleSentVar function
  server.begin();

 
}

void loop() {
  server.handleClient();
  delay(1000);
}

void toggle_motors()
{
  if (sensorValue0 == 0){  digitalWrite(motor, LOW);
   Serial.println("++++++++++++++++off+++++++++++++");}


  if (sensorValue0 == 1){  digitalWrite(motor, HIGH);
  Serial.println("------------on-------------");}

}