TCP example application failing to connect on Tracker One

I am trying to run the following code to initialize a TCP connection on the particle Tracker One which is listed under the Device OS API:

// EXAMPLE USAGE

TCPClient client;
byte server[] = { 74, 125, 224, 72 }; // Google
void setup()
{
  // Make sure your Serial Terminal app is closed before powering your device
  Serial.begin(9600);
  // Wait for a USB serial connection for up to 30 seconds
  waitFor(Serial.isConnected, 30000);

  Serial.println("connecting...");

  if (client.connect(server, 80))
  {
    Serial.println("connected");
    client.println("GET /search?q=unicorn HTTP/1.0");
    client.println("Host: www.google.com");
    client.println("Content-Length: 0");
    client.println();
  }
  else
  {
    Serial.println("connection failed");
  }
}

void loop()
{
  if (client.available())
  {
    char c = client.read();
    Serial.print(c);
  }

  if (!client.connected())
  {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    for(;;);
  }
}

I have tried utilizing this in the tracker-edgeV15 firmware as well but have similar issues and I see that the connection fails.

As an aside the API example for the filesystem open doesn’t work either. This may be due to the directory not being present.
This works:

int fd = open("test.txt", O_RDWR | O_CREAT | O_TRUNC);

Are you actually running that code with that IP address? It appears that you cannot run that code verbatim as Google is no longer using that IP address.

rickkas7, thanks so much for your prompt reply. I did try running the example code verbatim as a starting point. I have since adjusted the code to have the IP 8.8.8.8 which I believe is the IPv4 address of googles servers. I tried the example code listed above with the address changed to:

byte server[] = { 8, 8, 8, 8 }; // Google

This did not work. I wasn't sure if the cell modem was appropriately initialized so I tried to run the TCP example code on top of the tracker-edgeV15 software for the Tracker One:

#include "Particle.h"

#include "tracker_config.h"
#include "tracker.h"
#include <fcntl.h>

SYSTEM_THREAD(ENABLED);
SYSTEM_MODE(SEMI_AUTOMATIC);

PRODUCT_ID(TRACKER_PRODUCT_ID);
PRODUCT_VERSION(TRACKER_PRODUCT_VERSION);


STARTUP(
    Tracker::startup(););

SerialLogHandler logHandler(115200, LOG_LEVEL_TRACE, {
                                                         {"app.gps.nmea", LOG_LEVEL_INFO},
                                                         {"app.gps.ubx", LOG_LEVEL_INFO},
                                                         {"ncp.at", LOG_LEVEL_INFO},
                                                         {"net.ppp.client", LOG_LEVEL_INFO},
                                                     });

TCPClient client;
byte server[] = { 8, 8, 8, 8 }; // Google

void setupTCP()
{
  Log.info("connecting...");

  if (client.connect(server, 80))
  {
    Log.info("connected");
    client.println("GET /search?q=unicorn HTTP/1.0");
    client.println("Host: www.google.com");
    client.println("Content-Length: 0");
    client.println();
  }
  else
  {
    Log.info("connection failed");
  }
}

void loopTCP()
{
  if (client.available())
  {
    char c = client.read();
    //Log.info(c);
  }

  if (!client.connected())
  {
    //Serial.println();
    Log.info("disconnecting.");
    client.stop();
    for(;;);
  }
}

void testFileOpen()
{
    int fd = open("test.txt", O_RDWR | O_CREAT | O_TRUNC);
    if (fd != -1)
    {
        Log.info("opened");
        close(fd);
    }
    else
    {
        Log.info("not opened");
    }
}

void setup()
{
    waitFor(Serial.isConnected, 30000);
    testFileOpen();

    Tracker::instance().init();
}

void loop()
{
    Tracker::instance().loop();

    if(millis()>120000)
    {
        setupTCP();
        loopTCP();
    }
}

Here is the output log from this code (as much as my serial logger CoolTerm stores):

sh secure connection
0000003730 [comm.dtls] INFO: session has 0 uses
0000003738 [comm.dtls] INFO: (CMPL,RENEG,NO_SESS,ERR) restoreStatus=0
0000003739 [comm.dtls] INFO: out_ctr 0,1,0,0,0,0,3,41, next_coap_id=950
0000003739 [comm.dtls] INFO: restored session from persisted session data. next_msg_id=2384
0000003740 [comm.dtls] INFO: session cmd (CLS,DIS,MOV,LOD,SAV): 2
0000003756 [hal] TRACE: Cached ESP32 NCP firmware version: 7
0000003956 [hal] TRACE: Cached ESP32 NCP firmware version: 7
0000003968 [comm.protocol.handshake] INFO: Skipping HELLO message
0000003971 [comm.protocol] INFO: Checksum has not changed; not sending application DESCRIBE
0000003971 [comm.protocol] INFO: Checksum has not changed; not sending subscriptions
0000004329 [comm.dtls] INFO: session cmd (CLS,DIS,MOV,LOD,SAV): 4
0000004329 [comm.protocol] TRACE: Reply recieved: type=2, code=0
0000004330 [comm.protocol] TRACE: message id 2385 complete with code 0.00
0000004330 [comm.protocol] TRACE: rcv'd message type=13
0000004531 [comm.protocol] TRACE: Reply recieved: type=2, code=0
0000004531 [comm.protocol] TRACE: message id 2386 complete with code 0.00
0000004532 [comm.protocol] TRACE: rcv'd message type=13
0000004548 [comm.protocol] TRACE: Reply recieved: type=2, code=0
0000004548 [comm.protocol] TRACE: message id 2387 complete with code 0.00
0000004549 [comm.protocol] TRACE: rcv'd message type=13
0000004649 [system] INFO: Cloud connected
0000004720 [app.gps.ubx] INFO: enable PUBX-POSITION
0000004727 [app.gps.ubx] INFO: enable PUBX-SVSTATUS
0000004730 [app.gps.ubx] INFO: enable PUBX-TIME
0000004754 [app.gps.ubx] INFO: enable GPS
0000004754 [app.gps.ubx] INFO: enable QZSS
0000004754 [app.gps.ubx] INFO: enable SBAS
0000004754 [app.gps.ubx] INFO: enable Galileo
0000004755 [app.gps.ubx] INFO: enable BeiDou
0000004755 [app.gps.ubx] INFO: enable GLONASS
0000004758 [app.gps.ubx] INFO: set to power management mode 0
0000004761 [app.gps.ubx] INFO: set dynamic platform model to 0
0000004764 [app] TRACE: evaluatePublish first
0000004765 [app] TRACE: waiting for stable GNSS lock for triggers
0000004767 [app] INFO: cloud sent: {"cmd":"sync","time":1636123842,"hash":"78B454AB23887B369800A1766B21C878"}
0000004768 [app] TRACE: evaluatePublish first
0000004768 [app] TRACE: waiting for stable GNSS lock for triggers
0000005008 [comm.protocol] TRACE: Reply recieved: type=2, code=0
0000005008 [comm.protocol] TRACE: message id 2388 complete with code 0.00
0000005010 [comm.protocol] TRACE: rcv'd message type=13
0000005574 [comm.protocol] TRACE: rcv'd message type=8
0000005768 [app] TRACE: evaluatePublish first
0000005768 [app] TRACE: waiting for stable GNSS lock for triggers
0000006768 [app] TRACE: evaluatePublish first
0000006768 [app] TRACE: waiting for stable GNSS lock for triggers
0000007768 [app] TRACE: evaluatePublish first
0000007768 [app] TRACE: waiting for stable GNSS lock for triggers
0000008768 [app] TRACE: evaluatePublish first
0000008768 [app] TRACE: waiting for stable GNSS lock for triggers
0000009768 [app] TRACE: evaluatePublish first
0000009768 [app] TRACE: publishing from triggers
0000009768 [app] INFO: publishing now...
0000009770 [app] TRACE: extending execution
0000009790 [system.nm] TRACE: Request to power on the interface
0000010109 [hal] ERROR: Not found response
0000010129 [hal] ERROR: CMD0 response error, expect 0x1, response 0
0000010130 [hal] ERROR: Please restart slave and test again,error code:264
0000012193 [ncp.esp32.at] TRACE: > AT
0000012195 [ncp.esp32.at] TRACE: < OK
0000013196 [ncp.esp32.client] TRACE: NCP ready to accept AT commands
0000013198 [ncp.esp32.at] TRACE: > AT+MVER
0000013200 [ncp.esp32.at] TRACE: < 7
0000013200 [ncp.esp32.at] TRACE: < OK
0000013202 [ncp.esp32.at] TRACE: > AT+GETMAC=0
0000013205 [ncp.esp32.at] TRACE: < +GETMAC: "30:ae:a4:ac:3d:04"
0000013205 [ncp.esp32.at] TRACE: < OK
0000013208 [ncp.esp32.at] TRACE: > AT+CMUX=0
0000013210 [ncp.esp32.at] TRACE: < OK
0000013211 [ncp.esp32.mux] INFO: Starting GSM07.10 muxer
0000013211 [ncp.esp32.mux] INFO: Openning mux channel 0
0000013212 [ncp.esp32.mux] INFO: GSM07.10 muxer thread started
0000013219 [ncp.esp32.mux] INFO: Openning mux channel 1
0000013230 [ncp.esp32.at] TRACE: > AT
0000013239 [ncp.esp32.at] TRACE: < OK
0000013243 [ncp.esp32.at] TRACE: > AT+CWDHCP=0,3
0000013250 [ncp.esp32.at] TRACE: < OK
0000013250 [ncp.esp32.client] TRACE: NCP state changed: 1
0000013250 [system.nm] TRACE: Interface 5 power state changed: 2
0000013255 [ncp.esp32.at] TRACE: > AT+CWLAP
0000015367 [ncp.esp32.at] TRACE: < +CWLAP:(0,"Orange TV.b",-53,"fa:8f:ca:78:23:d3",6)
0000015368 [ncp.esp32.at] TRACE: < +CWLAP:(3,"Vita-Corp",-54,"74:ac:b9:e0:ec:bb",11)
0000015369 [ncp.esp32.at] TRACE: < +CWLAP:(3,"Vita-Guest",-57,"76:ac:b9:90:ec:bb",11)
0000015370 [ncp.esp32.at] TRACE: < +CWLAP:(3,"Vita-Corp",-62,"74:ac:b9:e0:ea:3c",11)
0000015372 [ncp.esp32.at] TRACE: < +CWLAP:(3,"Vita-Guest",-63,"76:ac:b9:90:ea:3c",11)
0000015376 [ncp.esp32.at] TRACE: < +CWLAP:(3,"Vita-Corp",-65,"74:ac:b9:e0:ed:72",11)
0000015377 [ncp.esp32.at] TRACE: < +CWLAP:(3,"Vita-Guest",-66,"76:ac:b9:90:ed:72",11)
0000015378 [ncp.esp32.at] TRACE: < +CWLAP:(3,"LUND",-69,"e0:cb:bc:88:a7:6e",1)
0000015380 [ncp.esp32.at] TRACE: < +CWLAP:(0,"Chromecast - Blueberry.m",-70,"fa:8f:ca:59:53:ef",6)
0000015381 [ncp.esp32.at] TRACE: < +CWLAP:(3,"",-71,"fc:ec:da:ff:49:3b",1)
0000015382 [ncp.esp32.at] TRACE: < +CWLAP:(0,"Derek M Office.b",-71,"fa:8f:ca:90:4b:25",6)
0000015386 [ncp.esp32.at] TRACE: < +CWLAP:(5,"",-73,"de:cb:bc:88:a7:6e",1)
0000015388 [ncp.esp32.at] TRACE: < +CWLAP:(3,"Stargate",-74,"f6:9f:c2:6c:94:13",6)
0000015389 [ncp.esp32.at] TRACE: < +CWLAP:(0,"Chromecast - Plum.b",-75,"fa:8f:ca:91:f8:d5",6)
0000015391 [ncp.esp32.at] TRACE: < +CWLAP:(3,"",-75,"fe:ec:da:17:0b:93",6)
0000015392 [ncp.esp32.at] TRACE: < +CWLAP:(3,"",-75,"82:83:c2:d3:a1:fd",11)
0000015393 [ncp.esp32.at] TRACE: < +CWLAP:(5,"corp-wifi",-75,"fc:ec:da:ff:60:55",11)
0000015395 [ncp.esp32.at] TRACE: < +CWLAP:(0,"UID Wi-Fi_IoT",-75,"fe:ec:da:9f:60:55",11)
0000015396 [ncp.esp32.at] TRACE: < +CWLAP:(3,"",-75,"fe:ec:da:af:60:55",11)
0000015397 [ncp.esp32.at] TRACE: < +CWLAP:(3,"DDS extreme",-75,"74:83:c2:d3:a1:fd",11)
0000015399 [ncp.esp32.at] TRACE: < +CWLAP:(3,"",-76,"fc:ec:da:77:0b:93",6)
0000015403 [ncp.esp32.at] TRACE: < +CWLAP:(3,"",-79,"06:18:d6:a0:55:47",1)
0000015404 [ncp.esp32.at] TRACE: < +CWLAP:(5,"corp-wifi",-79,"04:18:d6:a0:55:47",1)
0000015405 [ncp.esp32.at] TRACE: < +CWLAP:(3,"pkasemir corp1009_IoT",-81,"f6:9f:c2:0a:f8:fc",1)
0000015407 [ncp.esp32.at] TRACE: < +CWLAP:(3,"",-81,"fa:9f:c2:0a:f8:fc",1)
0000015408 [ncp.esp32.at] TRACE: < +CWLAP:(0,"pkasemir guest 6",-81,"f0:9f:c2:0a:f8:fc",1)
0000015409 [ncp.esp32.at] TRACE: < +CWLAP:(3,"pkasemir-lan100",-82,"74:ac:b9:a4:77:d5",1)
0000015410 [ncp.esp32.at] TRACE: < +CWLAP:(3,"",-82,"fa:9f:c2:a4:ca:93",6)
0000015411 [ncp.esp32.at] TRACE: < +CWLAP:(3,"",-83,"76:ac:b9:94:77:d5",1)
0000015413 [ncp.esp32.at] TRACE: < +CWLAP:(3,"",-83,"74:ac:b9:53:e1:b2",1)
0000015417 [ncp.esp32.at] TRACE: < +CWLAP:(0,"pkasemir guest 'm'",-83,"f0:9f:c2:a4:ca:93",6)
0000015418 [ncp.esp32.at] TRACE: < +CWLAP:(3,"pkasemir corp1009_IoT",-83,"b6:fb:e4:90:f7:40",11)
0000015419 [ncp.esp32.at] TRACE: < +CWLAP:(3,"",-84,"12:9f:c2:6e:a9:17",1)
0000015421 [ncp.esp32.at] TRACE: < +CWLAP:(3,"FieldFOXm",-84,"f2:9f:c2:6e:a9:17",1)
0000015422 [ncp.esp32.at] TRACE: < +CWLAP:(3,"NETGEAR52",-84,"9c:3d:cf:14:85:5c",3)
0000015423 [ncp.esp32.at] TRACE: < +CWLAP:(3,"CENN. REALTY ADVISORS",-84,"ac:84:c6:a0:87:6b",9)
0000015424 [ncp.esp32.at] TRACE: < +CWLAP:(3,"",-85,"b6:fb:e4:a0:f7:40",11)
0000015426 [ncp.esp32.at] TRACE: < +CWLAP:(3,"FLATIRONS-Guest_EXT",-90,"28:80:88:e2:4f:5d",11)
0000015427 [ncp.esp32.at] TRACE: < +CWLAP:(0,"assetsense-guest",-92,"5a:ef:68:a3:95:63",2)
0000015429 [ncp.esp32.at] TRACE: < +CWLAP:(3,"ARLO_VMB_4731864147",-93,"a4:11:62:6f:a7:00",6)
0000015431 [ncp.esp32.at] TRACE: < OK
0000016435 [app] INFO: {"cmd":"loc","time":1636123847,"loc":{"lck":1,"time":1584835205,"lat":39.92010117,"lon":-105.11060333,"alt":1660.463,"hd":20.35,"spd":0.00,"h_acc":3.000,"hdop~
0000016437 [app] INFO: cloud sent: {"cmd":"loc","time":1636123847,"loc":{"lck":1,"time":1584835205,"lat":39.92010117,"lon":-105.11060333,"alt":1660.463,"hd":20.35,"spd":0.00,"h_acc"~
0000016439 [app] TRACE: published and transitioning to EXECUTE
0000016445 [system.nm] TRACE: Request to power off the interface
0000016446 [ncp.esp32.mux] INFO: Stopping GSM07.10 muxer
0000016446 [ncp.esp32.mux] INFO: Gracefully stopping GSM07.10 muxer
0000016447 [ncp.esp32.mux] INFO: Closing all muxed channels
0000016447 [ncp.esp32.mux] INFO: Closing mux channel 1
0000016447 [ncp.esp32.mux] INFO: Muxed channel 2 already closed
0000016448 [ncp.esp32.mux] INFO: Muxed channel 3 already closed
0000016449 [ncp.esp32.mux] INFO: Muxed channel 4 already closed
0000017068 [comm.protocol] TRACE: Reply recieved: type=2, code=0
0000017069 [comm.protocol] TRACE: message id 2389 complete with code 0.00
0000017070 [comm.protocol] TRACE: rcv'd message type=13
0000017719 [ncp.esp32.mux] INFO: Sending CLD (multiplexer close down)
0000017727 [ncp.esp32.mux] INFO: Received response to CLD or timed out, exiting multiplexed mode
0000017727 [ncp.esp32.mux] INFO: GSM07.10 muxer thread exiting
0000017728 [ncp.esp32.mux] INFO: GSM07.10 muxer stopped
0000017729 [system.nm] TRACE: Interface 5 power state changed: 1
0000017729 [ncp.esp32.client] TRACE: Deinit modem serial.
0000017730 [system.nm] TRACE: Interface 5 power state changed: 3
0000017730 [system.nm] TRACE: Interface 5 power state changed: 1
0000017731 [ncp.esp32.client] TRACE: NCP state changed: 0
0000017731 [ncp.esp32.client] TRACE: Deinit modem serial.
0000028000 [app] INFO: location cb publish 16 timeout
0000120001 [app] INFO: connecting...
0000138251 [app] INFO: connection failed
0000138252 [app] INFO: disconnecting.

Do you have any suggestions for getting the TCP connection to work with the Tracker one? My overall goal is to perform an operation similar to that described by CanCon_ENG as described here: Best way to update local device with Particle - #3 by rickkas7

Thanks so much for your help and support!
trevor1

8.8.8.8 won’t work as it’s the DNS server, not the HTTP server. You could try 142.250.64.78, that’s what I got from dig google.com just now. I’m not positive you’ll get an actual page back, however, because it may require that the connection switch to https (TLS/SSL encrypted) and the TCP client won’t do that. But you should get back something.

This did seem to work. Thanks rickkas7! I would note that the client.available() does not provide consistent data so it would be best to use a timeout or some sort of check to know when the page has been successfully downloaded.

The available() function only returns the amount of data in the buffer. And the buffer is small, so you can almost never download the entire file into it. You definitely should copy the data as it arrives and store it in a different buffer, which will also allow you to know the actual size.