|
|
#include <Arduino.h>
#include <analogWrite.h>
#include <ESP32Servo.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include "fontdef.h"
//#include "SSD1306.h" // alias for `#include "SSD1306Wire.h"`
#include "SSD1306Wire.h"
#include "OLEDDisplayUi.h"
#include <driver/adc.h>
Servo myservo; // create servo object to control a servo
int servoPin = 23;
// Initialize the OLED display using Wire library
SSD1306Wire display(0x3c, 21, 22);
#define DHTPIN 25 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
DHT_Unified dht(DHTPIN, DHTTYPE);
uint32_t delayMS;
int count1 = 0; long int deltask1 = 30 * 60 * 1000 / portTICK_PERIOD_MS; // 30 min
//long int deltask1 = 1 * 60 * 1000 / portTICK_PERIOD_MS; // 30 min
int transPin = 14; int ledPin = 22;
////////////////////////
void task1(void * parameters) { for(;;){ servoDance(); vTaskDelay(deltask1); } }
/////////////////////////
void servoDance(){
for(int n=1; n<=10; n++){ myservo.write(90); delay(500); myservo.write(180); delay(500); }
myservo.write(90); }
////////////////////////
void setup() {
Serial.begin(115200);
myservo.setPeriodHertz(10); myservo.attach(servoPin);
// pinMode(transPin, OUTPUT);
// pinMode(ledPin, OUTPUT);
//
// digitalWrite(ledPin,HIGH);
helloHuman(); // init OLED display
initTempSensor(); // init temperature sensor
xTaskCreate( task1, // function name
"Task1", // task name
1000, // stack size
NULL, // task parameters
1, // priority (low = more priority)
NULL // task handle
);
}
void ShowTemperature(){ // SENSOR AND DISPLAY
display.clear(); // Delay between measurements.
delay(delayMS); // Get temperature event and print its value.
sensors_event_t event; dht.temperature().getEvent(&event); if (isnan(event.temperature)) { Serial.println(F("Error reading temperature!")); } else { display.setFont(Monospaced_bold_16); display.drawString(0, 10, "Temp:"); display.drawString(50, 10, String(int(event.temperature)) + " c" );
Serial.print(F("Temperature: ")); Serial.print(event.temperature); Serial.println(F("°C")); } // Get humidity event and print its value.
dht.humidity().getEvent(&event); if (isnan(event.relative_humidity)) { Serial.println(F("Error reading humidity!")); } else { display.setFont(Monospaced_bold_16); display.drawString(0, 50, "Hum:"); display.drawString(50, 50, String(int(event.relative_humidity)) + " %"); Serial.print(F("Humidity: ")); Serial.print(event.relative_humidity); Serial.println(F("%")); }
display.display(); }
////////////////////////////
void initTempSensor(){ // Initialize DHT device.
dht.begin(); Serial.println(F("DHTxx Unified Sensor Example")); // Print temperature sensor details.
sensor_t sensor; dht.temperature().getSensor(&sensor); Serial.println(F("------------------------------------")); Serial.println(F("Temperature Sensor")); Serial.print (F("Sensor Type: ")); Serial.println(sensor.name); Serial.print (F("Driver Ver: ")); Serial.println(sensor.version); Serial.print (F("Unique ID: ")); Serial.println(sensor.sensor_id); Serial.print (F("Max Value: ")); Serial.print(sensor.max_value); Serial.println(F("°C")); Serial.print (F("Min Value: ")); Serial.print(sensor.min_value); Serial.println(F("°C")); Serial.print (F("Resolution: ")); Serial.print(sensor.resolution); Serial.println(F("°C")); Serial.println(F("------------------------------------")); // Print humidity sensor details.
dht.humidity().getSensor(&sensor); Serial.println(F("Humidity Sensor")); Serial.print (F("Sensor Type: ")); Serial.println(sensor.name); Serial.print (F("Driver Ver: ")); Serial.println(sensor.version); Serial.print (F("Unique ID: ")); Serial.println(sensor.sensor_id); Serial.print (F("Max Value: ")); Serial.print(sensor.max_value); Serial.println(F("%")); Serial.print (F("Min Value: ")); Serial.print(sensor.min_value); Serial.println(F("%")); Serial.print (F("Resolution: ")); Serial.print(sensor.resolution); Serial.println(F("%")); Serial.println(F("------------------------------------")); // Set delay between sensor readings based on sensor details.
delayMS = sensor.min_delay / 1000; } ////////////////////////
void helloHuman(){ // Initialising the OLED display
display.init(); display.flipScreenVertically(); display.setTextAlignment(TEXT_ALIGN_LEFT); display.clear(); display.setFont(Monospaced_bold_16); display.drawString(0, 0, "Hello Human!"); display.setFont(Monospaced_plain_12); display.drawString(20, 30, "How are you"); display.drawString(40, 50, "today?"); display.display();
delay(3000);
}
////////////////////////
void loop() { ShowTemperature(); delay(1000); }
|