Getting Started with Losant and a NCD Temperature Humidity Sensor

Getting Started with Losant and NCD Temperature Humidity Sensor

Recently I discovered this really cool company called Losant. They build Enterprise level IoT software solutions and their platform is so easy to use, supporting tons of hardware platforms. So I decided to give it a try.  Losant is a great choice for Raspberry pi, ESP8266, Arduino, and Particle. In this article, I will be interfacing a temperature humidity sensor with Losant through an ESP8266. This is a simple application, but it will give us pretty good idea about how to use Losant with ncd.io hardware.  It only took a few minutes to build this temperature/humidity sensing application using our plug-and-play hardware platform.

To build this weather station, I will be using following hardware:

  1. ESP8266
  2. SHT25
  3. I2C Shield

To assemble the hardware plug the ESP8266 into the I2C shield and use the I2C cable to connect SHT25 temperature Humidity Sensor.

ESP8266 IoT temperature Humidity data logger

Losant Setup on ESP8266

You will need to install Losant Libraries in your ESP8266, these libraries and installation info can be found on the Losant Website over here.

All ncd.io hardware works in the same way, so using this setup, you can interface any device or sensor with Losant in just a few minutes.

Step 1: Create an Account on Losant.  To learn more, checkout this blog post.

Step 2: Click “Applications” and create a new application. Here you can fill out the details like your application name and Description.

Losant temp humidity sensor

Step 3: Select Device Attributes

Attributes are the things which ESP8266 will send to Losant. In this case we will report humidity, temperature in °C and temperature in °F.

losant weather station

Selecting Attributes

losant weather kit

 

Step 4: Create a Dashboard

cloud weather application

 

Step 5 : Create Access Key : To create an access key, click on “Applications” and select “Security”.  Here you can write your application name.

losant key

Make sure you save your access key and access secret keys.

Step 6 : Now its time to write the arduino lib for ESP8266. The Arduino lib can be found  on github.

void reconnectMQTT(){
   Serial.println("setting up mqtt");
   while(!mqttCli.connected()){
     if(mqttCli.connect("ESP8266Client123456789")==true){
        Serial.println("connected");
        String subTopic = String("channels/"+ String(channelID) + "/subscribe/json/" + String(readAPIKey));
        int subTopicLength = subTopic.length()+1;
        char subTopicBuffer[subTopicLength];
        subTopic.toCharArray(subTopicBuffer,subTopicLength);
        
        String pubMessage = "status=MQTTPUBLISH";
        String pubTopic =String("channels/"+String(channelID)+"/publish/"+String(writeAPIKey));
        int pubTopicLength = pubTopic.length()+1;
        char pubTopicBuffer[pubTopicLength];
        pubTopic.toCharArray(pubTopicBuffer,pubTopicLength);
        //Publish to MQTT Broker 
        Serial.println(mqttCli.publish(pubTopicBuffer, pubMessage.c_str()) ? "Published" : "NotPublished");
        //Subscribe to MQTT Broker
        Serial.println(mqttCli.subscribe(subTopicBuffer) ? "Subscribed" : "Unsbscribed"); 
       }else{
           Serial.print("failed, rc=");
           Serial.println(mqttCli.state());
           delay(1000);
        }
    }
  }

//----------WiFiCallback-----------//
void taskWiFiCallback(){
  Serial.println("taskWiFiCallbackStarted");
  Serial.print("timeout for this task: t");
  Serial.println(tWiFi.getTimeout());
      if(!mqttCli.connected()){
          Serial.println("Client not connected");
          reconnectMQTT();
        } 
        String topicString ="channels/"+String(channelID)+"/publish/"+String(writeAPIKey);
       int topicLength = topicString.length()+1;
       char topicBuffer[topicLength];
       topicString.toCharArray(topicBuffer,topicLength+1);
       Serial.println(topicBuffer);
       String dataString  = String("field1="+ String(tempC,1) + "&field2=" + String(tempF,1) + "&field3=" + String(humid,1));
       int dataLength = dataString.length()+1;
       byte dataBuffer[dataLength];
       dataString.getBytes(dataBuffer,dataLength);
       
       mqttCli.beginPublish(topicBuffer,dataLength,false);

       Serial.println(mqttCli.write(dataBuffer,dataLength) ? "published" : "published failed");
      
       mqttCli.endPublish();
       
       //mqttCli.loop();
  } 

Arduino Output on the Serial Monitor

Losant ESP8266

 

Weather Station Data On Losant

losant weather app

 

 

Need a Long-Range Wireless temperature humidity sensor?  Here you can learn how to interface Industrial Long range wireless temperature humidity sensors with Arduino.