[SOLVED] Two sensors data in the Electric Imp

A user just left this comment in our support box:

I am following this tutorial:


but just got temperature from electric imp. code is exactly as posted but not sure how i can get two sensors data if i only using one ID into electric imp.
please advice
thanks
Alex

Hi @Alex we suggest these steps:

  1. Define another SPI pin, so that you have two pins for two DHT sensors:

    spi <- hardware.spi257;
    spi_2 <- hardware.spi189;

  2. Call the DHT class for every sensor:

    dht11 <- DHT11(spi, clkspeed);
    dht11_2 <- DHT11(spi_2, clkspeed);

  3. Save both data and send them to the agent:

    data <- dht11.read();
    data_2 <- dht11_2.read();

    server.log(format(“Relative Humidity: %0.1f”,data.rh)+" %");// just to check in the log
    server.log(format(“Temperature: %0.1f C”,data.temp));

    server.log(format(“Relative Humidity 2: %0.1f”,data_2.rh)+" %");
    server.log(format(“Temperature 2: %0.1f C”,data_2.temp));

    agent.send(“temp”,data.temp);
    agent.send(“temp_2”,data_2.temp);

    //agent.send(“rh%”,data.rh); here you would send the relative humidity as well

    imp.sleep(1);

  4. All of this is made in the device code. In the Agent code it’s necessary to add a few lines as well:

    device.on(“temp”, function(value) {
    server.log(“Trying to post to Ubidots the value:”);
    server.log(value);
    local headers = { “Content-Type”: “application/json”, “X-Auth-Token”: “NBbF3PWPxWc2IaO40aXOKnhIu8tOv92rYN3ibiEc7Jh6GV3KZUUCHtXuNz7Y” }; // Replace with your own token
    local url = “http://things.ubidots.com/api/v1.6/variables/53d2beb37625424630223dac/values”; // Replace with the ubidots variable
    local string = {“value”: value};
    local request = http.post(url, headers, http.jsonencode(string));
    local response = request.sendsync();
    });

    device.on(“temp_2”, function(value) {
    server.log(“Trying to post to Ubi the value:”);
    server.log(value);
    local headers = { “Content-Type”: “application/json”, “X-Auth-Token”: “NBbF3PWPxWc2IaO40aXOKnhIu8tOv92rYN3ibiEc7Jh6GV3KZUUCHtXuNz7Y” }; // Replace with your own token
    local url = “http://things.ubidots.com/api/v1.6/variables/53d2beb37625424630223dac/values”; // Replace with the ubidots variable 2
    local string = {“value”: value};
    local string = {“value”: value};
    local request = http.post(url, headers, http.jsonencode(string));
    local response = request.sendsync();
    });

Hope this helps!