Hello everyone.
I’d thought i would share this for the community. THIS IS STILL A WORK IN PROCESS.
What it does:
steps:
1: spark broadcasts an udp packet on the network under a port.
2: java listens to the same port.
3: if the packet if found, takes the ip adress from that packet.
4: creates a TCP connection
5: Writes something on the connection.
6: spark bounces the message back.
Currently the code is not optimized, this is just a working code first draft.
broadcast will be done with an interrupt timer later.
Do note, my primary language is dutch, and this is still a work in process.
Some commentary can be in dutch, when i have a final draft i will do a full translation.
spark code.
//tcp server / udp server.
TCPServer server = TCPServer(23);
TCPClient client;
UDP Udp;
int minuut;
void setup()
{
// start listening for clients
server.begin();
Udp.begin(4700);
minuut = 0;
}
void loop()
{
connecttcp();
broadcast();
}
//accepteerd connectie en bounced bericht.
void connecttcp(){
if (client.connected()) {
// echo all available bytes back to the client
while (client.available()) {
server.write(client.read());
//if(client.read() == '1'){
//server.write("een ontvangen!");
//}
}
} else {
// if no client is yet connected, check for a new connection
client = server.available();
}
}
//broadcast een bericht op udp
void broadcast(){
minuut++;
if(minuut == 1000){
//Udp.beginPacket(IPAddress (255,255,255,255), 4800);
Udp.beginPacket(IPAddress (192,168,1,255), 4800);
Udp.write("ip-broadcast");
Udp.endPacket();
minuut = 0;
}
//delay(5000);
}
java classes
public class Apl {
public static void main(String[] args) throws Exception
{
//zoeken naar de udp broadcast functie van de spark.
UDPServer udpserver = new UDPServer();
System.out.println("Spark zit op ip: " + udpserver.getIP().toString());
//Zet een TCP verbinding op.
TCPClient client = new TCPClient(udpserver.getIP());
client.run();
}
}
test
// Client Side
import java.io.*;
import java.net.*;
public class TCPClient {
private InetAddress ip;
TCPClient(InetAddress ip)
{
this.ip = ip;
}
public void run() {
try {
//socket poort van de spark, in te stellen in de firmware van spark zelf.
int serverPort = 23;
System.out.println("Verbinding maken met spark op poort: " + serverPort);
Socket socket = new Socket(ip,serverPort);
System.out.println("Connectie opgezet met: " + socket.getRemoteSocketAddress());
PrintWriter toServer =
new PrintWriter(socket.getOutputStream(),true);
BufferedReader fromServer =
new BufferedReader(
new InputStreamReader(socket.getInputStream()));
//Bericht versturen(op dit moment krijg je een bounce).
toServer.println("Dit is wat ik verstuur naar de spark");
//bericht ontvangen
String line = fromServer.readLine();
//bericht weergeven.
System.out.println("Bericht ontvangen: \'" + line + "\' van de spark");
//alles afsluiten.
toServer.close();
fromServer.close();
socket.close();
}
//error handling.
catch(UnknownHostException ex) {
ex.printStackTrace();
}
catch(IOException e){
e.printStackTrace();
}
}
}
import java.io.*;
import java.net.*;
//Deze klass is hier om het ip adress te verkrijgen van de spark.
class UDPServer
{
//private waarde ip adress.
private static InetAddress ip;
UDPServer() throws Exception
{
//zet ip op null, blijf zoeken voor 1500ms.
ip = null;
search();
Thread.sleep(1500);
}
public InetAddress getIP(){
return ip;
}
//zoek naar de broadcast van de spark.
public void search() throws Exception
{
DatagramSocket serverSocket = new DatagramSocket(4800);
byte[] receiveData = new byte[1024];
while(ip == null)
{
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
String sentence = new String( receivePacket.getData());
//System.out.println("RECEIVED: " + sentence);
InetAddress IPAddress = receivePacket.getAddress();
int port = receivePacket.getPort();
//zet het ip adress vast.
ip = IPAddress;
}
}
}
my todo list:
broadcast on interrupt timer
write string on both lines.
do something on the spark with data send.
If you have any questions, ask away ;).