WEEK 2 - WEEK 3 FIND AND MAKE CODING FOR KOOLBOX

 /*CODING FOR KOOLBOX*/

/* Muhammad shahrizal irwan bin salleh*/

/* 51220220156*/

 


/* OLED */

#include <ACROBOTIC_SSD1306.h> // library for OLED: SCL ==> D1; SDA ==> D2

#include <SPI.h>

#include <Wire.h>


/* DHT22*/

#include "DHT.h"

#define DHTPIN D3  

#define DHTTYPE DHT22 

DHT dht(DHTPIN, DHTTYPE);

float hum = 0;

float temp = 0;


void setup() 

{

  Serial.begin(115200);

  delay(10);

  dht.begin();

  oledStart();

}


void loop() 

{

  getDhtData();

  displayData();

  delay(2000); // delay for getting DHT22 data

}


/***************************************************

 * Start OLED

 **************************************************/

void oledStart(void)

{

  Wire.begin();  

  oled.init();                      // Initialze SSD1306 OLED display

  clearOledDisplay();

  oled.clearDisplay();              // Clear screen

  oled.setTextXY(0,0);              

  oled.putString("     COOLBOX ");

  oled.setTextXY(1,0);              

  oled.putString("       FYP ");

}


/***************************************************

 * Get DHT data

 **************************************************/

void getDhtData(void)

{

  float tempIni = temp;

  float humIni = hum;

  temp = dht.readTemperature();

  hum = dht.readHumidity();

  if (isnan(hum) || isnan(temp))   // Check if any reads failed and exit early (to try again).

  {

    Serial.println("Failed to read from DHT sensor!");

    temp = tempIni;

    hum = humIni;

    return;

  }

}


/***************************************************

 * Display data at Serial Monitora & OLED Display

 **************************************************/

void displayData(void)

{

  Serial.print(" Temperature: ");

  Serial.print(temp);

  Serial.print("oC   Humidity: ");

  Serial.print(hum);

  Serial.println("%");

  

  oled.setTextXY(3,0);              // Set cursor position, start of line 3

  oled.putString("TEMP: " + String(temp) + " oC");

  oled.setTextXY(4,0);              // Set cursor position, start of line 3

  oled.putString("HUM:  " + String(hum) + " %");

}


/***************************************************

 * Clear OLED Display

 **************************************************/

void clearOledDisplay()

{

  oled.setFont(font8x8);

  oled.setTextXY(0,0); oled.putString("                ");

  oled.setTextXY(1,0); oled.putString("                ");

  oled.setTextXY(2,0); oled.putString("                ");

  oled.setTextXY(3,0); oled.putString("                ");

  oled.setTextXY(4,0); oled.putString("                ");

  oled.setTextXY(5,0); oled.putString("                ");

  oled.setTextXY(6,0); oled.putString("                ");

  oled.setTextXY(7,0); oled.putString("                ");

  oled.setTextXY(0,0); oled.putString("                ");              

}

This coding are only to test the Oled display and the dht22 sensor 

Comments

  1. I suggest do not put all coding in your blog, only flowchart is OK

    ReplyDelete

Post a Comment