Help Ethernet Arduino web client sending data to web server (TomCat)

Hello to everyone,
I'm new to Arduino and new to Network communication too xD
Btw I'm trying to send a simple value (tension read from an analog PIN) with Arduino as WebClient to a WebServer done with Eclipse/Tomcat, but I can't get the data from the server even if the webclient connects to it.
Here is my Arduino sketch:

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; 
// the dns server ip 198.168.1.48
IPAddress dnServer(198,168,1,48);
// the router's gateway address:
IPAddress gateway(192, 168, 1, 123);
// the subnet:
IPAddress subnet(255, 255, 0, 0);
//the IP address
IPAddress ip(192,168,9,123);



EthernetClient client;

//pin where I get the tension
const int pinPot = A0;

int pot = 0 ; 


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

	Ethernet.begin(mac, ip, dnServer, gateway, subnet );
	delay(1000);
	Serial.println("Connesso ");
	Serial.println("il tuo ip é: ");
	Serial.println(Ethernet.localIP());
	
}

void loop()
{
	String uscitaPerServer;
	pot =analogRead(pinPot)/4;
	Serial.print("Tensione mappata:");
	Serial.println(pot);

	if(client.connect("http://localhost:8080" , 80))
	{
		String potString = String(pot);
		client.print("Get /Arduino/FirstServlet?pippo=");
		client.print(potString);
		client.print(" HTTP/1.0");
                client.println();

	        client.stop();

	}
	else
	{
		Serial.print("Nessun client connesso \t");
		client.stop();
	}
	delay(500);
	
}

I know that Arduino connects to the server because the error message "Nessun client connesso \t" never appears.

I've done my Server setting Tomcat v8.0 for Eclipse (I can open http://localhost:8080/ )
After I've done a servlet (new Dynamic Web Project, after new Servlet) and this is the Java code:

package com.journaldev.first;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.PrintWriter;
import java.util.Date;


/**
 * Servlet implementation class FirstServlet
 */
//@WebServlet("/FirstServlet")
@WebServlet(description = "My First Servlet", urlPatterns = { "/FirstServlet" , "/FirstServlet.do"}, initParams = {@WebInitParam(name="id",value="1"),@WebInitParam(name="name",value="pankaj")})

public class FirstServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	public static final String HTML_START="<html><body>";
    public static final String HTML_END="</body></html>";
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FirstServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
//	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		// TODO Auto-generated method stub
//	}
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();


        String pippo = request.getParameter("pippo");
        Date date = new Date();
        out.println(HTML_START + "<h2>Hi There!</h2>
<h3>Date="+date +"</h3>
"+pippo+"
"+HTML_END);
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}

What my webpage return is:

Hi There!


Date=Thu Feb 26 22:11:43 CET 2015


null

Instead of null I want to get "pippo" value that should be sent by the webclient, but I don't get what doesn't work (I think Arduino code is fine, but I'm not sure).

I hope someone could help me and Thank You!

This is not correct in a few ways.

  1. No protocol (http://) in the server variable.
  2. No port either.
  3. The return value is evaluated incorrectly.
  4. Localhost is only available on the server.
if(client.connect("http://localhost:8080" , 80))

You should use the IP of the server unless the server can be resolved by dns, and localhost cannot be.

This is how to connect if the server/domain can be resolved by dns.

if(client.connect("www.google.com",80) == 1)

I've changed it as You said and it works now!
Thank you

Good afternoon, excuse me, I did what SurferTim said, but it keeps showing me "null" on the website. I've already put the protocol http, and the localhost address of my server. could you help me please.

could you help me please.

We can't see your code OR your results. Doesn't seem likely that we can help you.

This code of JAVA:
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.PrintWriter;
import java.util.Date;

/**

  • Servlet implementation class FirstServlet
    */
    //@WebServlet("/FirstServlet")
    @WebServlet(description = "My First Servlet", urlPatterns = { "/FirstServlet" , "/FirstServlet.do"}, initParams = {@WebInitParam(name="id",value="1"),@WebInitParam(name="name",value="pankaj")})

public class FirstServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public static final String HTML_START="";
public static final String HTML_END="";

/**

  • @see HttpServlet#HttpServlet()
    */
    public FirstServlet() {
    super();
    // TODO Auto-generated constructor stub
    }

/**

  • @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    */
    // protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // // TODO Auto-generated method stub
    // }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    PrintWriter out = response.getWriter();

String pippo = request.getParameter("pippo");
Date date = new Date();
out.println(HTML_START + "

Hi There!

Date="+date +"

"+pippo+" "+HTML_END); }

/**

  • @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    }

}

This code of arduino:

#include <SPI.h>
#include <Ethernet.h>
#include "DHT.h"

#define DHTPIN 2
#define DHTTYPE DHT11

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// the dns server ip 198.168.0.11
IPAddress dnServer(198,168,0,11);
// the router's gateway address:
//IPAddress gateway(192, 168, 0, 1);
// the subnet:
//IPAddress subnet(255, 255, 0, 0);
//the IP address
IPAddress ip(192,168,0,117);
EthernetClient client;
DHT dht(DHTPIN, DHTTYPE);
void setup()
{
Serial.begin(9600);
dht.begin();
Ethernet.begin(mac, ip);
delay(1000);
Serial.println("Conexion ");
Serial.println("ip: ");
Serial.println(Ethernet.localIP());
}

void loop()
{
float h = dht.readHumidity();
Serial.print("Humedad:");
Serial.println(h);

if(client.connect(dnServer ,8080))
{
String potString = String(h);
client.print("Get /WebApplication3/FirstServlet?pippo=");
client.print(potString);
client.println(" HTTP/1.1"); //script padrão para o protocolo HTTP
Serial.println(" HTTP/1.1");
client.println("Host: www.arduinoHuno.com");
Serial.println("Host: www.arduinoHuno.com");
client.println("User-Agent: Arduino");
Serial.println("User-Agent: Arduino");
client.println("Accept: text/html");
Serial.println("Accept: text/html");
client.stop();
}
else
{
Serial.print("Error de conexion \t");
client.stop();
}
delay(500);
}

The code you posted does something. You failed to tell us what it does.

    String potString = String(h);
    client.print("Get /WebApplication3/FirstServlet?pippo=");
    client.print(potString);

Do you REALLY think that print() can't handle a float?

The request type is GET or POST, not Get.

In arduino it appears to me that there is a successful connection

ip: 
192.168.0.117
 Conexion exitosa 
GET /WebApplication3/FirstServlet?pippo==50 HTTP/1.1

And in JAVA
Displays the value "null".

Hola mundo!

Dia=Thu Nov 22 11:45:22 CST 2018

null