Mega+Display+Ethernet

Hi everybody,

I am new in Arduino, but I just want to make Ethernet tester with Arduino

I have Mega2560, TFT_320QVT display and Ethernet Shield together.

My problem is that i want to make a back button. I have one screen where arduino is like DHCP client, so Ethernet shield accept ethernet IP address and than i want to go back to home screen.

here is my code

#include <TFT.h>
#include <memorysaver.h>
#include <UTFT.h>
#include <UTouch.h>
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>

//#define localAddr 

#include "ICMPPing.h"

int led1=1;
int led2=10;

char m[16];
char su[16];
char s[16];
char g[16];

  

SOCKET socket = 0 ;
int pingTimeout = 4 ;
byte destIp[] = {8,8,8,8} ; // google.com
char strLineResult[256] ;


extern uint8_t SmallFont[];
//  extern uint8_t BigFont[];

byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
IPAddress ip(192,168,1, 199);
IPAddress gateway(192,168,1, 1);
IPAddress subnet(255, 255, 0, 0);

// telnet defaults to port 23
EthernetServer server(23);
boolean gotAMessage = false;
 
UTFT myGLCD(ITDB32S,38,39,40,41);  
UTouch myTouch(52,53,50,51,2);
long x,y;
char page;

boolean straighta_res[8] = {false};
boolean cross_res[8] = {false};
 
 
void setup(){
   Serial.begin( 9600 ) ;
   myTouch.InitTouch();
  //nastavení přesnosti dotykového panelu
  myTouch.setPrecision(PREC_HI);
  // nastavení LCD
  myGLCD.InitLCD();
  // nastavení písma na velký font
  myGLCD.setFont(SmallFont);
  // vymazání obrazovky
  myGLCD.clrScr();
  Home();
  page= '0';
  
  pinMode(4, OUTPUT);
  pinMode(10, OUTPUT);

 
}

void Home(){
  
  
  
  myGLCD.setBackColor(255, 255, 255);
 
  myGLCD.fillRect(0,0,319,239);
 
  myGLCD.setColor(120, 120, 255);
  myGLCD.drawRoundRect(15,90,105,150);
  myGLCD.print("PING", 44, 115); 
  
  myGLCD.drawRoundRect(120,90,210,150);
  myGLCD.print("SIETOVE", 140, 115);
  myGLCD.print("INFO", 150, 125);  
  
  myGLCD.drawRoundRect(225,90,315,150);
  myGLCD.print("KONTROLA", 238, 115);
  myGLCD.print("KABLA", 250, 125);
  myGLCD.print("DIPLOMOVA PRACA",CENTER,20);
  myGLCD.print("TESTER SIETE ETHERNET",CENTER,40);
  myGLCD.print("Bc. Pavol Majerik, Zilinska Univerzita",CENTER,200);
   
}
 

 
 void loop(){ 
   
  if(page == '0'){ 
      if (myTouch.dataAvailable()){
      myTouch.read();
      x = myTouch.getX();
      y = myTouch.getY();
      if ((x > 15) && (x < 105) && (y > 90) && (y < 150)){
               page='1';
               Ping() ; 
               }
             
       if ((x > 125) && (x < 210) && (y > 90) && (y < 150)) {
               page='2';
              Info();
               } 
               
       if ((x > 225) && (x < 315) && (y > 90) && (y < 150)){
               page='3';
               Kabel() ; 
               }
      } 
    }
}


int a=0;
int b=0;
int i=0;
int d=100;
char c[256]= "Request Timed Out"; 
   
   
void Info(){
  
  digitalWrite(10, LOW);
 digitalWrite(4, HIGH);  
  
  myGLCD.clrScr();
  myGLCD.setBackColor(255, 255, 255);
  myGLCD.setColor(255, 255, 255);
  myGLCD.fillRect(0,0,319,239);
  myGLCD.setColor(120, 120, 255);
 
  
  Serial.begin(9600);
  Serial.println("Trying to get an IP address using DHCP");
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    Ethernet.begin(mac, ip, gateway, subnet);
    }
  
  Serial.print("My IP address: ");
  ip = Ethernet.localIP();
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(ip[thisByte], DEC);
    Serial.print("."); 
  }  
  byte oct1 = ip[0];
  byte oct2 = ip[1];
  byte oct3 = ip[2];
  byte oct4 = ip[3];

  sprintf(s,"IP: %d.%d.%d.%d",ip[0],ip[1],ip[2],ip[3]);
  myGLCD.print(s, CENTER, 100);
  
  byte ct1 = Ethernet.gatewayIP()[0];
  byte ct2 = Ethernet.gatewayIP()[1];
  byte ct3 = Ethernet.gatewayIP()[2];
  byte ct4 = Ethernet.gatewayIP()[3];

  sprintf(g,"GATEWAY: %d.%d.%d.%d",Ethernet.gatewayIP()[0],Ethernet.gatewayIP()[1],Ethernet.gatewayIP()[2],Ethernet.gatewayIP()[3]);
  myGLCD.print(g, CENTER, 120);
 
  byte t1 = Ethernet.subnetMask()[0];
  byte t2 = Ethernet.subnetMask()[1];
  byte t3 = Ethernet.subnetMask()[2];
  byte t4 = Ethernet.subnetMask()[3];

  sprintf(su,"SUBNET: %d.%d.%d.%d",Ethernet.subnetMask()[0],Ethernet.subnetMask()[1],Ethernet.subnetMask()[2],Ethernet.subnetMask()[3]);
  myGLCD.print(su, CENTER, 140);
  
  byte mt1 = mac[0];
  byte mt2 = mac[1];
  byte mt3 = mac[2];
  byte mt4 = mac[3];
  byte mt5 = mac[4];
  byte mt6 = mac[5];
  sprintf(m,"MAC:%x:%x:%x:%x:%x:%x",mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
  myGLCD.print(m, CENTER, 160);
  
  
   
 myGLCD.fillRoundRect(260,10,310,40);
 myGLCD.print("SPAT", 270, 22);
  
  if (myTouch.dataAvailable())
  {
    myTouch.read();
    x = myTouch.getX();
    y = myTouch.getY();
    if ((x!=-1) and (y!=-1))
    {
    
       if ((x > 260) && (x < 310) && (y > 10) && (y < 40)) setup(); 
           
       }
  }  
  }

in attachment is table how i connect display with mega

       if ((x > 260) && (x < 310) && (y > 10) && (y < 40)) setup();

It makes no sense to call setup() again. It will not initialize the hardware again.

Calling Home() there might make sense.

  byte ct1 = Ethernet.gatewayIP()[0];
  byte ct2 = Ethernet.gatewayIP()[1];
  byte ct3 = Ethernet.gatewayIP()[2];
  byte ct4 = Ethernet.gatewayIP()[3];

  sprintf(g,"GATEWAY: %d.%d.%d.%d",Ethernet.gatewayIP()[0],Ethernet.gatewayIP()[1],Ethernet.gatewayIP()[2],Ethernet.gatewayIP()[3]);
  myGLCD.print(g, CENTER, 120);

What is the purpose of storing the gateway IP in 4 local variables that soon go out of scope without ever having been used?

The code that you posted (that desperately needs to have Tools + Auto Format run on it) does something. You didn’t way what it does. You expect it to do something. You didn’t say what you expect it to do.

I hope you feel better after having vented, but more details would have probably produced a more useful response.

i changed it to Home() screen thanks,

About gateway, i wasnt able to find any option how to visualisation this address on display and this way make it :slight_smile:
If you have some kind of idea how it can be it would be great.

My problem is that when I use ethernet shield touch doesnt work. I need show network info like IP address, Gateway ... and than 1 button for Back option, but it doesnt work. I touch this button but no action.

UTouch myTouch(52,53,50,51,2);

Those are the SPI pins. The Ethernet shield uses the SPI pins. Unless the touch screen is an SPI device, you must get it off those pins, if you expect it and the ethernet shield to work at the same time.