Http request

Hello

I use a code to retrieve the value of a tempertaure a probe OneWire
I would like to recover this value with an http request

device code :

`function one_wire_reset()
{
// Configure UART for 1-Wire RESET timing
ow.configure(9600, 8, PARITY_NONE, 1, NO_CTSRTS)
ow.write(0xF0)
ow.flush()
if (ow.read() == 0xF0)
{
// UART RX will read TX if there's no device connected
server.log("No 1-Wire devices are present.")
return false
}
else
{
// Switch UART to 1-Wire data speed timing
ow.configure(115200, 8, PARITY_NONE, 1, NO_CTSRTS)
return true
}
}
 function one_wire_write_byte(byte)
{
for (local i = 0; i < 8; i++, byte = byte >> 1)
{
// Run through the bits in the byte, extracting the
// LSB (bit 0) and sending it to the bus
one_wire_bit(byte & 0x01)
}
}
 
function one_wire_read_byte()
{
local byte = 0
for (local i = 0; i < 8; i++)
{
// Build up byte bit by bit, LSB first
byte = (byte >> 1) + 0x80 * one_wire_bit(1)
}
return byte;
}
 
function one_wire_bit(bit)
{
bit = bit ? 0xFF : 0x00
ow.write(bit)
ow.flush()
local return_value = ow.read() == 0xFF ? 1 : 0
return return_value
}
 
function one_wire_search(next_node)
{
local last_fork_point = 0
 
// Reset the bus and exit if no device found
 
if (one_wire_reset())
{
// There are 1-Wire device(s) on the bus, so issue the 1-Wire SEARCH command (0xF0)
one_wire_write_byte(0xF0)
// Work along the 64-bit ROM code, bit by bit, from LSB to MSB
for (local i = 64 ; i > 0 ; i--)
{
local byte = (i - 1) / 8
// Read bit from bus
local bit = one_wire_bit(1)
// Read the next bit
if (one_wire_bit(1))
{
if (bit)
{
// Both bits are 1 which indicates that there are no further devices
// on the bus, so put pointer back to the start and break out of the loop
last_fork_point = 0
break
}
}
else if (!bit)
{
// First and second bits are both 0: we're at a node
if (next_node > i || (next_node != i && (id[byte] & 1)))
{
// Take the '1' direction on this point
bit = 1
last_fork_point = i
}
}
 
// Write the 'direction' bit. For example, if it's 1 then all further
// devices with a 0 at the current ID bit location will go offline
one_wire_bit(bit)
// Write the bit to the current ID record
 
id[byte] = (id[byte] >> 1) + 0x80 * bit
}
}
 
// Return the last fork point so it can form the start of the next search
return last_fork_point
}
 
function one_wire_slaves()
{
id <- [0,0,0,0,0,0,0,0]
next_device <- 65
 
while(next_device)
{
next_device = one_wire_search(next_device)
// Store the device ID discovered by one_wire_search() in an array
// Nb. We need to clone the array, id, so that we correctly save
// each one rather than the address of a single array
slaves.push(clone(id))
}
}
 
function get_temp()
  {
    local temp_LSB = 0
    local temp_MSB = 0
    local temp_celsius = 0
 
    imp.wakeup(200.0, get_temp)
    // Reset the 1-Wire bus
    one_wire_reset()
    // Issue 1-Wire Skip ROM command (0xCC) to select all devices on the bus
    one_wire_write_byte(0xCC)
    // Issue DS18B20 Convert command (0x44) to tell all DS18B20s to get the temperature
    // Even if other devices don't ignore this, we will not read them
    one_wire_write_byte(0x44)
    // Wait 750ms for the temperature conversion to finish
    imp.sleep(0.75)
 
    foreach (device, slave_id in slaves)
      {
        // Run through the list of discovered slave devices, getting the temperature
        // if a given device is of the correct family number: 0x28 for BS18B20
        if (slave_id[7] == 0x28)
          {
            one_wire_reset()
            // Issue 1-Wire MATCH ROM command (0x55) to select device by ID
             one_wire_write_byte(0x55)
            // Write out the 64-bit ID from the array's eight bytes
            for (local i = 7 ; i >= 0; i--)
              {
                one_wire_write_byte(slave_id[i])
              }
            // Issue the DS18B20's READ SCRATCHPAD command (0xBE) to get temperature
            one_wire_write_byte(0xBE)
            // Read the temperature value from the sensor's RAM
            temp_LSB= one_wire_read_byte()
            temp_MSB = one_wire_read_byte()

            // Signal that we don't need any more data by resetting the bus
            one_wire_reset()
             
            // Calculate the temperature from LSB and MSB


            //temp_celsius = ((temp_MSB * 256) + temp_LSB) / 16.0


          //  server.log(temp_MSB);
          //  server.log(temp_LSB);


            if ((temp_MSB & 128) == 0)
              {
               // server.log("temp positive")
                temp_celsius = ((temp_MSB * 256) + temp_LSB) * 0.0625
                server.log(format("Device: %02d Family: %02x Serial: %02x%02x%02x%02x%02x%02x Temp: %3.2f", (device + 1), slave_id[7], slave_id[1], slave_id[2], slave_id[3], slave_id[4], slave_id[5], slave_id[6], temp_celsius))
                agent.send("updateTemp",  temp_celsius);
              }else
              {
              //  server.log("temp negative")
                temp_celsius = (((temp_MSB * 256) + temp_LSB) ^ 65535 )* 0.0625
                server.log(format("Device: %02d Family: %02x Serial: %02x%02x%02x%02x%02x%02x Temp: %3.2f", (device + 1), slave_id[7], slave_id[1], slave_id[2], slave_id[3], slave_id[4], slave_id[5], slave_id[6], temp_celsius))
                agent.send("updateTemp",- temp_celsius);
            
              }
             // imp.onidle(function(){ imp.deepsleepfor(300); });
         

            }
          }
  }
 
 
 
 
 
// PROGRAM STARTS HERE
 
ow <- hardware.uart12
slaves <- []



// Enumerate the slaves on the bus
 
one_wire_slaves()
 
// Start sampling temperature data
get_temp() `

agent code :

`// HTTP Request handlers expect two parameters:
// request: the incoming request
// response: the response we send back to whoever made the request
function requestHandler(request, response) {
  // Check if the variable led was passed into the query
  if ("temp" in request.query) {
    // if it was, send the value of it to the device

    device.send(???????);
  }
  // send a response back to whoever made the request
  ??? response.send( ????)
}

// your agent code should only ever have ONE http.onrequest call.
http.onrequest(requestHandler);`

Your agent wants to look something like this:

`local saveResp = null;

function requestHandler(request, response) {
  // Check if the variable led was passed into the query
  if ("temp" in request.query) {
    saveResp = response;
    device.send("get.temp", true);
  } else {
  // send a response back to whoever made the request
 response.send(200, "Command not recognized");
}

function sendTemp(data) {
     if (saveResp == null) return;
     saveResp.send(200, data);
}

http.onrequest(requestHandler);
device.on("send.temp", sendTemp);
`

This assumes device has an agent.on("get.temp", ...) structure that calls a function which converts the current temperature value to a string then calls agent.send("send.temp", tempAsString).

Basically, you hold the requestHandler’s response object (in saveResp in the code abobve) until you need it, ie. when you have a temp value sent by the device.

I do not understand at all

agent.on(“get.temp”, …)

Using smittytone’s suggestion,
In your device code, replace
get_temp()
with
agent.on("get.temp",get_temp)
and replace
agent.send("updateTemp",- temp_celsius);
with
agent.send("send.temp", temp_celsius.tostring());

@baudetd, you should read the Developer Guides ‘Event-driven Programming’ and ‘Effective Internet-agent-device Communication’, and look at the example code for device-agent communications.

thanks you but …

i have an error with “agent.on(“get.temp”,get_temp)”

2015-12-30 10:47:00 UTC+1 [Device] ERROR: callback must take 1 parameter in agent.on(msg, callback) 2015-12-30 10:47:00 UTC+1 [Device] ERROR: at main:223

and an error “red cross” with device.on(“send.temp”, sendTemp);

Just change your get_temp() declaration to:

function get_temp(data) {

This is because the callback function needs a parameter for data sent by the agent (even if you don’t ever need that data).

And there’s a missing } in the agent code requestHandler() - hence the red cross error. It should read:

`function requestHandler(request, response) {
    // Check if the variable led was passed into the query
    if ("temp" in request.query) {
        saveResp = response;
        device.send("get.temp", true);
    } else {
        // send a response back to whoever made the request
        response.send(200, "Command not recognized");
    }
}`

My bad.

@smittytone , thanks for the developper guide

well , it’s good but …

i have a error with request

https://agent.electricimp.com/?temp

2015-12-30 14:24:34 UTC+1 [Device] ERROR: callback must take 0 parameters in imp.wakeup(time, callback) 2015-12-30 14:24:34 UTC+1 [Device] ERROR: at get_temp:126

Remove the line

imp.wakeup(200.0, get_temp)

This cause temperature readings to be taken regularly (every 200 seconds), but of course you don’t need that - you only want readings taken in response to the agent, which is what agent.on() does.

You can remove (or comment out) the last line of the device code too.

ok it’s good !! thanks yiu very very much
I will try to program a little bit to understand…