Streaming sampler data from imp to imp

I try to stream one imp ADC sampler data to other imp and output streamed data to fixed-frequency DAC.
But still have no success, so I post code here - may be some have thoughts is it possible at all or what is wrong with code ,posted below.
IMP-1 (streaming out)
Agent code

stream <- null;
local data;

device.on(“bufferReady”, function (s){
local sendByffer;
local url = “”;
local headers = {“Content-Type”:“application/x-www-form-urlencoded”};
local stream = http.poststream(url, headers, function(gogo){ server.log(“Sent”); });
stream.send(http.urlencode({sendByffer = s}));

Device code

function OKsampled(buffer, length) {
if (length > 0) {
agent.send(“bufferReady”, buffer);
server.log(“Buffer " + length + " bytes”);
} else {
buffer1 <- blob(15000); //for 3 pins ->5KHz, 500ms
buffer2 <- blob(15000);
buffer3 <- blob(15000);

hardware.sampler.configure([hardware.pin5, hardware.pin7, hardware.pin9], 5000, [buffer1, buffer2, buffer3], OKsampled, A_LAW_COMPRESS);

IMP-2 (receiving)
Agent code

writeBuffer <- blob(15000);

function requestHandler(request, response) {
device.on(“more_data_please”, function(nbuffers) {
local buffer = [writeBuffer(), writeBuffer(), writeBuffer(), writeBuffer()];
if (buffer != 0) {
device.send(“more_data”, buffer);

Device code
buffer1 <- blob(15000);
buffer2 <- blob(15000);
buffer3 <- blob(15000);
local pin = hardware.pin1;

function bufferEmpty(buffer) {
server.log(“bufferEmpty() called”);

if (!buffer) {
    server.log("Warning: buffer underrun");
agent.send("more_data_please", 1);


hardware.fixedfrequencydac.configure(pin, 5000, [buffer1, buffer2, buffer3], bufferEmpty, A_LAW_DECOMPRESS);

agent.on(“more_data”, function(buffer) {
server.log(“Received more data”);

Ummm, from a practical perspective, I would be impressed if you got this to work. Between IMP-1 and IMP-2 you have the Internet, so there is huge potential for underflow. There’s also potential for overflow in either agent or in IMP-2.

As you are learning squirrel and asynchronous programming, I suggest that you get simple device <-> agent communications working reliably first. After that, try agent <-> agent. You can then use that code as a foundation to attempt streaming.

your code for IMP-2 Agent doesn’t make any sense. “requestHandler” is a handler. Its job is to process each HTTP request as it comes in. It needs to queue each of the incoming buffers. “device.on” also requires a handler. It only needs to be called once, with the string key from the device that it is expecting and the handler that will process any message from the device. Also, “writeBuffer” is a blob. It’s not clear why you are attempting to call it with “writeBuffer()”. Furthermore, if your IMP-2 needs to ask for more data, its already too late.

So - it isn’t possible? I don’t need synchronous data transfer. Currently I cannot find solution how to receive stream on IMP-2, there are some discussions about .wav files playing, probably old Lola can show a way to me but I cannot find any piece of code for Lola.
But I don’t see reason why it cannot work at all. May be I’m wrong.

You can’t do this with poststream, because agent inbound endpoints don’t support streaming.

When initiating http sessions, agents can stream outbound (poststream) or inbound (requeststream). What you really want are websockets which are on our roadmap :slight_smile:

You should be able to get something working with individual requests, but as @coverdriven says, you have not written a valid onrequest handler here.

Some other hints - you don’t need to encode the binary data in the body (your urlencode call is not needed). You can just send binary, with the type set to application/octet-strream

1 Like