TCPServer isn't working

Hello there,

I am currently working on a Photon project for school, and it involves sending image data from my photon to an HTML-page.

I have a Photon connected to a camera module. This module takes a picture, and then sends the data in packages containing bytes, to the Photon.
My current objective is to send this data from the Photon to an HTML-page, so that I can view the image.
My next objective will be a video stream to that HTML-page.

I haven't made up my mind about how I want to tackle this problem, but I've been investigating the possibility of using a TCPServer on my photon to send the byte array somewhere else.

An example I found on this topic here:

Shows a code that I can use to test it out, but unfortunately it does not seem to be working for me. On the html-page provided I can get the IP address from the cloud variable, but I cant seem to make requests to it.

What could be the problem?

The exact code I'm referring to is this:

TCPServer server = TCPServer(80);
TCPClient client;

char   addr[16];
char   myInput[30];
char   myIncoming;
int    myLoop1;
String myInStr;


void setup()
{

   pinMode(D7, OUTPUT);
   digitalWrite(D7, HIGH); 
   delay(1000);
   digitalWrite(D7, LOW); 
   delay(1000); 
   digitalWrite(D7, HIGH); 
   delay(1000);
   digitalWrite(D7, LOW); 
   delay(1000); 
   delay(1000);
   delay(1000);  // give a few seconds to reflash the core if it gets unresponsive on startup
   digitalWrite(D7, HIGH); 
   delay(300);
   digitalWrite(D7, LOW); 


  server.begin();
  IPAddress localIP = WiFi.localIP();
  sprintf(addr, "%u.%u.%u.%u", localIP[0], localIP[1], localIP[2], localIP[3]);
  Spark.variable("Address", addr, STRING);
  Spark.variable("myIn",  myInput, STRING);  
  Spark.variable("myLoop1", &myLoop1, INT);
}


void loop() {
    // listen for incoming clients
    client = server.available();
    if (client) {


        myLoop1 = 0;
        myInput[0] = '\0';

        boolean currentLineIsBlank = true;
        while (client.connected()) {


            if (client.available()) {




                myIncoming = client.read();       // read from the http request

               if (myLoop1 < 29 ){                 // http request should be much longer than 29 characters!
                   myInput[myLoop1] = myIncoming; // put the character into an array

                } else {                           // read enough information from the http request

                    myInput[myLoop1] = '\0';      // helps make a char array compatible with a string.

                    myInStr = myInput;
                    myInStr.toUpperCase();

                   if (myInStr.indexOf("D7-ON") >= 0){   digitalWrite(D7, HIGH); }  
                   if (myInStr.indexOf("D7-OFF") >= 0){  digitalWrite(D7, LOW); }  



                   }

                 myLoop1++;




                if (myIncoming == '\n' && currentLineIsBlank) {


                    //client.println("<H1>Hello World.</h1>");   // use for debugging to check if http request can get returned


                    delay(1);
                    break;
                }
                if (myIncoming == '\n') {          // you're starting a new line
                    currentLineIsBlank = true;
                }
                else if (myIncoming != '\r') {     // you've gotten a character on the current line
                    currentLineIsBlank = false;
                }


            }
        }
        // give the web browser time to receive the data
        delay(1);
    }
    client.flush();
    client.stop();
}










/*

MAKE THIS HTML PAGE TO COMMUNICATE WITH YOUR CORE



<a  target="myI" href="https://api.spark.io/v1/devices/{CORE-ID}/Address?access_token={ACCESS-TOKEN}" >Address</a><br>


<a  target="myI" href="http://192.145.1.65?D7-ON" >D7-ON</a>...
<a  target="myI" href="http://192.145.1.65?D7-OFF" >D7-OFF</a><br><br><br>

<iframe name="myI" width=500 height=400></iframe>

I hope someone can point me in the right direction regarding this TCPServer example, but if someone has an entirely different idea to deal with my underlying objective: I am open to suggestions.

EDIT: I managed to get this example working on another WiFi network, so I'm going to continue exploring this path. If anyone can tell me why it might not work on certain networks, while it does on others, I'm still interested in that.

Thanks in advance!

@Jelmertje I wrote that code a while back, not sure if it is the best option as it was written for small amounts of data, I don’t even remember if it worked well as I eventually made a really hacked socket for really fast but flaky communication with the Photon near the bottom of that topic.

What camera do you have setup with the Core? I have used the http://charmedlabs.com/default/pixy-cmucam5/ but that does not send images it sends smaller amounts of data about detected colours to the core/photon.

With my students I almost always used https://github.com/hpssjellis/spark-core-web-page-html-control the html page ajaxbetter.html is the one to look at, but once again this is for small amounts of data.

For video I have always used other methods much better suited for this such as https://www.skype.com/en/, or IVIDEO at https://www.ivideon.com/ or https://ifttt.com/ may have a solution.

See video at https://www.youtube.com/watch?v=9gYDSeOqchU of a core with a cell phone as both the wifi hotspot and Skype setup to see what the car is seeing so you can drive the car over wifi

But these are not really connected with the Photon. Good luck, If I can get your camera I might be able to give some other ideas. I have several $3 web cams but I always hook them up using ivideon.

1 Like