The use of the ESP8266 in the world of IoT

User avatar
By TarekJ
#61381
Barnabybear wrote:Hi, have a look at the colourfull table about half way down this page
wiki/doku.php?id=esp8266_gpio_pin_allocations

By default the pins are as listed in the table function 0, pin 1 Tx and pin 3 Rx. You want pin 1 to be function 3 a GPIO.
You can activate any “FUNCTION” with pinMode(pin, FUNCTION1) for example.

Code: Select all// this will swap the pin to a GPIO
pinMode(1, FUNCTION3);

// this will restore the pin to Tx
pinMode(1, FUNCTION0);



I have code I used to get 2 gpios and it is working fine, I modified the code to have 4 gpios as you advised, would you check the code below and confirm if it is ok?

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>

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

String form ;
ESP8266WebServer server(80);
const int led1 = 0; //GPIO 0
const int led2 = 2; //GPIO 2
const int led3 = 1; //GPIO 1
const int led4 = 3; //GPIO 3
int Led_1_IsOn = 0; //GPIO 0
int Led_2_IsOn = 0; //GPIO 2
int Led_3_IsOn = 0; //GPIO 1
int Led_4_IsOn = 0; //GPIO 3

void handle_led() {
String state = server.arg("state");

if ((state)=="btn1") {
Led_1_IsOn = (digitalRead(led1)==0?1:0);
digitalWrite(led1, Led_1_IsOn);}

else if ((state)=="btn2") {
Led_2_IsOn = (digitalRead(led2)==0?1:0);
digitalWrite(led2, Led_2_IsOn);}

else if ((state)=="btn3") {
Led_3_IsOn = (digitalRead(led3)==0?1:0);
digitalWrite(led3, Led_3_IsOn);}

else if ((state)=="btn4") {
Led_4_IsOn = (digitalRead(led4)==0?1:0);
digitalWrite(led4, Led_4_IsOn);}

else if ((state)=="btnAll") {
Led_1_IsOn = 0;
Led_2_IsOn = 0;
Led_3_IsOn = 0;
Led_4_IsOn = 0;
digitalWrite(led1, Led_1_IsOn);
digitalWrite(led2, Led_2_IsOn);}
digitalWrite(led3, Led_3_IsOn);}
digitalWrite(led4, Led_4_IsOn);}

String TmpForm = form;
if (Led_1_IsOn == 1)
TmpForm.replace("state=btn1'><button>" , "state=btn1'><button style=background-color:#00ff00;>");
if (Led_2_IsOn == 1)
TmpForm.replace("state=btn2'><button>" , "state=btn2'><button style=background-color:#00ff00;>");
if (Led_3_IsOn == 1)
TmpForm.replace("state=btn3'><button>" , "state=btn3'><button style=background-color:#00ff00;>");
if (Led_4_IsOn == 1)
TmpForm.replace("state=btn4'><button>" , "state=btn4'><button style=background-color:#00ff00;>");

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

void setup(void) {
form = "<html>";
form += "<head><meta content='text/html; charset=utf-8'>";
form += "<style>button{width: 36px; height: 36px; border-radius:24px; font-size: 24px;}</style>";
form += "</head>";
form += "<body>";
form += "<a href='led?state=btn1'><button> </button></a><br/>";
form += "<a href='led?state=btn2'><button> </button></a><br/>";
form += "<a href='led?state=btn3'><button> </button></a><br/>";
form += "<a href='led?state=btn4'><button> </button></a><br/>";
form += "<a href='led?state=btnAll'><button> </button></a>";
form += "</html>";

pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);

// Start Server (Work as Access Point)
WiFi.softAP(ssid, password);
server.on("/", [](){
server.send(200, "text/html", form);
});
server.on("/led", handle_led);

// Start the server
server.begin();
// Serial.println(WiFi.localIP());
}

void loop(void) {
// check for incoming client connections frequently in the main loop:
server.handleClient();
}
User avatar
By Barnabybear
#61429 Hi, no that will not work. You need to change the function by adding the two lines below:
Code: Select all//********************* CHANGE PIN FUNCTION **********************
pinMode(led3, FUNCTION_3); //GPIO 1 swap the pin to a GPIO.
pinMode(led4, FUNCTION_3); //GPIO 3 swap the pin to a GPIO.
//***************************************************************************
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
User avatar
By TarekJ
#61457
Barnabybear wrote:Hi, no that will not work. You need to change the function by adding the two lines below:
Code: Select all//********************* CHANGE PIN FUNCTION **********************
pinMode(led3, FUNCTION_3); //GPIO 1 swap the pin to a GPIO.
pinMode(led4, FUNCTION_3); //GPIO 3 swap the pin to a GPIO.
//***************************************************************************
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);


Thank you very much, but does this affect the module when reflashing it? as the tx is no longer tx, it is gpio? :)
User avatar
By rudy
#61462
TarekJ wrote:
Barnabybear wrote:Hi, no that will not work. You need to change the function by adding the two lines below:
Code: Select all//********************* CHANGE PIN FUNCTION **********************
pinMode(led3, FUNCTION_3); //GPIO 1 swap the pin to a GPIO.
pinMode(led4, FUNCTION_3); //GPIO 3 swap the pin to a GPIO.
//***************************************************************************
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);


Thank you very much, but does this affect the module when reflashing it? as the tx is no longer tx, it is gpio? :)


No it doesn't. When it is in boot loader mode (re-flashing) it is not running your code.