Spark & telnet problem

Hello,
does anyone have an idea what I’m doing wrong?

#
TCPServer server = TCPServer(90);
TCPClient client;

int led = D7;
int incomingByte;      


void setup()
{

 pinMode(led, OUTPUT);
  server.begin();

  Serial.begin(9600);
  while(!Serial.available()) SPARK_WLAN_Loop();

  Serial.println(Network.localIP());
  Serial.println(Network.subnetMask());
  Serial.println(Network.gatewayIP());
  Serial.println(Network.SSID());
}

void loop()
{
    if (client.available()) {

    incomingByte = client.read();
    
    Serial.write(incomingByte);

    if (incomingByte == '1')
    {
      digitalWrite(led, HIGH);
      Serial.println("LED ist eingeschaltet!");
    } 

    if (incomingByte == '0')
    {
      digitalWrite(led, LOW);
      Serial.println("LED ist ausgeschaltet!");
    }

  }
}

#

None of the led function on the terminal ?!

Regards from Germany

Marc

Hi @MarcGermany,

That program won’t do anything until you open a serial terminal and hit a key to start it up. It also isn’t listening for connections or start a server, it just checks the socket that hasn’t been opened. I think you want something like:

void loop() {

  if (client.connected()) {
    //do things
  }
  else {
    // if no client is yet connected, check for a new connection
    client = server.available();
  }
}

Thanks,
David

this should get you started:

// import libs
#include "application.h"
#include "spark_disable_cloud.h"

// init vars
TCPServer server = TCPServer(23);
TCPClient client;

void setup()
{
    // set led as output
    pinMode(D7, OUTPUT);
    
    // start server
    server.begin();
}
  
void loop()
{
    if (client.connected())
    {
        // loop until data stops
        while (client.available())
        {
            // handle keypress events
            switch ((char)client.read())
            {
                case '1':
                    // 1 turns on
                    digitalWrite(D7, HIGH);
                    break;
                case '0':
                    // 0 turns off
                    digitalWrite(D7, LOW);
                    break;
                case 'q':
                    // q quits
                    client.stop();
                    break;
            }
        }
    }
    else
    {
        // wait for new connections
        client = server.available();
    }
}

1 Like