I have Arduino with a rev3 ethernet shield and I'm trying to make work a servo connected to pin 3 work ,controlling it with the TCP client written on for Android and the other server on Arduino but with port 8888 the tcp client says "198.168.1.214:8888 connection refused" , Do not know why?
Any encryption is there which I'm not able to detect and code for?
Android Code:
/*
* Simple Android -> Arduino TCP Client
*
* 20.04.2012
* by Laurid Meyer
*
* http://www.lauridmeyer.com
*
*/
package com.lauridmeyer.tests;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
public class SimpleAndroidTCPClientActivity extends Activity {
TextView textlog;//Log for outputs
Button buttonConnect;//(dis)connect Button
SeekBar seekBar;//Seekbar to control the Servo
TextView seekBarValue;//Textfield displaing the Value of the seekbar
Boolean connected=false;//stores the connectionstatus
DataOutputStream dataOutputStream = null;//outputstream to send commands
Socket socket = null;//the socket for the connection
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//connect the view and the objects
buttonConnect = (Button)findViewById(R.id.connect);
textlog = (TextView)findViewById(R.id.textlog);
seekBar = (SeekBar)findViewById(R.id.seekbar);
seekBarValue = (TextView)findViewById(R.id.seekbarvalue);
textlog.setText("Starting Client");//log that the App launched
changeConnectionStatus(false);//change connectionstatus to "disconnected"
//Eventlisteners
buttonConnect.setOnClickListener(buttonConnectOnClickListener);
seekBar.setOnSeekBarChangeListener(seekbarchangedListener);
}
// ----------------------- SEEKBAR EVENTLISTENER - begin ----------------------------
SeekBar.OnSeekBarChangeListener seekbarchangedListener = new SeekBar.OnSeekBarChangeListener(){
//Methd is fired everytime the seekbar is changed
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
String valueOfseekbar = String.valueOf(progress);//save the value of the seekbar in a string
seekBarValue.setText(valueOfseekbar);//update the value in the textfield
if(connected){//if the socket is connected
try {
//send a string to the Arduino Server in the form of "set: -seekbarvalue- \n"
dataOutputStream.writeBytes("set:"+valueOfseekbar+'\n');
}catch (UnknownHostException e) {//catch and
outputText(e.getMessage());//display errors
} catch (IOException e) {//catch and
outputText(e.getMessage());//display errors
}
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
};
// ----------------------- SEEKBAR EVENTLISTENER - end ----------------------------
// ----------------------- CONNECTION BUTTON EVENTLISTENER - begin ----------------------------
Button.OnClickListener buttonConnectOnClickListener = new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
if(!connected){//if not connected yet
outputText("connecting to Server");
try {//try to create a socket and outputstream
socket = new Socket("192.168.1.214", 8888);//create a socket
dataOutputStream = new DataOutputStream(socket.getOutputStream());//and stream
outputText("successfully connected");//output the connection status
changeConnectionStatus(true);//change the connection status
} catch (UnknownHostException e) {//catch and
outputText(e.getMessage());//display errors
changeConnectionStatus(false);
} catch (IOException e) {//catch and
outputText(e.getMessage());//display errors
changeConnectionStatus(false);
}
}else{
outputText("disconnecting from Server...");
try {//try to close the socket
socket.close();
outputText("successfully disconnected");
changeConnectionStatus(false);//change the connection status
} catch (UnknownHostException e) {//catch and
outputText(e.getMessage());//display errors
} catch (IOException e) {//catch and
outputText(e.getMessage());//display errors
}
}
}};
// ----------------------- CONNECTION BUTTON EVENTLISTENER - END ----------------------------
// Method changes the connection status
public void changeConnectionStatus(Boolean isConnected) {
connected=isConnected;//change variable
seekBar.setEnabled(isConnected);//enable/disable seekbar
if(isConnected){//if connection established
buttonConnect.setText("disconnect");//change Buttontext
}else{
buttonConnect.setText("connect");//change Buttontext
}
}
//Method appends text to the textfield and adds a newline character
public void outputText(String msg) {
textlog.append(msg+"\n");
}
}
Arduino Ethernet code:
#include <SPI.h>
#include <Ethernet.h>
#include <Servo.h>
// Enter a MAC address, IP address and Portnumber for your Server below.
// The IP address will be dependent on your local network:
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x78, 0xE1 };
byte ip[] = {192,168,1,214};
int serverPort=8888;
int servoPin=3;
// Initialize the Ethernet server library
// with the IP address and port you want to use
Server server(serverPort);
//Initialise the Servo Library
Servo myservo;
void setup()
{
// start the serial for debugging
Serial.begin(9600);
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
Serial.println("Server started");//debug
myservo.attach(servoPin); // attache the servo on the givven servoPin
}
void loop()
{
// listen for incoming clients
Client client = server.available();
if (client) {
String commandStr ="";//Connamdstring where incoming commands are stored
while (client.connected()) {//if a client is connected
if (client.available()) {//and available
//reading the inputs from the client
char c = client.read();
commandStr+=c;//and ands them to the commands String
if (c == '\n') {//if a newline character is sent (commandline is fully recieved)
Serial.println("Command:"+commandStr);//output the command
if(commandStr.indexOf("set:")==0){//if the command begins with "set:"
String value=commandStr;//store the command into a new String
value.replace("set:", " "); //replace the "set:" from the command string
Serial.println("Set a Servo:"+value);//output the servo value
myservo.write(convertToInt(value));//set the servo to the recieved value
}
commandStr="";//reset the commandline String
}
}
}
// give the client time to receive the data
delay(1);
// close the connection:
client.stop();
}
}
int convertToInt(String value){
char buf[value.length()];
value.toCharArray(buf,value.length());
return atoi(buf);
}