Arduino Uno with WiFi – Arduino to Arduino Communication with URL

This project shows you how to send commands from one Arduino Uno with WiFo to another without needing a server. This is a simple Analog Temperature Sensor Project that Turns on LEDS on the other Arduino.

Functional Parts in the Project:

Arduino Sensor Code

  • Sensor Devices can get IP’s from DHCP, but need to point to the IP of the Receiver Arduino.
#include <WiFiNINA.h>

#define sensorPin A5

char ssid[] = "Basement";
char pass[] = "0123456789";

int status = WL_IDLE_STATUS;

char server[] = "10.0.1.16";

WiFiClient client;

void setup() {
  pinMode(LED_BUILTIN, 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);

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

void loop() {

  int reading = analogRead(sensorPin);
  float voltage = reading * 5.0;
  voltage /= 1024.0;
  float temperatureC = (voltage - 0.5) * 100 ;
  float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
  delay (1000);

  Serial.print("Temp F: ");
  Serial.println(temperatureF);

//SEND CODE FROM HERE ---

  if (temperatureF > 80) {
    if (client.connect(server, 80)) {
      Serial.println("connected");
      client.println("GET /red HTTP/1.0");
      client.println();
    }
  } else if (temperatureF < 80) {
    if (client.connect(server, 80)) {
      Serial.println("connected");
      client.println("GET /green HTTP/1.0");
      client.println();
    }
  } else {
    Serial.println("problem");
  }

// ---- TO HERE

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

  delay(1000);
}

Arduino Receiver Code

  • You must statically assign an IP to the Receiver Arduino
#include <WiFiNINA.h>

#define greenLED 8
#define redLED 9

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

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());
  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') {

//URL PARSE CODE FROM HERE ---
            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 {
              digitalWrite(greenLED, HIGH);
              digitalWrite(redLED, HIGH);
              Serial.print("PROBLEM:  ");
              Serial.println(readString);

            }
// ------ TO HERE

            readString = "";

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

Be the first to comment

Leave a Reply