So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By daz3nt
#95926 I'm sorry this is a very basic question, But writing code is my weak point. All I need to do is display the state of sevral GPIO pins from an ESP8266 on to a website. I've found loads of tutorials to display and change the gpio's but nothing to read only the state. I've tried adapting these examples but they seem to stop working after i try adapting it. Could someone plesse help point me in the right direction :D
User avatar
By rooppoorali
#96038 You may not find a full tutorial of what you want to do. But you can do this from seeing multiple tutorials. You have to use the
Code: Select alldigitalRead
function. I used this code to control and see the state of a door lock. You can use this to read the state of the GPIO pins:
Code: Select all#include <ESP8266WiFi.h>
 
const char* ssid = "Your wifi network name";//Write your wifi network's name here
const char* password = "Your passowrd";//Write your password here
 
int LOCK = 4; // GPIO4
WiFiServer server(80);
 
void setup() {
  Serial.begin(115200);
  delay(10);
 
  pinMode(LOCK, OUTPUT);
  digitalWrite(LOCK, LOW);
 
  // 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
  server.begin();
  Serial.println("Server started");
  Serial.println("");
  Serial.println("************WiFi doorlock******************");
 
 
  // Print the IP address
  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("/LOCK=ON") != -1)  {
    digitalWrite(LOCK, HIGH);
    value = HIGH;
  }
  if (request.indexOf("/LOCK=OFF") != -1)  {
    digitalWrite(LOCK, LOW);
     value = LOW;
  }
 
// Set LOCK according to the request
   //digitalWrite(LOCK, 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.print("Door is now: ");
 
  if(value == HIGH) {
    client.print("Open");
  } else {
    client.print("Closed");
  }
  client.println("<br><br>");
  client.println("<a href=\"/LOCK=ON\"\"><button>Turn On </button></a>");
  client.println("<a href=\"/LOCK=OFF\"\"><button>Turn Off </button></a><br />"); 
  client.println("</html>");
 
  delay(1);
  Serial.println("Client disonnected");
  Serial.println("");
 
}
 


You can also use BLYNK app for similar projects. Here is an example of ESP 8266 Nodemcu Ws 2812 Neopixel Based LED MOOD Lamp Controlled by Local Web Server:

https://www.pcbway.com/project/sharepro ... 45c0f.html