0

I am trying to integrate two different Arduino Ethernet libraries, EtherShield and UIPEthernet in one sketch.

EtherShield library is used to control LEDs via Android mobile application. While UIPEthernet is used to control the same LEDs via Web Server.

Both codes run perfectly when they're uploaded individually. But I need them to run in a single sketch. So I combined them in one sketch. That's where my problem is. They're not running smoothly anymore. My mobile app can only turn one LED off then the app will crash. Accessing my Arduino's web server gives me error as well (but before it is really, really working T.T)

Help me, please. This is my code

// ========== UIPEthernet library
#include <Dhcp.h>
#include <Dns.h>
#include <ethernet_comp.h>
#include <UIPClient.h>
#include <UIPEthernet.h>
#include <UIPServer.h>
#include <UIPUdp.h>

// ========== EtherShield library
#include "EtherShield.h"
#include "ETHER_28J60.h"

// ========== MOBILE
//static uint8_t mac[6] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};                                                    
//static uint8_t ip[4] = {192, 168, 15, 97};                       
static uint16_t port = 80;                                      

ETHER_28J60 e;

// ========== WEB
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 15, 97 };
byte gateway[] = { 192, 168, 15, 1 };
byte subnet[] = { 255, 255, 255, 0 };

String inString = String(35);

int i;  // Used for looping
String Room;

int pin[] = { 0, 2, 3, 4, 5 }; // Lock pin num 0 in array is not used
int numOfRooms = 4; // Number of rooms
String value[] = { "OPEN", "OPEN", "OPEN", "OPEN", "OPEN" }; // Startup all rooms are closed

EthernetServer server(port);
String data;

void setup()
{
  Serial.begin(9600);

  // ========== MOBILE
  e.setup(mac, ip, port);

  // ========== WEB  
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();

  // Set pin mode
  for (int j = 1; j < (numOfRooms + 1); j++)
  {
    pinMode(pin[j], OUTPUT);
  }

  Serial.println("\n[ WEB + MOBILE ]\n");
  Serial.println("Serial READY");
  Serial.println("Ethernet READY");
  Serial.println("Server READY\n");
}

void loop()
{
  webModule();
  mobileAppModule();
}

void mobileAppModule()
{
char* params;
if (params = e.serviceRequest())
{

  //ROOM 311
  if (strcmp(params, "?cmd=1") == 0)
  {
        digitalWrite(pin[1], HIGH);
        Serial.println("ROOM311 is OPEN\n");
  }
  if (strcmp(params, "?cmd=2") == 0)
  {
        digitalWrite(pin[1], LOW);
        Serial.println("ROOM311 is CLOSE\n");
  }

  //ROOM 312 
  if (strcmp(params, "?cmd=3") == 0)
  {
        digitalWrite(pin[2], HIGH);
        Serial.println("ROOM312 is OPEN\n");
  }
  if (strcmp(params, "?cmd=4") == 0)
  {
        digitalWrite(pin[2], LOW);  
        Serial.println("ROOM312 is CLOSE\n");
  }

  //ROOM 313 
  if (strcmp(params, "?cmd=5") == 0)
  {
        digitalWrite(pin[3], HIGH);
        Serial.println("ROOM313 is OPEN\n");
  }
  if (strcmp(params, "?cmd=6") == 0)
  {
        digitalWrite(pin[3], LOW);
        Serial.println("ROOM313 is CLOSE\n");   
  }

  //ROOM 314 
  if (strcmp(params, "?cmd=7") == 0)
  {
        digitalWrite(pin[4], HIGH);
        Serial.println("ROOM314 is OPEN\n");
  }
  if (strcmp(params, "?cmd=8") == 0)
  {
        digitalWrite(pin[4], LOW);
        Serial.println("ROOM314 is CLOSE\n");   
  }

  //
  if (strcmp(params, "?cmd=9") == 0)
  {
        digitalWrite(pin[1], LOW);  
        digitalWrite(pin[2], LOW);  
        digitalWrite(pin[3], LOW);  
        digitalWrite(pin[4], LOW);
        Serial.println("All rooms are now CLOSED\n");   
  }


  e.respond();
  }
}

void webModule()
{
     EthernetClient client = server.available();

 if (client)
 {
 // An HTTP request ends with a blank line
 boolean current_line_is_blank = true;

 while (client.connected())
 {
  if (client.available())
  {
    char c = client.read();
    // If users have gotten to the end of the line (received a newline
    // character) and the line is blank, the HTTP request has ended,
    // so users can send a reply

    if (inString.length() < 35)
    {
      inString.concat(c);
    }

    if (c == '\n' && current_line_is_blank)
    {

      // Send a standard HTTP response header
      client.println("HTTP/1.1 200 OK");
      client.println("Content-Type: text/html");
      client.println();

      client.println("<html>");

      client.println("<head><title>CONTROL PANEL</title></head>");

      client.println("<body style='padding: 0; margin: 0;' onunload='window.opener.document.getElementById('controlPanel').disabled = false;'>");

      client.println("<div style='width: 100%; height: auto; background-color: #000000;'><center style='font-size: 18px; font-family: Impact; color: white;'>POLYTECHNIC UNIVERSITY OF THE PHILIPPINES<br />COLLEGE OF ENGINEERING<br />COMPUTER ENGINEERING DEPARTMENT<br /></center><center style='font-size: 30px; font-family: Impact; color: #FF9600;'>COMPUTER ENGINEERING LABORATORY</center></div>");
      client.println("<div style='width: 100%; height: 10px; background-color: gray;'></div>");
      client.println("<div style='width: 100%; height: 10px; background-color: #FF9600;'></div><br />");
      client.println("<div style='width: 80%; height: auto; background-color: #FF9600; border-radius: 20px; margin: auto; padding: 5px;'>");
      client.println("<div style='width: auto; height: auto; background-color: #FFFFFF; border-radius: 20px; margin: auto; padding: 5px;'><br /><center style='font-size: 30px; font-family: Impact;'>CONTROL PANEL</center><br /><hr /><br />");

      client.println("<form method=get>");

      for (i = 1; i < (numOfRooms + 1) ; i++)
      {
        Room = String("ROOM") + (i + 310);

        if (inString.indexOf(Room + "=OPEN") > 0 || inString.indexOf("All=OPEN") > 0)
        {
          Serial.println("\n" + Room + " is OPEN");
          digitalWrite(pin[i], HIGH);
          value[i] = "CLOSE";
        }
        else if (inString.indexOf(Room + "=CLOSE") > 0 || inString.indexOf("All=CLOSE") > 0)
        {
          Serial.println("\n" + Room + " is CLOSE");
          digitalWrite(pin[i], LOW);
          value[i] = "OPEN";
        }

        client.println("<br /><center style='font-family: Impact;'>" + Room + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='submit' name=" + Room + " value=" + value[i] + " style='height: 50px; width:100px;' /><br /><br />");
      }

      client.println("<br /><br />All <input type='submit' name='All' value='OPEN' style='height: 50px; width:100px;' />&nbsp;&nbsp;&nbsp;&nbsp;<input type='submit' name='All' value='CLOSE' style='height: 50px; width:100px;' />");
      client.println("</center></form>");

      for (i = 1; i < (numOfRooms + 1); i++)
      {
        if (value[i] == "ON")
        {
          i = i + 310;
          client.println("<br /><br /><p>Room " + String(i + 310) + " is OPEN</p>");
        }
        else if (value[i] == "OFF")
        {
          client.println("<p><p>Room " + String(i + 310) + " is CLOSE");
        }
      }
      client.println("</div>");
      client.println("</div>");
      client.println("</body></html>");
      break;
    }

    if (c == '\n')
    {
      // we're starting a new line
      current_line_is_blank = true;
    }
    else if (c != '\r')
    {
      // we've gotten a character on the current line
      current_line_is_blank = false;
    }
  }
}

    // give the web browser time to receive the data
    delay(1);
    inString = "";
    client.stop();
    }
}

MOBILE means code for Arduino-Android WEB means the code for Web Server

1 Answer 1

0

You can't.

  1. The code has both libraries trying to run on the same port which is not allowed (in fact you couldn't even do that on a computer). You could try setting one to one port and the other to another port, but that might not work because...
  2. These libraries most likely are both expecting to be the only code accessing the hardware and they will step over each other and cause problems. Sharing hardware between different pieces of code is an advanced programming topic not suited for beginners and would involve rewriting significant portions of these libraries.

The best course of action is to either re-implement your app functionality to use the UIPEthernet Library, or re-implement the webpage using the EtherShield library. You should be able to do this because your app interface is supplying parameters (e.g the "?cmd=" part) and the webpage is not. You should be able to set it up so that if it has "?cmd=" in the request, it should perform the command, otherwise it should return the webpage.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.