Skip to content

Instantly share code, notes, and snippets.

@benwis
Created November 16, 2014 21:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save benwis/fe56826a6e530eb28e6d to your computer and use it in GitHub Desktop.
Save benwis/fe56826a6e530eb28e6d to your computer and use it in GitHub Desktop.
TCP/UDP client and server
SYSTEM_MODE(MANUAL);
// UDP Port used for two way communication
unsigned int localPort = 1900;
unsigned int trigger = D0;
//Creates buffer variable and gives it a size
#define MAX_SIZE 1024
char buffer[MAX_SIZE];
//Creates UDP manipulation variable
char message[MAX_SIZE];
//Creates TCP message Receive buffer
char response;
unsigned int once = 0;
String webAddress;
unsigned int ssdpport = 1900;
//This is the multicast broadcast IP
IPAddress ip( 239, 255, 255, 250 );
IPAddress addr( 0, 0, 0, 0 );
//Stores Ip address as a char for the packetification
char host[24];
// An UDP instance to let us send and receive packets over UDP
UDP Udp;
//TCP initialization
TCPClient client;
TCPServer server = TCPServer(64321);
void setup() {
Spark.disconnect();
Serial.begin(9600);
WiFi.on();
// Connects to a network with a specified authentication procedure.
// Options are WPA2, WPA, or WEP.
WiFi.clearCredentials();
//WiFi.setCredentials("DIRECT-thQ0:DSC-QX10", "1R7WJZ7U", WPA2);
WiFi.setCredentials("here", "wegoagain", WPA);
WiFi.connect();
// while(!Serial.available()) Spark.process();
while (!WiFi.ready()) {
Serial.println("Waiting for WiFi...");
Spark.process();
delay(1000);
}
}
void loop() {
Spark.process();
if ((!once) && (WiFi.ready() == true)) {
addr = WiFi.localIP();
if ((addr[0] != 0 ||addr[1] != 0 || addr[2] != 0 || addr[3] != 0) && (once == 0)) {
WiFi.ping(WiFi.gatewayIP());
sprintf(host, "%d.%d.%d.%d", addr[0], addr[1], addr[2], addr[3]);
Serial.println("UDP has begun");
once = 1;
Udp.begin(localPort);
server.begin();
Spark.process();
Serial.println(WiFi.localIP());
//Sends SSDP Packet to multicast address
Udp.beginPacket(ip, ssdpport);
Spark.process();
Udp.write("M-SEARCH * HTTP/1.1\r\nHOST: 239.255.255.250:1900\r\nMAN: \"ssdp:discover\"\r\nMX: 5\r\nST: ssdp:all\r\n\r\n");
Spark.process();
Udp.endPacket();
Spark.process();
}
}
//Module to parse received SSDP packet
Spark.process();
int rcvd = Udp.parsePacket();
if (rcvd > 0) {
//Serial.print("Received UPD packet, length: ");
//Serial.println(rcvd);
//Reads UDP data into buffer
Udp.read(buffer, MAX_SIZE);
//Prints UDP data from buffer for examination
// Serial.println(buffer);
//Copies buffer data into secondary variable
// message = buffer;
// const char * bufferPtr = buffer;
String strMessage = String ( buffer ); //"Some String Value";
int locationAt = strMessage.indexOf ( "http://" );
if (locationAt > 0)
{
int addressEndsAt = strMessage.indexOf ( "/", locationAt + 7 );
String webAddress = strMessage.substring ( locationAt, addressEndsAt );
// webAddress.replace ( "LOCATION:", "" );
//webAddress.replace ( "http://", "" );
webAddress.trim ();
//Serial.print("webAddress ...");
//Serial.println(webAddress);
//Serial.println("done!!!");
Spark.process();
}
else{
Serial.println(strMessage);
}
//Complains that received packet is too big
if (rcvd > MAX_SIZE) {
Serial.println("Too large packet");
while (Udp.available())
Udp.read();
Spark.process();
}
}
// if (digitalRead(trigger)==HIGH)
// {
//Connects to the client to initiate TCP connection
client.connect("192.168.8.1",80);
{
Serial.println("Connected to camera");
//Encodes JSON Data so that the camera will understand it.
//Also adds POST data and sends packet
String request = "{\"method\": \"actTakePicture\", \"params\":\"[]\", \"id\": \"1\", \"version\": \"1.0\"}";
client.println("POST /camera HTTP/1.1");
client.println("Host: " + webAddress);
client.println("Accept: application/json-rpc");
client.print("Content-Length: ");
client.println(request.length());
client.println("Content-Type: application/json-rpc");
client.println(request);
Spark.process();
}
if (client.connected()) {
Spark.process();
// echo all available bytes back to the client
while (client.available()) {
server.write(client.read());
Spark.process();
response = client.read();
Spark.process();
}
Serial.println(response);
}
else {
// if no client is yet connected, check for a new connection
client = server.available();
Spark.process();
}
// }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment