This is a continuation of the Microcontrollers Intro exercise and assumes you have the light detection circuit already in place.
The last exercise finished with exposing variables to the cloud via, e.g. Particle.variable("cds", cds_reading)
. The value of these variables is only actually sent to the cloud if someone requests (pulls) the information. Data logging is usually accomplished more easily by having the microcontroller publish (push) data to the cloud on a regular basis.
char data[255]; // maximum length of single publish
snprintf(data, sizeof(data),
"\"sheet\":\"light\",\"location\":\"my desk\",\"cds\":%d",
cds_reading);
Serial.printf("INFO: sending log (%s)... ", data);
bool success = Particle.publish("gs_logger", data);
(success) ? Serial.println("successful.") : Serial.println("failed.");
Now that your microcontroller publishes to the cloud, you can set up a webhook that automatically forwards the information to various destinations (e.g. a database, text messaging service, email, etc.). Here we will set up a generic webhook that can forward information from any of your devices that publishes with this webhook to a google spreadsheet.
Tools
-> Script Editor
scripts/google_spreadsheet_script.js
in this repo and copy its entire content into your new google scriptcurl
(short for copy URL) is a good check if you have curl
already installed, if not it may not be worth the troublegs_logger
(or whatever you want to use in your Particle.publish
call)POST
JSON
any
Advanced Settings -> JSON DATA: select Custom
and paste the following code:
{
"published_at":"{{PARTICLE_PUBLISHED_AT}}",
"payload":{
{{{PARTICLE_EVENT_VALUE}}}
}
}
yes
Test
button at the top to check communication with the google spreadsheetPull the data directly from the google spreadsheet using googlesheets in R or gspread in Python
For this exercise we will use a simple temperature & humidity sensor, the DHT11 (Technical Specs). It is not particularly accurate (5% Humidity, 2C) but easy to use and has a well-developed library provided by the awesome open-hardware company Adafruit.
Start by creating a new project (e.g. clone this repository, open it in the Particle ID, and use the Start new project button to turn it into a project).
Adafruit_DHT
libraryUse
-> Add to current project
button)particle library search Adafruit_DHT
particle library add Adafruit_DHT
project.properties
file to see all dependenciesUse
- > Copy to current project
in the library browser instead or use the following command: particle library copy Adafruit_DHT
which will add it to the lib
folder. This can be useful even just to look at the code or the examples that are usually provided with any library and you can remove the library simply by deleting the entire library folder (or the line in project.properties
if using a remote copy).View source
to pull up the source code of the Adafruit_DHT
libraryexample/dht-test
folder and open the dht-test.ino
file, copy its contents into your own new project (src/???.ino
file)#define DHTTYPE DHT11
and comment out the other #define DHTTYPE ...
linesSerial.print
statements at the end of the loop
with the following line:Serial.printf("%s: Humidity = %.2f%%, Temp = %.2f C\n", Time.timeStr().c_str(), h, t);
MPU6050
example-mpu-dump-to-serial
from the MPU6050
library to your project src/???.info
file.while(!Serial.available()) SPARK_WLAN_Loop();
with delay(5000)
Serial.print
statements again with the following line:Serial.printf("ax = %5d, ay = %6d, az = %5d | gx = %5d, gy = %5d, gz = %5d\n", ax, ay, az, gx, gy, gz);
MPU6050
library how to get the temperature reading? Hint: you want to look in the library’s src/MPU6050.h
file. Make a variable that stores the temperature reading, read it in every loop step and include it in the serial output.Note: additional information on this sophisticated motion sensor module is available here.