Noob wiring/software help for esp8266

want to make a remote pc switch. found this guys vid on youtube

he makes it remote using two esp2866 boards. One connected to a button or in my case a momentary toggle switch away from my pc and the other in the pc.
he made code to run it all and ive got it all working the problem is that my toggle switch doesnt have a built in led so i got an indicator lamp/led but it stays on and then turns off in the circuit when the switch activates the code. ive tried altering his code using info i found online and tried putting the led in different positions but at the end of the day i dont know what im doing, just doing this as a lil project to learn. if anyone could tell me whether i need more hardware like a resistor (according to google that could be a solution) or if its something simple like rewritting the software properly which im confident i didnt do right if you could you please help that would be great
his code for button controller is here
his actual ardiuno links are under his vid
cheers again

#include <ESP8266WiFi.h>
byte input_pin = 2;
char comm_name[] = "IN_PC";
char password[] = "pa55w0rd";
IPAddress server(192,168,10,1);         
WiFiClient client;
void setup() {
  Serial.begin(9600);
  WiFi.mode(WIFI_STA);
  WiFi.begin(comm_name, password);      
  Serial.println();
  Serial.println("Connection to the AP");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  Serial.println();
  Serial.println("Connected");
  Serial.print("LocalIP:"); Serial.println(WiFi.localIP());
  Serial.println("MAC:" + WiFi.macAddress());
  Serial.print("Gateway:"); Serial.println(WiFi.gatewayIP());
  Serial.print("AP MAC:"); Serial.println(WiFi.BSSIDstr());
  pinMode(input_pin, INPUT_PULLUP);
}
void loop() {
  int input_val = digitalRead(input_pin);
  if (input_val == LOW){
    client.connect(server, 80);
    while (input_val == LOW){
      input_val = digitalRead(input_pin);
      client.print(' ');
    }
    client.print('\r');
    client.flush();
    client.stop();
  }
}

You may have noticed that your original post has been edited to remove the bad language. Don't use such language again or you run the risk of suspension

2 Likes

The ESP8266 has a build-in LED which you could use rather than trying to add an external LED.

I don't see anything in your code to indicate toggling an LED.

yes so the on board led turns on but i would like to add to the code that when i flick my momentary toggle switch it also turns on a red led which will be placed on a faceplate with the switch. Theres nothing in that code turning on led because thats the guy from the vids original code. when ive tried wiring it differently i can get it to stay on and then turn off when i flick switch but i need the opposite of that. if theres some hardware that can reverse it that would be great too

Which is it? It's important.

And neither do we (know what you're doing). We are not psychic.

Please post a schematic. Hand-drawn will be fine as long as it is neat.

1 Like

using these Gebildet 5pcs 6mm 3V-4.5V-5V-6V-7.5V-9VDC Waterproof Signal Lamp with Wire, LED Metal Indicator Light Energy Saving(Red) : Amazon.co.uk: Business, Industry & Science

when i say altered it i mean i attempted before realising someone whom has never coded will probs struggle for hrs and its best just to ask someone who knows, the code is posted is the original. the schematic is microcontroller gpio pin to toggle switch terminal. the switch has two terminals with the other returning to the microcontrollers ground. The microcontroller is powered by 5v microusb. with this setup the code and its intended function works. What i would like to know is how to add an external light to the equation. will draw up a schematic when i sit down although dont expect anything fancy as ive never done it


this is the schematic in the vid though its for a button with a integrated led. When i try plug a external light into the vin pin as he did it doesnt work. ive since learnt from google that the vin pin is normally an input? So dont know the science of what he did in that diagram

Big clue VIN not VOUT.
2nd clue the code is looking somewhere else for the pin change.
3rd clue look into a button library.

Why not Vout?

According to NodeMCU internal schematic, the Vin pin is connected directly to the 5V power from the USB Socket.

@darthbonk57 is powering the circuit through USB, so Vin pin should be 5V and the "lamp" or led or whatever should light.

this is what i thought as inexperienced as i am, dont know what any of that diagram means. google said i might need a resistor to drop the volt from 5 to 3 but why do this if one of you guys can teach me to put a bit of extra code into the prexisting code to power the light from a gpio instead of vin. Dont even know if it would work but thats why im here. im looking up button librarys as @sonofcy said

Bad practice. No reason not to use a conventional 5V source.
BTW, could you circle the VIN pin? I don't see it.

Try starting with the Arduino Cookbook then use the examples that come with a library to learn how to use it.

the vin is where the red line from led+ is going on picture i posted

My comment wasn't addressed to you.

i can get it to stay on and then turn off when i flick switch but i need the opposite of that.

Can you show how the led light was wired and how the switch was wired when you got this result?

@cattledog this is the first time ive drawn a schematic so sorry if i offend anyone with incorrect symbols etc havent looked that stuff up. the negative lines ive marked orange although i can wire both the switch and led either way it doesnt affect result. the momentary toggle switch has 2 terminals and again doesnt matter what terminal is connected to what. With this setup the light stays on but when i flick switch it turns off

What code is running on the esp8266 which makes pin4 HIGH with output voltage on it?

the code the guy made above? i believe d4 corresponds with gpio 2 which is why hes writes in the code input pin =2? i dont know

You do not need any extra code if all you want to do is have a LED go on when the switch is closed and off when it is open.

When the toggle is to the left, the LED is grounded which turns is ON and D2 reads LOW.
When the toggle is to the right, the D2 is pulled HIGH since you configured the pin as INPUT_PULLUP and the LED is OFF.

1 Like

@blh64 your an absolute weapon this worked appreciate the help