Now that the dashboard is ready to receive & display some data, it's time to write our Arduino sketch, so the board can actually send data to the dashboard.
You will first need to get your AIO key, which is a unique key that identify your account on Adafruit IO. You can for example get it from your dashboard:
Let's now build your Arduino sketch. You will need three libraries for this project:
- CC3000 library
- PubSub library (deprecated)
- Adafruit MQTT library
- DHT library
To install a library, simply place the downloaded library folder inside your Arduino 'libraries' folder.
As the code is quite long, I will only cover the important parts here. Of course, you can find all the code inside the GitHub repository of the project. The sketch starts by including the required libraries:
#include <Adafruit_CC3000.h> #include <ccspi.h> #include <SPI.h> #include <PubSubClient.h> #include "DHT.h"
After that, we define the pin & type of the DHT sensor, and also create an instance of this sensor:
#define DHTPIN 7 // what pin we're connected to #define DHTTYPE DHT11 // DHT 11 DHT dht(DHTPIN, DHTTYPE);
Then, that's where you will need to enter your WiFi network name and password:
#define WLAN_SSID "your_wifi_network" #define WLAN_PASS "your_wifi_password" #define WLAN_SECURITY WLAN_SEC_WPA2
You also need to enter your user name and AIO key:
#define ADAFRUIT_USERNAME "your_adafruit_username" #define AIO_KEY "your_aio_key"
After that, you will need to enter the different paths for your data feeds, that you created while building the dashboard. You might need to modify this part if you are using different names in the dashboard:
#define TEMPERATURE_PUBLISH_PATH "api/feeds/temperature/data/send.json" #define HUMIDITY_PUBLISH_PATH "api/feeds/humidity/data/send.json" #define LIGHT_PUBLISH_PATH "api/feeds/light/data/send.json"
We also need to create instances of the CC3000 client, and also of the PubSubClient that we will use to communicate with Adafruit IO:
Adafruit_CC3000_Client client = Adafruit_CC3000_Client(); PubSubClient mqttclient("io.adafruit.com", 1883, callback, client);
In the loop() function of the sketch, we first start by measuring data from the light level sensor & from the DHT11 sensor:
// Measure ambient light float light_level_reading = analogRead(LIGHT_SENSOR_PIN); int light_level = (int)(light_level_reading/1024.*100.); // Measure temperature & humidity int h = dht.readHumidity(); int t = dht.readTemperature();
Then, we send the data to the respective data feeds, using the mqttclient.publish() function. We also wait a bit during each publish, so the client has the time to receive the data:
mqttclient.publish(TEMPERATURE_PUBLISH_PATH, (char *) String(t).c_str()); delay(2000); mqttclient.publish(HUMIDITY_PUBLISH_PATH, (char *) String(h).c_str()); delay(2000); mqttclient.publish(LIGHT_PUBLISH_PATH, (char *) String(light_level).c_str()); delay(2000);
Finally, we keep the connection with Adafruit IO alive using the mqttclient.loop() function:
mqttclient.loop();
Note that you can find all the code for this project inside the corresponding GitHub repository:
Text editor powered by tinymce.