Here's a menu driven TCP telnet connection for testing

Here is some code I used to test IP connection to a local web server.
I couldn’t the web server to accept GET requests from the Photon.
I found out it was the firewall.

I hope this can help save somebody some time.

int     xcmd;

char    xSSID[250];

char    xoutstring[250];

IPAddress mainpc(10,0,0,142);

TCPClient client;

int pht1 = A0;
int pwr1 = A5;

int pht1_value;

void setup() {
    pinMode(pwr1, OUTPUT);
    pinMode(pht1, INPUT);

    digitalWrite(pwr1,HIGH);

    Serial.begin(9600);
}

void loop() {
print_Menu();

while(!Serial.available()) Particle.process();
xcmd = Serial.read();


Serial.print("Command entered: ");
sprintf(xoutstring,"%c",xcmd);
Serial.println(xoutstring);

switch(xcmd)
    {
        case 'a':
            Serial.println("Connected to SSID");
            Serial.println("");
            Serial.println(WiFi.SSID());
            Serial.println("");
            Serial.println("");
            break;

        case 'b':
            Serial.print("Connected to SSID:");
            Serial.println(WiFi.SSID());
            Serial.print("Signal Strength:");
            Serial.print(WiFi.RSSI());
            Serial.println("db");
            Serial.println("");
            Serial.println("");
            break;

        case 'c':
            Serial.print("Local IP Address:");
            Serial.println(WiFi.localIP());
            Serial.print("Subnet Mask:");
            Serial.println(WiFi.subnetMask());
            Serial.println("");
            Serial.println("");
            break;

        case 'd':
            Serial.print("Pinging Router:");
            Serial.println(WiFi.gatewayIP());
            Serial.println("");
            Serial.print("Number of Packets: ");
            Serial.println(WiFi.ping(WiFi.gatewayIP()));
            Serial.println("");
            Serial.println("");
            break;

        case 'e':
            Serial.print("Pinging Main:");
            Serial.println(mainpc);
            Serial.println("");
            Serial.print("Number of Packets:");
            Serial.println(WiFi.ping(mainpc));
            Serial.println("");
            Serial.println("");
            break;

        case 'f':
            Serial.println("Connecting to Main");
            if (client.connect(mainpc, 80))
                {
                    Serial.println("connected");
                    pht1_value = analogRead(pht1);

                    sprintf(xoutstring,"GET /addlightvalue.asp?tstamp=%lu&lvalue=%d HTTP/1.0",Time.now(),pht1_value);
                    client.println(xoutstring);
                    client.println("Content-Length: 0");
                    client.println();


                    while(client.connected())
                        {
                            if(client.available())
                                {
                                char c = client.read();
                                Serial.print(c);
                                }
                            Particle.process();
                        }
                    client.stop();
                    Serial.println("Disconnected");

                }
            else
                {
                    Serial.println("connection failed");
                }

    }
}

void print_Menu()
{
    Serial.println("Menu");
    Serial.println("a. Display SSID");
    Serial.println("b. Display Signal Strength");
    Serial.println("c. Display Local IP");
    Serial.println("d. Ping Router");
    Serial.println("e. Ping Main");
    Serial.println("f. Connect to Main");
    Serial.println("");
    Serial.println("");
}
3 Likes

@jbstcyr, great utility! A keeper for sure :wink:

I tried this code out and it works for me. I can ping my Photon from my PC. For case ‘f’ (Connect to Main) the Photon says it’s “connected”. What would be the simplest way to test this case from the PC side? Browser? Putty? Chrome says ‘webpage not available’ when I enter the Photon’s IP address:80.
Thanks.

@JWaters, the app does not run a web server on the Photon, just a pure TCP client to test the TCP path between a Photon and a target server. :smile:

OK. Then where the code does:

                while(client.connected())
                    {
                        if(client.available())
                            {
                            char c = client.read();
                            Serial.print(c);
                            }
                        Particle.process();
                    }

How do I test this code and get ‘c’?

@JWaters, this code is designed for the OP’s server specifically where he posts a value to it and prints out the response to Serial. That portion of the code needs to be replaced for your own purposes. You could, for example, send a request to a public server and just print out the response to prove the GET/POST worked. :smile: