Rapid data transfer

I would like to get rapid (at least 60 hz) data from an IMU shield attached to a photon. It seems that the cloud function protocol is not the way to go - I’ve only managed at most 4 hz. I can verify that neither the latency of the sensor or the photon is the issue, since I get good performance over serial. Does anyone have advice about what the right way to do this would be? Thanks.

On what sort of client do you want your data received?
TCPClient or UDP seem the best fit for WiFi transfer and for wired any of the onboard ports.

Yes the cloud limits you to 1 update per second (or 4 per second in short bursts) as you have seen.

One option would be to make your photon be a TCP server (listens for connections, when connected sends the IMU data every 1/60th of a second). The TCPServer documentation will have an example of this.

Another option would be to have your computer act as the server and have the Photon open a connection and send the data. This recent thread Quickest way to stream data from Photon has examples of this.

2 Likes

You could setup a node.js UDP server and have the photon send UDP packages, no connection overhead, but no delivery insurance either.

Nodejs server

const coap        = require('coap')
    , server      = coap.createServer();   

server.on('request', function(req, res) {
    var url = req.url.split('/')[1];

    //  console.log(req.rsinfo.address+":"+req.rsinfo.port+" >> "+req.payload.toString('utf-8'));

      if (url == "incoming")
      {
        var payload = req.payload.toString('utf-8');
        try
        {
            var js = JSON.parse(payload.toString('utf-8'));

         }
         catch (ex) {
              console.log("Json parse failed >> ",payload.toString('utf-8'),ex);
       }
})

// the default CoAP port is 5683
server.listen(function() {
  console.log("Server up ...");
})

Photon code

#include <application.h>
#include <CoapSender.h>

UDP Udp;

void CoapSender::init()
{
    Udp.begin(5683);
}

void CoapSender::send(char* data)
{
    char tempbuf[430];
    
    Udp.beginPacket({192,168,1,15}, 5683);

    uint8_t coapid = 1;
    uint8_t p = 0;
    //          VVTTLLLL
    tempbuf[p++] = 0b01010000; //V=01 T=01 (No confirm) 0 token length
    tempbuf[p++] = 0b00000010; //0.2=POST req
    tempbuf[p++] = (coapid & 0xFF00) >> 8; //Message id
    tempbuf[p++] = (coapid & 0xff); //Message id 1
    //tempbuf[p++] = 0xff; //no options
    
    //coap_server seems to require a hostname, although the contents does not matter
    char host[13] = "abc.dk";
    //Option 3 - delta 3 - URI_Host
    uint8_t hostlen = strlen(host);

    if (hostlen > 12) //Hostname is limited to 99chars, so we dont need to support type 14
    {
        tempbuf[p++] = 0b00110000 | 13; //1101 = 13
        tempbuf[p++] = hostlen - 13;
        } else {
        tempbuf[p++] = 0b00110000 | hostlen;
    }
    
    strncpy(&tempbuf[p], host, hostlen);
    p += hostlen;

    //Option 11 - delta 8 - URI_PATH - /incoming
    //88 69 6e 63 6f 6d 69 6e 67
    tempbuf[p++] = 0x88;
    tempbuf[p++] = 0x69;
    tempbuf[p++] = 0x6e;
    tempbuf[p++] = 0x63;
    tempbuf[p++] = 0x6f;
    tempbuf[p++] = 0x6d;
    tempbuf[p++] = 0x69;
    tempbuf[p++] = 0x6e;
    tempbuf[p++] = 0x67;
    
    //Option 12 - delta 1 - format json
    //11 32
    tempbuf[p++] = 0x11;
    tempbuf[p++] = 0x32;
    
    tempbuf[p++] = 0xff; //end of options    

    strncpy(&tempbuf[p], data, strlen(data));
    Udp.write((uint8_t*)tempbuf, p+strlen(data));
    Udp.endPacket();
}