Arduino Uno with WiFi – Control Arduino with wget on Linux

You can send commands to your Arduino Uno with WiFi thorough connection URL’s with wget on Linux. This is a simple way to add Arduino Commands to your scripts and web apps.

wget

Functional Parts in the Project:

Linux Commands:

sudo apt-get install wget

wget --tries=1 http://10.0.1.15/?green

Arduino Code:

#include <WiFiNINA.h>

#define greenLED 8
#define redLED 9

char ssid[] = "Basement";
char pass[] = "0123456789";
IPAddress ip(10, 0, 1, 15);


int status = WL_IDLE_STATUS;
WiFiServer server(80);

String readString;

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(greenLED, OUTPUT);
  pinMode(redLED, OUTPUT);

  Serial.begin(9600);

  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);
    status = WiFi.begin(ssid, pass);
    delay(10000);
  }
  
  digitalWrite(LED_BUILTIN, HIGH);
  WiFi.config(ip);
  server.begin();

  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
}

void loop() {


  WiFiClient client = server.available();
  if (client) {
    Serial.println("new client");

    while (client.connected())
    {
      if (client.available())
      {
        char c = client.read();
        if (readString.length() < 100)
        {
          readString += c;
          Serial.write(c);

          if (c == '\n') {

            if (readString.indexOf("?green") > 0)
            {
              digitalWrite(greenLED, HIGH);
              digitalWrite(redLED, LOW);

              delay(1);
            } else if (readString.indexOf("?red") > 0) {
              digitalWrite(greenLED, LOW);
              digitalWrite(redLED, HIGH);

              delay(1);

            } else if (readString.indexOf("?both") > 0) {

              digitalWrite(greenLED, HIGH);
              digitalWrite(redLED, HIGH);

              delay(1);

            } else if (readString.indexOf("?off") > 0) {

              digitalWrite(greenLED, LOW);
              digitalWrite(redLED, LOW);

              delay(1);

            } else {

              Serial.print("PROBLEM:  ");
              Serial.println(readString);

            }
            
                readString = "";

                delay(1);
                client.stop();
                Serial.println("client disconnected");
            }
          }
        }
      }
    }
  }

Be the first to comment

Leave a Reply