edited October 2016 in Device Code
Hello all,

I am newbie on imp. I have 5 sensor [ex: A,B,C,D,E] which connected on arduino. Now, the data success read on imp as string. My problem is how to send individual sensor data to agent? So, I can send data sensor A,B,C,D,E individual to agent and pass it being independent tagname to ubidot.
Already try to solve myself but always fail.
Here is my device code program:
DataArduino <- "";

function readSerial() {
while(d >= 0) {
if (d=='\n') {
} else {
if (d>=32) DataArduino+=d.tochar();

d =;

hardware.uart57.configure(19200, 8, PARITY_NONE, 1, NO_CTSRTS, readSerial);

Thanks for help


  • Setting up readSerial() as a callback is good, but it'll get called every time there's a byte in the buffer, so you don't need to trigger readSerial() manually (as you do every second via imp.wakeup() and you don't need to loop with while.

    I would simplify your code this way:
    dataArduino <- "";

    function readSerial() {
    local d =;
    if (d == -1) return;
    if (d == '\n') {
    agent.send("DataFromArduino", dataArduino);
    dataArduino = "";
    } else {
    if (d >= 32) dataArduino += d.tochar();

    hardware.uart57.configure(19200, 8, PARITY_NONE, 1, NO_CTSRTS, readSerial);
    ie. byte comes in. If it's useful (!= -1) check if it's a carriage return or add it to the data string. If it is a carriage return, send the string to the agent and clear the data string for the next lot of data.
  • Thanks smittytone for your correction.

    And how to separate string at "dataArduino" be individual data sensor? So I can send it to ubidot individually. Ubidot cand read data Sensor A,B,C,D,E being tagname A,B,C,D & E
  • It's not clear from your description what "dataArduino" string contains. Assuming this contains all five sensor readings and these readings are separated out by some character (e.g. a comma) then use:

    Then you can create code logic to review each item etc.
  • Assuming you have the data from Arduino to imp figured out, you can pass it between device and agent something like this:

    on the device:

    data_to_send <- {"A": 0, "B": 3.4, "C": 0, "D": "hi there", "E" : 56};

    data_to_send.B = 55; //illustrating a way to change the value
    data_to_send["A"] = -22; //illustrating another way to change the value


    on the agent



    doing things with a Table makes it pretty easy to understand. Another thing that can be done is to package the data into a blob using your own scheme for the bytes and then unpack the blob in the Agent. It is harder to code but *might* be more efficient on data use if you think that matters.
Sign In or Register to comment.