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!