Mason SigmonMichael RayTapan Subba
Published

IOT Project Group 55: Temperature Controlled Fan

This system created using three Particle Photon 2, will add intelligence to your everyday fan and make sure your room is never hot again.

BeginnerFull instructions provided193
IOT Project Group 55: Temperature Controlled Fan

Things used in this project

Hardware components

Photon 2
Particle Photon 2
×3
ELEGOO Upgraded 37 in 1 Sensor Modules Kit V2.0
ELEGOO Upgraded 37 in 1 Sensor Modules Kit V2.0
The three sensors we used came from this kit, in our project we have a digital temperature sensor, a button, and an RGB LED.
×1
Jumper wires (generic)
Jumper wires (generic)
×7
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
×3
Breadboard (generic)
Breadboard (generic)
×3

Software apps and online services

Particle Build Web IDE
Particle Build Web IDE

Story

Read more

Schematics

RGB LED Circuit Diagram (Represents Fan)

RGB LED Physical Circuit (Represents Fan)

Button Circuit Diagram

Button Physical Circuit

Temperature Sensor Circuit Diagram

Temperature Sensor Physical Circuit

Code

LED (Represents Fan)

C/C++
This code subscribes to data from the button and temperature sensor circuits. It turns on the LED if the button is pressed or if the temperature sensor is reading a temperature over its set point. It also publishes whether or not the LED is on or off.
int receivedInteger = 0;  // Global int variable
int receivedInteger2 = 0; // Global int variable

void myHandler(const char *event, const char *data) {
  receivedInteger = String(data).toInt();  // Convert char array to string and then to an int
}

void myHandler2(const char *event, const char *data) {
  receivedInteger2 = String(data).toInt();  // Convert char array to string and then to an int
}

void setup() {
  Serial.begin(9600);
  pinMode(D2, OUTPUT);
  Particle.subscribe("temperature", myHandler);
  Particle.subscribe("pressed", myHandler2);
}

void loop() {
  Serial.println(receivedInteger);   // Using a global variable, you can see it anywhere in your code
  Serial.println(receivedInteger2);  // Using a global variable, you can see it anywhere in your code

  if (receivedInteger == 1) {  // Corrected the condition
    digitalWrite(D2, HIGH);
    Particle.publish("D2_State", "HIGH", PRIVATE);
  } else if (receivedInteger2 == 1) {  // Corrected the condition
    digitalWrite(D2, HIGH);
    Particle.publish("D2_State", "HIGH", PRIVATE);
  } else {
    digitalWrite(D2, LOW);
    Particle.publish("D2_State", "LOW", PRIVATE);
  }

  delay(5000);
}

Temperature

C/C++
This code subscribes to the LED to see whether it is on or off. If the LED is on then it uses a 1 second delay between loops, if the LED is off it uses a 10 second delay between loops. The code also publishes whether the temperature sensor is at a temperature higher or lower than the set temperature threshold.
#include <Particle.h>

const int sensorPin = D2;
int receivedD2State = 0;
bool d2StateUpdated = false;

void d2StateHandler(const char *event, const char *data) {
  if (strcmp(data, "HIGH") == 0) {
    receivedD2State = HIGH;
  } else {
    receivedD2State = LOW;
  }
  d2StateUpdated = true;
}

void setup() {
  pinMode(sensorPin, INPUT);
  Serial.begin(9600);
  
  Particle.subscribe("D2_State", d2StateHandler);
}

void loop() {
  // Read the digital value from D2 on the current device
  int localD2State = digitalRead(D2);

  // Read the analog value from the temperature sensor
  int digitalValue = digitalRead(sensorPin);

  Particle.publish("temperature", String(digitalValue), PRIVATE);

  // Check if the D2 state has been updated before publishing
  if (d2StateUpdated) {
    Serial.println("Received D2 State: " + String(receivedD2State == HIGH ? "HIGH" : "LOW"));
    Particle.publish("OtherDeviceD2_State", String(receivedD2State), PRIVATE);
    d2StateUpdated = false;  // Reset the flag after publishing
  } else {
    Serial.println("Waiting for D2 State update...");
  }

  // Adjust delay based on the state of D2 from another device
  if (receivedD2State == HIGH) {
    delay(1000);
  } else {
    delay(10000);
  }
}

Button

C/C++
This code is used to read whether the button is pressed or not and publish that data to the cloud.
int buttonPin = D0;

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {
  delay(10000);
  int buttonState = digitalRead(buttonPin);
  if (buttonState == LOW) {  // Check for HIGH instead of LOW
    Particle.publish("pressed", "1", PRIVATE);
  }
    else {
    Particle.publish("pressed", "0", PRIVATE);
  }
}

Credits

Mason Sigmon
1 project • 1 follower
Michael Ray
1 project • 1 follower
Tapan Subba
1 project • 0 followers

Comments