[SOLVED] Photon wifi speed test with short packages

Hi,

I need to send a sensor value (so not big amount of data but more frequently) to my iPhone (SE) every 100 ms… I was not sure with how much delay it is possible, and I did not find any test about it. So I did myself, and share it with you guys, maybe somebody needs it also.
Test method:
I send the current millis of the photon to iphone, and the iphone sends back the value it has received.
If the photon got back the message, it prints out the current millis (received time), and the received msg (which is the current millis at the time when the photon has sent). So the differences shows, that how much time to send the sensor value to iphone, and receive it back.
I have tested with 20 ms sending interval, and my findings show that:
it takes around 3-5 ms to send and receive back the short msg between photon and iPhone SE

My code was:

TCPClient client;
byte server[] = { 192, 168, 1, 102 };


bool client_connected = false;
unsigned long last_send = 0;
unsigned long last_connection_try = 0;
unsigned long last_blink = 0;
int blink_interval = 100;
int led = D7;
bool blink_state = LOW;

void setup() {
    pinMode(led, OUTPUT);
    Serial.begin(115200);
    // Wait for a USB serial connection for up to 30 seconds
    //waitFor(Serial.isConnected, 30000);
    connectToServer();
    Particle.publish("device starting");
    client.setTimeout(1);
}

void connectToServer() {
    if (millis()-last_connection_try > 6000) {
        
        Serial.println("connecting...");
        last_connection_try = millis();

        if (client.connect(server, 3456)) {
            Serial.println("Connected");
            client_connected = true;
        } else{
            Serial.println("connection failed");
        }
        Serial.println(millis() - last_connection_try);
    }
    
}




void loop() {

        if (!client.connected()) {
            
            if (client_connected == true) {
                Serial.println("disconnecting.");
                client.stop();
                client_connected = false;
            }
            
            connectToServer();
        }
        
        if (client.available()) {
            Serial.printf("client - %i: ", millis());
            delay(10);
            while (client.available()) {
                Serial.write(client.read());
            }
            Serial.println();
        }
        
        if (millis() - last_send >= 20) {
            last_send = millis();
            if (client.connected()) client.print(millis());
        }
        
        if (millis() - last_blink > blink_interval) {
            last_blink = millis();
            blink_state = !blink_state;
            digitalWrite(led, blink_state);
        }
}

Attached the logged files.

What app/code were you running on your iphone?

import UIKit
import SwiftSocket

class ViewController: UIViewController {
 
 var client: TCPClient?
 let formatter = DateFormatter()
 
 override func viewDidLoad() {
 super.viewDidLoad()
 formatter.dateFormat = "HH:mm:ss.SSS"
 testServer()
 }
 func echoService(client: TCPClient) {
 print("Newclient from:\(client.address)[\(client.port)]")
 while true {
 // print()
 
 let number1: Int32 = client.bytesAvailable()!
 let number2 = Int(number1)
 var d = client.read(number2)
 
 if d != nil {
 print(self.formatter.string(from: Date()), " ", terminator:"")
 print(String(data: Data(d!), encoding: .utf8))
 client.send(data: d!)

 }
 
 }
 }
 
 func testServer() {
 let server = TCPServer(address: "192.168.1.102", port: 3456)
 switch server.listen() {
 case .success:
 print("success")
 while true {
 print("loop")
 if var client = server.accept() {
 echoService(client: client)
 } else {
 print("accept error")
 }
 
 
 }
 case .failure(let error):
 print(error)
 }
 }

 
}

https://github.com/swiftsocket/SwiftSocket

My code must be reviewed, because uses 100% CPU!!
Also after disconnection, the app must be started again!!