Project tutorial
MKR1000 to ThingSpeak.com

MKR1000 to ThingSpeak.com © GPL3+

A fairly simple project that attaches everyone's favorite environmental monitor, the DHT, to the Arduino MKR1000 then transmits sensor data.

  • 4,311 views
  • 8 comments
  • 10 respects

Components and supplies

About this project

I had already developed code for the ESP8266 12-E to have environmental sensor data, taken by the DHT11, uploaded wirelessly to the ThingSpeak website and graphed.  I just needed to make minor changes to make it work with the new Arduino MKR1000.

Step 1.

Connect the DHT data line out to the MKR1000 Pin1( labeled 1 on back of board).

Step 2. Download the WiFi101 Library and DHT Library from:

https://github.com/arduino-libraries/WiFi101

https://learn.adafruit.com/dht

Step 3.

Go to the Board Manager in the Arduino IDE

Tools-->Board-->Boards Manager

Search for for Arduino SAMD Boards (32-bits ARM Cortex M0+)

Download version 1.6.3 (I had issues with 1.6.4 as the MKR100 Board didn't get installed)

Step 4.

Create a free ThingSpeak.com account, label the fields to Graph (humidity and temperatures) and paste your ThingSpeak "write" API key into the provided sketch along with your WiFi SSID and WiFI password.

Step 5.

Select the proper Com Port, looking for the MKR1000, and compile and run the code.

You should see the updates being sent to ThingSpeak in your Serial Window.

have fun!

Code

MKR1000 to DHT to ThingSpeak Wireless cloud based Environmental Sensor graphingArduino
A combination of sketches from AdaFruit, WiFi101 WebClient, and ThingSpeak Arduino Templates.
/*
This sketch is a combination of ADAFruits DHT sketch, WiFi101 Webclient
and The arduino example from ThingSpeak
Modified by Stephen Borsay for the MKR1000, feel free to use
 */

#include <SPI.h> //you don't need this as we arn't using the shiled just chip
#include <WiFi101.h>
#include "DHT.h"

#define DHTPIN 1    // what pin we're connected to, pin1 is 5th pin from end

// Uncomment whatever DHT sensor type you're using!
#define DHTTYPE DHT11  // DHT 11
//#define DHTTYPE DHT21  // DHT 21
//#define DHTTYPE DHT22  // DHT 22

DHT dht(DHTPIN,DHTTYPE);

String apiKey ="YourApiKeyHere"; // api from ThingSpeak

char ssid[] = "YourSSIDHERE"; //  your network SSID (name)
char pass[] = "YourPasswordHere";    //your network password
int keyIndex = 0;     // your network key Index number (needed only for WEP)

//#define WEBSITE "api.thingspeak.com"

int status = WL_IDLE_STATUS;
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128);  // numeric IP for Google (no DNS)
char server[] = "api.thingspeak.com";    // name address for Google (using DNS)

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
WiFiClient client;

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) 
  {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue:
    while (true);
  }

  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) 
  {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    //Connect to WPA/WPA2 network.Change this line if using open/WEP network
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }
  
  Serial.println("Connected to wifi");
  printWifiStatus();
  
}

void loop() {

   // Wait a few seconds between measurements.
  delay(2000);

  //prefer to use float, but package size or float conversion isnt working
  //will revise in future with a string fuction or float conversion function

  int h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  int t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  int f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f))
  {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // Compute heat index in Fahrenheit (the default)
  int hif = dht.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  int hic = dht.computeHeatIndex(t, h, false);

  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.print(" *C ");
  Serial.print(f);
  Serial.print(" *F\t");
  Serial.print("Heat index: ");
  Serial.print(hic);
  Serial.print(" *C ");
  Serial.print(hif);
  Serial.println(" *F\n");

    Serial.println("\nStarting connection to server..."); 
  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.println("connected to server");


          client.print(F("POST "));
          client.print("/update?key=apiKey&field1=" 
          +               (String) h
          +  "&field2=" +(String) t
          +  "&field3=" +(String) f
          +  "&field4=" +(String) hic
          +  "&field5=" +(String) hif
                                   );
                                      
          String tsData = "field1="   //need the length to give to ThingSpeak
          +             (String)  h
          +  "&field2=" +(String) t
          +  "&field3=" +(String) f
          +  "&field4=" +(String) hic
          +  "&field5=" +(String) hif
        ; 


          client.print("POST /update HTTP/1.1\n");  
          client.print("Host: api.thingspeak.com\n");
          client.print("Connection: close\n");
          client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
          client.print("Content-Type: application/x-www-form-urlencoded\n");
          client.print("Content-Length: ");
          client.print(tsData.length());  //send out data string legth to ts
          client.print("\n\n");
          client.print(tsData);
          client.stop();
          delay(1000);
    } 

}


void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}
Code in GitHub
Same code but in GitHub for MKR1000

Schematics

Board image
Wire DHT data line to MKR100 pin 1 (5th pin up)
connect 5v and ground to DHT sensor, both of which are provided by the MKR1000
Mrk1000 ts

Comments

Similar projects you might like

Multiple mode Environmental Sensor Deck with MKR1000

Project tutorial by ConsoleTeam

  • 13,417 views
  • 14 comments
  • 50 respects

MKR1000 Temp and Humidity Sensor

Project tutorial by Don Coleman

  • 21,501 views
  • 11 comments
  • 34 respects

MKR1000 to Initial State DHT Visulaizations

Project tutorial by Stephen Borsay

  • 1,066 views
  • 1 comment
  • 3 respects

Send MKR1000 Data to Google Sheets

Project tutorial by Stephen Borsay

  • 14,001 views
  • 29 comments
  • 38 respects

Wifi Enabled 2/4 Wheeled Robot Platform Utilizing MKR1000

Project showcase by James Martel

  • 4,140 views
  • 6 comments
  • 11 respects

MKR1000 Connecting to the WiFi (3 steps)

by Anton Nazarenko

  • 16,042 views
  • 3 comments
  • 19 respects
Add projectSign up / Login