Pete Hoffswell
Published © CC BY-NC-SA

Multiple Sensors: Particle Photon + ThingSpeak

Read multiple sensors from a Particle Photon and Publish via webhook to Thingspeak.

BeginnerFull instructions provided2 hours6,769
Multiple Sensors: Particle Photon + ThingSpeak

Things used in this project

Hardware components

Photon
Particle Photon
×1
DHT11 Temperature & Humidity Sensor (4 pins)
DHT11 Temperature & Humidity Sensor (4 pins)
×1
Photo resistor
Photo resistor
×1
Resistor 10k ohm
Resistor 10k ohm
×1
Resistor 330 ohm
Resistor 330 ohm
Or 220 ohm. I think this is a pull-down resistor, isn't it?
×1
Breadboard (generic)
Breadboard (generic)
×1

Software apps and online services

Particle Build Web IDE
Particle Build Web IDE
If you don't have an account already, sign up. It's Free!
ThingSpeak Open Data Platform
If you don't have an account already, sign up. It's Free!

Story

Read more

Schematics

Circuit

Here's the whole circuit -

Code

Particle.io Webhook

JSON
This is the code that resides at particle.io. No changes are required.
{
    "event": "thingSpeakWrite_",
    "url": "https://api.thingspeak.com/update",
    "requestType": "POST",
    "form": {
        "api_key": "{{k}}",
        "field1": "{{1}}",
        "field2": "{{2}}",
        "field3": "{{3}}",
        "field4": "{{4}}",
        "field5": "{{5}}",
        "field6": "{{6}}",
        "field7": "{{7}}",
        "field8": "{{8}}",
        "lat": "{{a}}",
        "long": "{{o}}",
        "elevation": "{{e}}",
        "status": "{{s}}"
    },
    "mydevices": true,
    "noDefaults": true
}

solarmonitor.ino

C#
// This #include statement was automatically added by the Particle IDE.
#include "Adafruit_DHT/Adafruit_DHT.h"

// -----------------------------------------
// solarmonitor
// Monitor light, temperature and Humidy at solar panel
// Particle Photon + Particle.io Webhook + ThingSpeak Integration
// April, 2016 - Pete Hoffswell - pete@hoffswell.com
// -----------------------------------------

#define DHTPIN 2
#define DHTTYPE DHT11 // DHT11 Module

#define publish_cycle 60000 // Only publish every 60 seconds
const String key = "8OAB88505K78478J"; // Change this to your Thingspeak api write key

int led = D7; // Sensor Read Light
int photoCell = A0;
int power = A5; // Photocell power.  An analog pin to gives a more steady voltage.

int light; // Light
double tempF; // Temperature F
double tempC; // Temperature C
double humidity; // Humidity
unsigned int lastPublish = 0;

DHT dht(DHTPIN, DHTTYPE);

void setup() {
    // Set Pin Modes
    pinMode(led,OUTPUT);
    pinMode(photoCell,INPUT);
    pinMode(power,OUTPUT);

    digitalWrite(power,HIGH); // Turn on power source for photoCell
    digitalWrite(led,LOW); 

    // Connect variables to particle cloud
    // This allows you to save data to particle.io, and run commands against it such as "particle variable Photon get light"
    Particle.variable("light", &light, INT);
    Particle.variable("tempF", &tempF, DOUBLE);
    Particle.variable("tempC", &tempC, DOUBLE);
    Particle.variable("humidity", &humidity, DOUBLE);

    dht.begin();
    Serial.begin(9600);
    delay(10000);

} //setup


void loop() {
  unsigned long now = millis();
  digitalWrite(led,HIGH); // Signal read sequence led

  // read sensors
  light = analogRead(photoCell);
  delay(100);  // is this needed?
  humidity = dht.getHumidity();
  tempC = dht.getTempCelcius();
  tempF = dht.getTempFarenheit();

  // DHT Read ok?
  if (isnan(humidity) || isnan(tempF) || isnan(tempC)) {
    Serial.println("");
    Serial.println("Failed to read from DHT sensor!");
    Serial.println("humidity=" + String(humidity) + " tempF=" + String(tempF) + " tempC=" + String(tempC));
    Serial.println("");
    return; // exit loop and try again
  }

  // Display to serial
  Serial.println();
  Serial.print("humidity=" + String(humidity) + " tempF=" + String(tempF) + " tempC=" + String(tempC) + " light=" + String(light));
  delay(200);

  // Publish to thinkspeak
  if ((now - lastPublish) > publish_cycle) {
    Particle.publish("thingSpeakWrite_All", "{ \"1\": \"" + String(humidity) + "\"," +
       "\"2\": \"" + String(tempC) + "\"," +
       "\"3\": \"" + String(tempF) + "\"," +
       "\"4\": \"" + String(light) + "\"," +
       "\"k\": \"" + key + "\" }", 60, PRIVATE);
    lastPublish = now;
    Serial.println(" - Published!");
  } else {
      Serial.println();
  }

  digitalWrite(led,LOW);
  delay(2000); // Wait 2 seconds before next loop
  
} // loop

Credits

Pete Hoffswell

Pete Hoffswell

10 projects • 63 followers
Network Engineer for All the People. Internet advocate. Microcontroller Fan. Maker.

Comments