Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Hesam Moshiri
Published

Wireless Home Automation (Control and Monitoring)

Wireless Home Automation (Control and Monitoring) Using a Nextion HMI Display

IntermediateFull instructions provided2 hours165
Wireless Home Automation (Control and Monitoring)

Story

Read more

Schematics

Schematic

Mainboard

Code

Code

C/C++
#include "Adafruit_SHTC3.h"
#include <RH_ASK.h>
#include <SPI.h>
#include <TaskScheduler.h>


void t1Callback();
void t2Callback();
void t3Callback();


Scheduler runner;


Task t1(30, TASK_FOREVER, &t1Callback);
Task t2(500, TASK_FOREVER, &t2Callback);
Task t3(10, TASK_FOREVER, &t3Callback);


Adafruit_SHTC3 shtc3 = Adafruit_SHTC3();


#define heaterSW 12
#define coolerSW 8
#define humidSW 7
#define lightSW 5
#define lightSensor A7


int tempValue = 0, lightValue = 50, counter = 0;


sensors_event_t humidity, temp;


RH_ASK rf_driver(2000, 2, 15);


void setup() {
  shtc3.begin();
  rf_driver.init();
  runner.init();
  runner.addTask(t1);
  runner.addTask(t2);
  runner.addTask(t3);
  t1.enable();
  t2.enable();
  t3.enable();
  pinMode(heaterSW, OUTPUT);
  pinMode(coolerSW, OUTPUT);
  pinMode(humidSW, OUTPUT);
  pinMode(lightSW, OUTPUT);
  pinMode(lightSensor, INPUT);
  digitalWrite(heaterSW, LOW);
  digitalWrite(coolerSW, LOW);
  digitalWrite(humidSW, LOW);
  digitalWrite(lightSW, LOW);
}


void loop() {
  runner.execute();
}


void t1Callback() {
  //--------------------------Data Collection---------------------
  tempValue = map(analogRead(lightSensor), 0, 1023, 0, 99) + tempValue;
  counter ++;
  if (counter == 5)
  {
    lightValue = tempValue / 5;
    counter = 0;
    tempValue = 0;
    shtc3.getEvent(&humidity, &temp);
  }
}


void t2Callback() {
  //-------------------Transmitter--------------------------------
  static char trBuffer[20];
  sprintf(trBuffer, "P:%d:%d:%d#", int(round(temp.temperature)), int(round(humidity.relative_humidity)), lightValue);
  rf_driver.send((uint8_t *)trBuffer, strlen(trBuffer));
  rf_driver.waitPacketSent();
}
void t3Callback() {
  //--------------------Receiver----------------------------------
  char buf[12];
  uint8_t buflen = sizeof(buf);
  if (rf_driver.recv(buf, &buflen))
  {
    buf[buflen - 1] = 0;
    if (buf[0] == 'M')
    {
      if (buf[2] == 'W' && buf[3] == '1') {
        digitalWrite(coolerSW, LOW);
        digitalWrite(heaterSW, HIGH);
      }
      if (buf[2] == 'W' && buf[3] == '0') digitalWrite(heaterSW, LOW);
      if (buf[2] == 'C' && buf[3] == '1') {
        digitalWrite(heaterSW, LOW);
        digitalWrite(coolerSW, HIGH);
      }
      if (buf[2] == 'C' && buf[3] == '0') digitalWrite(coolerSW, LOW);


      if (buf[6] == '1') digitalWrite(humidSW, HIGH);
      if (buf[6] == '0') digitalWrite(humidSW, LOW);


      if (buf[9] == '1') digitalWrite(lightSW, HIGH);
      if (buf[9] == '0') digitalWrite(lightSW, LOW);
    }
  }
}

Credits

Hesam Moshiri

Hesam Moshiri

51 projects • 32 followers
https://www.youtube.com/c/MyVanitar/videos

Comments