Sunday, November 17, 2019

Read ADC value with nodeMCU





This is an extention for the previous post... I have added the ADC read button to get the current ADC Value.

Source Code:

#include <ESP8266WiFi.h>

const char* ssid = "Dialog 4G"; //your WiFi Name
const char* password = "YJNHQ102RD8";  //Your Wifi Password
int ledPin = D0;
int ledPin2 = D1;
const int analogInPin = A0;  // ESP8266 Analog Pin ADC0 = A0
int sensorValue = 0;  // value read from the pot
WiFiServer server(80);
//-----------------------------------------------------------------------
void setup() {
  Serial.begin(115200);
  delay(10);

  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  pinMode(ledPin2, OUTPUT);
  digitalWrite(ledPin2, LOW);
//  pinMode(ADC, INPUT);
 // digitalRead(ADC);

 
 

// Connect to WiFi network------------------------------------------------
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");

  // Start the server by NodeMCU
  server.begin();
  Serial.println("Server started");

  // Print the IP address on serial Monitor
  Serial.print("Use this URL to connect: ");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/");

}

void loop() {
// Check if a client has connected---------------------------------------
  WiFiClient client = server.available();
  if (!client) {
    return;
  }

// Wait until the client sends some data----------------------------------
  Serial.println("new client");
  while(!client.available()){
    delay(1);
  }

// Read the first line of the request---------------------------------------
  String request = client.readStringUntil('\r');
  Serial.println(request);
  client.flush();

// Match the request-------------------------------------------------------

  int value = LOW;
  if (request.indexOf("/LED=ON") != -1)  {
    digitalWrite(ledPin, HIGH);
    value = HIGH;
  }
  if (request.indexOf("/LED=OFF") != -1)  {
    digitalWrite(ledPin, LOW);
    value = LOW;
  }
//-------------------------------------------------------------------------
   int value2 = LOW;
  if (request.indexOf("/LED2=ON") != -1)  {
    digitalWrite(ledPin2, HIGH);
    value2 = HIGH;
  }
  if (request.indexOf("/LED2=OFF") != -1)  {
    digitalWrite(ledPin2, LOW);
    value2 = LOW;
  }

// Set ledPin according to the request---------------------------------------
//digitalWrite(ledPin, value);

  // Return the response
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println(""); //  do not forget this one
  client.println("<!DOCTYPE HTML>");
  client.println("<html>");
 
  client.println("  <br><br>");
  client.print(" &nbsp;&nbsp;&nbsp;&nbsp;  Led is now: "); // add 4 tab spaces
   if(value == HIGH) {
    client.print("On");
  } if(value == LOW) {
    client.print("Off");
  }
  client.println(" <br><br>");
  client.println("  &nbsp;&nbsp;&nbsp;&nbsp;  <a href=\"/LED=ON\"\"><button>On </button></a>");
  client.println("  &nbsp;&nbsp;&nbsp;&nbsp;  <a href=\"/LED=OFF\"\"><button>Off </button></a><br />"); 
  client.println("</html>");
  client.println("<br><br>");

 
  client.print("  &nbsp;&nbsp;&nbsp;&nbsp;  Led2 is now: ");

  if(value2 == HIGH) {
    client.print("On");
  } if(value2 ==LOW) {
    client.print("Off");
  }
  client.println("<br><br>");
  client.println("  &nbsp;&nbsp;&nbsp;&nbsp;  <a href=\"/LED2=ON\"\"><button>On </button></a>");
  client.println("  &nbsp;&nbsp;&nbsp;&nbsp;  <a href=\"/LED2=OFF\"\"><button>Off </button></a><br />"); 
  client.println("</html>");


  sensorValue = analogRead(analogInPin); // read the analog in value
  client.println("<br><br><br>");
  client.println(" &nbsp;&nbsp;&nbsp;&nbsp; ADC:   ");
  client.println(sensorValue);
  Serial.println("ADC:   ");
  Serial.println(sensorValue);


 client.println("<br><br>");
   client.println("  &nbsp;&nbsp;&nbsp;&nbsp;  <a href=\"/sensorValue = analogRead(analogInPin)\"\"><button>Room Temperature </button></a><br />"); 
  client.println("</html>");


  delay(25);
  Serial.println("  Client disonnected");


}

No comments:

Search This Blog