I want to have only one LED that serves as indicator

I want to have only one LED that serves as indicator for each connection the ESP makes.

I wanted to use this type of LED. I want to include it in the code wherein if the ESP is on, red light will turn on. If it is connected to wifi, green light will on, if it is connected to the database green light will blink for approximately 5 secs (or is there a way to make the LED turn yellow if the ESP connects to the database?). Lastly, if it is disconnected to wifi or database, led is off.

I am planning to use GPIO4, GPIO5 of the ESP for the pin of the LED.

Here is my code:

#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>
#include <ESP8266WiFi.h>

//Network credentials
const char* ssid = "wifi";
const char* pass = "password";

//Database credentials
IPAddress server_addr (192,167,2,100); // MySQL server IP
char user[] = "name";
char password[] = "pass";
char database[] = "table";

// Default values
uint32_t data_1 = 0;
uint32_t data_2 = 0;

WiFiClient client;
MySQL_Connection conn((Client *)&client);
//Create an instance of the cursor passing in the connection
MySQL_Cursor cur = MySQL_Cursor(&conn);

//Declaration of byte format
uint8_t header = 0xFF; // byte 0 as header
uint8_t address1 = 0xFA; // 1st layer address
uint8_t address2 = 0xFB; // 2nd layer address
uint8_t byte2 = 0x90;
uint8_t byte6 = 0x10;

//Wi-Fi connection set up
void setup_wifi() {
  delay(500);
  //Connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void setup() {
  Serial.begin(115200);
  Serial1.begin(9600, SERIAL_8N1);
  setup_wifi();
 
// Connect to MySQL server
  if (conn.connect(server_addr, 3306, user, password, database)) {
    Serial.println("Connected to MySQL server"); }
  else {
    Serial.println("Connection to MySQL server failed");
    return;
  }
}

void loop () {
// Execute query to get a single row
  char query[] = "SELECT SUM(data1), SUM(data2) FROM data ORDER BY time DESC LIMIT 1 ";
  
  MySQL_Cursor* cursor = new MySQL_Cursor(&conn);
  if (conn.connected())
  {
  cursor->execute(query);

  // Fetch the columns and print them
  column_names *cols = cursor->get_columns();
  for (int f = 0; f < cols->num_fields; f++) {
    Serial.print(cols->fields[f]->name);
    if (f < cols->num_fields-1) {
      Serial.print(',');
    }
  }

  Serial.println();
  // Fetch the first row of data
  row_values* row = NULL;
  int data1 = 0;
  int data2 = 0;

  do {
    row = cursor->get_next_row();
    if (row != NULL) {
    data1 = atoi(row->values[1]);
    data2 = atoi(row->values[2]);

    }
  } while (row != NULL);

  //Assign the converted values to data_1 - data_4
  data_1 = data1;
  data_2 = data2;

  Serial.println(data_1);
  Serial.println(data_2);

  // Clean up cursor memory to avoid memory leak
  delete cursor;
  } else {
    conn.close();
    Serial.println("Connection...");
    delay(200);

    if (conn.connect(server_addr, 3306, user, password, database)) {
      delay(500);
      Serial.println("Successful reconnect!");
    } else {
      Serial.println("Cannot reconnect!");
    }
  }
  
// Process the values of data_1, data_2
  for (int i = 1; i <= 2; i++) {
    uint32_t data_i;
    uint8_t address;

    if (i == 1) {
      data_i = data_1;
      address = address1;
    } else if (i == 2) {
      data_i = data_2;
      address = address2;
    }

    uint8_t byte5 = (data_i >> 16) & 0xFF;
    uint8_t byte4 = (data_i >> 8) & 0xFF;
    uint8_t byte3 = data_i & 0xFF;

    uint8_t checksum = (address + byte2 + byte3 + byte4 + byte5 + byte6);
    uint8_t data_packet[8] = {header, address, byte2, byte3, byte4, byte5, byte6, checksum};
    
  Serial1.write(data_packet, sizeof(data_packet));
  Serial1.flush();}
  }

You just turn on both the red and the green LEDs at the same time to show yellow.

You might want to use PWM to put the red and green on at a lower intensity so that the yellow doesn't appear brighter than the other colours.

1 Like
  while (WiFi.status() != WL_CONNECTED) {
    digitalWrite(GPIO4, LOW);
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  digitalWrite(GPIO4, HIGH);
// Connect to MySQL server
  if (conn.connect(server_addr, 3306, user, password, database)) {
    Serial.println("Connected to MySQL server");
    digitalWrite(GPIO4, LOW);
    digitalWrite(GPIO5, HIGH);
  }
1 Like

If both are on then what colour do you expect the LED to show ?

1 Like

I wanted to clarify, there is no need for declaring the GPIO4 and GPIO5 before digitalWrite function? Something like setting the pinmode?

void setup() {

  // Initialize the serial port
  Serial.begin(115200);

  // Initialize the LEDs
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);

  // Set the red LED to be on
  digitalWrite(4, HIGH);

  // Connect to WiFi
  setup_wifi();

  // Check if the device is connected to WiFi
  if (WiFi.status() == WL_CONNECTED) {

    // Set the green LED to be on
    digitalWrite(5, HIGH);
  }
}

I am not confident with this code of mine, but I think this code makes the LED turn red light after powering it on, and then making the green light (connected to GPIO5) turn on if the device connects to the WIFI.

As compared to your answer, if I were to comprehend it, it makes the red light turn on (GPIO4) if it is connected to wifi, and green light (GPIO5) is on if it is connected to the database.

opposite

maybe I can turn both of them on to have a yellow light, right? If so, can I add a delay so that let's say green light will only turn on for 10 seconds.

Will this code work? (can't test it right now)

// Initialize the LED
pinMode(5, OUTPUT);

// Set the LED to be on
digitalWrite(5, HIGH);

// Delay for 1 second
delay(10000);

// Set the LED to be off
digitalWrite(5, LOW);

I'm sorry but I don't get your reply of "opposite"

this is correct. use it.

1 Like

is using delay function okay if I wanted the green light to only power let's say for 10 seconds?

like this:

// Initialize the LED
pinMode(5, OUTPUT);

// Set the LED to be on
digitalWrite(5, HIGH);

// Delay for 1 second
delay(10000);

// Set the LED to be off
digitalWrite(5, LOW);

this will cause ESP resetting.

Tried to search base on your response and discovered that delay might cause ESP to draw too much power and reset. If so, is there no other way to turn on the LED for a few seconds just to indicate that it has connected to wifi/database?

google "blink without delay"

read sample sketches on this but I am a bit confused as the examples are inserting this code inside the for loop, however, with my code, I just need it to blink or be on during the set up.

you mean only once light up and then switch off?

if you just want to indicate a state during setup and when you are fine to block your code before it continues, a delay() is ok in setup().

Nevertheless using millis() like in BlinkWithoutDelay will work also. You switch on the LED in setup and switch off the LED when enough time has passed in loop.

1 Like

For the power on, it wil light as long as the ESP is on. And for the wifi connection and database, yes, it will only light up once as it will be connected when the esp is turned on, and inside the for loop only the transmission is looping and not the wifi/database connection.

so what is the problem? i showed places where it can be done. you know how to initialize pins. you know what you want to light up on specific action.