Tuesday, July 8, 2025

Programming my Arduino Industrial Board




 

I 'm excited to start testing my newly acquired Arduino Industrial 101 board for potential R&D in Internet of Things (IOT) applications. Thanks to the help of various AI tools, programming microcontrollers has become much easier and faster, making the learning more efficient.

Upon inspecting the board, I can see that this board has 3 digital pins and 4 analog input channels. For my first project, I 'm using the digital pins 5, 6 and 13 each connected to a differently colored LED. Based on  ChatGPT,'s suggestion, activating these LEDs requires the use of Bridge library, and is recommended the following code as a starting point.

#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>

YunServer server;

void setup() {
  Bridge.begin();      // Start Bridge
  server.listenOnLocalhost();
  server.begin();

  pinMode(5, OUTPUT);   // Set pin 5 as output
  pinMode(6, OUTPUT);   // Set pin 6 as output
  pinMode(13, OUTPUT);  // Set pin 13 as output
}

void loop() {
  YunClient client = server.accept();

  if (client) {
    process(client);
    client.stop();
  }

  delay(50);
}

void process(YunClient client) {
  String command = client.readStringUntil('/');

  if (command == "digital") {
    int pin = client.readStringUntil('/').toInt();
    int value = client.readStringUntil('\r').toInt();

    if (pin == 5 || pin == 6 || pin == 13) {
      digitalWrite(pin, value);
      client.print(F("Pin "));
      client.print(pin);
      client.print(F(" set to "));
      client.print(value);
    } else {
      client.print(F("Invalid pin."));
    }
  } else {
    client.print(F("Invalid command."));
  }
}

After compiling the code and programming it to the board, the 3 LEDs can be tested by the following http commands.

Activate pin 5: http://arduino.local/arduino/digital/5/1
Deactivate pin 5: http://arduino.local/arduino/digital/5/0

Activate pin 6: http://arduino.local/arduino/digital/6/1
Deactivate pin6: http://arduino.local/arduino/digital/6/0

Activate pin 13: http://arduino.local/arduino/digital/13/1
Deactivate pin 13: http://arduino.local/arduino/digital/13/0





Using ChatGPT again, I was able to create an HTML interface with 3 buttons corresponding to each LED. With this dashboard, I can now control all the 3 LEDs remotely. Below is the HTML code used to control the LEDs. 


<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Arduino Pin Control</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      text-align: center;
      margin-top: 50px;
    }
    .button {
      display: inline-block;
      width: 140px;
      padding: 15px;
      margin: 10px;
      font-size: 18px;
      border: none;
      border-radius: 10px;
      cursor: pointer;
      background-color: #ccc;
    }
    .button.on {
      background-color: #4CAF50;
      color: white;
    }
  </style>
</head>
<body>

  <h1>Arduino Control Panel</h1>

  <button class="button" id="pin5" onclick="togglePin(5)">Pin 5 OFF</button>
  <button class="button" id="pin6" onclick="togglePin(6)">Pin 6 OFF</button>
  <button class="button" id="pin13" onclick="togglePin(13)">Pin 13 OFF</button>

  <script>
    const arduinoIP = "192.168.8.238";  // ✅ Your Arduino IP
    const pinStates = { 5: 0, 6: 0, 13: 0 };

    function togglePin(pin) {
      // Toggle internal state
      pinStates[pin] = pinStates[pin] ? 0 : 1;

      // Build URL
      const url = `http://${arduinoIP}/arduino/digital/${pin}/${pinStates[pin]}`;

      // Use Image to send GET request (avoids CORS)
      const img = new Image();
      img.src = url;

      // Update button UI
      const btn = document.getElementById(`pin${pin}`);
      if (pinStates[pin]) {
        btn.classList.add("on");
        btn.textContent = `Pin ${pin} ON`;
      } else {
        btn.classList.remove("on");
        btn.textContent = `Pin ${pin} OFF`;
      }
    }
  </script>

</body>
</html> 

Before uploading the code to Arduino, make sure to connect the board to your local WIFI network. Once connected, take note of the IP address assigned to the board, and update your HTML code accordingly to ensure proper communication.   --- 73 de du1vss