This is the fourth blog post of the getting started series, please if you didn't read the other blog post I encourage you to do it since covers the platform basics.

Objective:

We are going to divide this blog post in 3 main parts to finally arrive to the 3rd example that shows how to send the values from the sensor to the platform

  • 1 - Get Temperature and Humidity values from a sensor.
  • 2 - Connect Arduino to internet using CC3000.
  • 3 - Send it to ConnectingThings Dashboard using MQTT.

Get Temperature and Humidity values from a sensor

#include "DHT.h"

#define DHTPIN 2      
#define DHTTYPE DHT11   

DHT dht(DHTPIN, DHTTYPE);

char humidityBuffer[50];  
char tempBuffer[50];

void setup() {  
  Serial.begin(9600);
  dht.begin();
}

void loop() {  
  String temp = dtostrf(dht.readTemperature(), 5, 2, tempBuffer);
  String humidity = dtostrf(dht.readHumidity(), 5, 2, humidityBuffer);

  Serial.println("Temperature: "+temp); 
  Serial.println("Humidity: "+humidity); 

  delay(5000);
}

As we saw in this first example we are showing how to interact with the DHT11 getting the values of temperature and humidity and send the to the serial print.

Connect Arduino to internet using CC3000

cc3k connection

#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h> 

#define aref_voltage 3.3

#define ADAFRUIT_CC3000_IRQ   3  
#define ADAFRUIT_CC3000_VBAT  5
#define ADAFRUIT_CC3000_CS    10

Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT, SPI_CLOCK_DIVIDER);

#define WLAN_SSID       "WiFi-SSID"
#define WLAN_PASS       "Password"
#define WLAN_SECURITY   WLAN_SEC_WPA2

Adafruit_CC3000_Client client = Adafruit_CC3000_Client();

void setup(void)  
{
 Serial.begin(115200);

  Serial.println(F("Initialising the CC3000 ..."));
  if (!cc3000.begin()) {  Serial.println(F("Failed!")); while(1); }

  Serial.println(F("Deleting old connection profiles"));
  if (!cc3000.deleteProfiles()) {  Serial.println(F("Failed!"));  while(1);  }

  char *ssid = WLAN_SSID; 
  Serial.println(F("Attempting to connect to ")); Serial.println(ssid);

  if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {   
  Serial.println(F("Failed!")); while(1); }   
  Serial.println(F("Connected!"));

  Serial.println(F("Request DHCP"));
  while (!cc3000.checkDHCP()) { delay(100);}  

}

void loop(void)  
{ 
    displayConnectionDetails();

    while(true){}
}

bool displayConnectionDetails(void)  
{
  uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv;

  if(!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv))
  {
    Serial.println(F("Unable to retrieve the IP Address!\r\n"));
    return false;
  }
  else
  {
    Serial.print(F("\nIP Addr: ")); cc3000.printIPdotsRev(ipAddress);
    Serial.print(F("\nNetmask: ")); cc3000.printIPdotsRev(netmask);
    Serial.print(F("\nGateway: ")); cc3000.printIPdotsRev(gateway);
    Serial.print(F("\nDHCPsrv: ")); cc3000.printIPdotsRev(dhcpserv);
    Serial.print(F("\nDNSserv: ")); cc3000.printIPdotsRev(dnsserv);
    Serial.println();
    return true;
  }
}

Thanks to the adafruit libraries for CC3300 makes it easy to connect too the internet and start communicating with the world.

Send it to ConnectingThings platform using MQTT.

Now let's put all togheter plus MQTT

#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <DHT.h>
#include <PubSubClient.h>

#define aref_voltage 3.3

#define ADAFRUIT_CC3000_IRQ   3  
#define ADAFRUIT_CC3000_VBAT  5
#define ADAFRUIT_CC3000_CS    10

Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT, SPI_CLOCK_DIVIDER);

#define WLAN_SSID       "WiFi-SSID"
#define WLAN_PASS       "Password"
#define WLAN_SECURITY   WLAN_SEC_WPA2

#define DHTPIN 7     // what pin we're connected to
#define DHTTYPE DHT11   

char humidityBuffer[50];  
char tempBuffer[50];

Adafruit_CC3000_Client client = Adafruit_CC3000_Client();  
PubSubClient mqttclient("app.connectingthings.io", 1883, client);

DHT dht(DHTPIN, DHTTYPE);

void setup(void)  
{
 Serial.begin(115200);

  Serial.println(F("Initialising the CC3000 ..."));
  if (!cc3000.begin()) {  Serial.println(F("Failed!")); while(1); }

  Serial.println(F("Deleting old connection profiles"));
  if (!cc3000.deleteProfiles()) {  Serial.println(F("Failed!"));  while(1);  }

  char *ssid = WLAN_SSID; 
  Serial.println(F("Attempting to connect to ")); Serial.println(ssid);

  if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) { Serial.println(F("Failed!")); while(1); }   
  Serial.println(F("Connected!"));

  Serial.println(F("Request DHCP"));
  while (!cc3000.checkDHCP()) { delay(100);}
  displayConnectionDetails();

  dht.begin(); 
}

void loop(void)  
{ 
  String temp = dtostrf(dht.readTemperature(), 5, 2, tempBuffer);
  String humidity = dtostrf(dht.readHumidity(), 5, 2, humidityBuffer);

  String humidityJson = "{\"value\": \""+humidity+"\",\"tag\": \"humidity\"}";
  humidityJson.toCharArray(humidityBuffer, 50);

  String temperatureJson = "{\"value\": \""+temp+"\",\"tag\": \"temperature\"}";
  temperatureJson.toCharArray(tempBuffer, 50);

  // are we still connected?
  if (!mqttclient.connected()) {  reconnect(); }

  Serial.println("Temperature: "+temp); 
  mqttclient.publish("device/arduino_uno/key/xxxxxxx",tempBuffer);
  delay(5000);
  Serial.println("Humidity: "+humidity); 
  mqttclient.publish("device/arduino_uno/key/xxxxxxx",humidityBuffer, true);

  mqttclient.loop();
  delay(5000);
}

/**************************************************************************/
/*!
    @brief  Tries to reconnect the CC3000's to the mqtt broker
*/
/**************************************************************************/
void reconnect() {  
  // Loop until we're reconnected
  while (!mqttclient.connected()) {
    Serial.println("Attempting MQTT connection...");   
    if (mqttclient.connect("arduinoClient")) {
      Serial.print("Connected!");       
    } else {
      Serial.print("Failed!");
      Serial.print(mqttclient.state());
      Serial.println("trying again in 5 seconds"); 
      delay(5000);
    }
  }
}

/**************************************************************************/
/*!
    @brief  Tries to read the IP address and other connection details
*/
/**************************************************************************/
bool displayConnectionDetails(void)  
{
  uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv;

  if(!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv))
  {
    Serial.println(F("Unable to retrieve the IP Address!\r\n"));
    return false;
  }
  else
  {
    Serial.print(F("\nIP Addr: ")); cc3000.printIPdotsRev(ipAddress);
    Serial.print(F("\nNetmask: ")); cc3000.printIPdotsRev(netmask);
    Serial.print(F("\nGateway: ")); cc3000.printIPdotsRev(gateway);
    Serial.print(F("\nDHCPsrv: ")); cc3000.printIPdotsRev(dhcpserv);
    Serial.print(F("\nDNSserv: ")); cc3000.printIPdotsRev(dnsserv);
    Serial.println();
    return true;
  }
}

Finally we just need to add the MQTT library from Nick O'Leary and we can start sending information to the platform.

Here you have the full example with the correct version of the libraries, very well tested.

If you don't have an account already ask yours for free! here

And if you need help or you have ideas check out our community


May the code be with you.
---