153 lines
4.1 KiB
C++
Raw Normal View History

2024-07-13 23:48:32 -04:00
#include <Arduino.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>
2024-07-14 00:18:25 -04:00
#include <Wire.h>
2024-07-13 23:48:32 -04:00
#include <Adafruit_BME280.h>
2024-07-14 01:02:19 -04:00
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
2024-07-13 23:48:32 -04:00
#include "config.h"
2024-07-15 12:04:11 -04:00
//#include "epaper.h"
2024-07-13 23:48:32 -04:00
Adafruit_BME280 bme;
2024-07-14 00:59:39 -04:00
// Define tasks.
2024-07-16 18:12:20 -04:00
TaskHandle_t TaskStatusLED, TaskSensorValues, TaskLightsAuto;
2024-07-14 00:59:39 -04:00
// Dummy task blinking built-in LED
2024-07-14 01:01:19 -04:00
void TaskStatusLEDCode (void* parameters) {
2024-07-14 01:15:26 -04:00
Serial.print("Task 0 running on core # ");
Serial.println(xPortGetCoreID());
2024-07-14 00:59:39 -04:00
2024-07-14 01:23:05 -04:00
for (;;) {
digitalWrite(SYS_LED_PIN, LOW);
vTaskDelay(250 / portTICK_RATE_MS);
digitalWrite(SYS_LED_PIN, HIGH);
vTaskDelay(250 / portTICK_RATE_MS);
digitalWrite(SYS_LED_PIN, LOW);
vTaskDelay(250 / portTICK_RATE_MS);
digitalWrite(SYS_LED_PIN, HIGH);
2024-07-14 01:29:41 -04:00
vTaskDelay(pdMS_TO_TICKS(1000));
2024-07-14 01:23:05 -04:00
}
2024-07-14 00:59:39 -04:00
}
2024-07-14 01:23:50 -04:00
2024-07-16 18:12:20 -04:00
void TaskLightsAutoCode(void* parameters) {
2024-07-16 18:21:34 -04:00
for (;;) {
if (light_sensor_reading > 1000)
digitalWrite(LIGHTS_PIN, HIGH);
else
digitalWrite(LIGHTS_PIN, LOW);
vTaskDelay(pdMS_TO_TICKS(5000));
}
2024-07-16 18:12:20 -04:00
}
2024-07-14 01:01:19 -04:00
void TaskSensorValuesCode (void* parameters) {
2024-07-14 01:23:05 -04:00
for (;;) {
2024-07-15 18:10:12 -04:00
lighting_percentage = map(analogRead(ADC1), 0.0f, 4095.0f, 0, 100);
2024-07-14 01:23:05 -04:00
Serial.println("BME-280 Sensors Readings ...");
2024-07-14 01:33:07 -04:00
Serial.print("Temperature:\t\t");
2024-07-14 01:23:05 -04:00
Serial.print(bme.readTemperature());
2024-07-14 01:39:14 -04:00
Serial.println("\t°C");
2024-07-14 01:33:07 -04:00
Serial.print("Humidity:\t\t");
2024-07-14 01:23:05 -04:00
Serial.print(bme.readHumidity());
2024-07-14 01:39:14 -04:00
Serial.println("\t%");
2024-07-14 01:34:09 -04:00
Serial.print("Barometric Pressure:\t");
2024-07-14 01:23:05 -04:00
Serial.print(bme.readPressure() / 100.0F);
2024-07-14 01:39:14 -04:00
Serial.println("\tkPa");
2024-07-15 17:02:27 -04:00
// ADC GPIO1
light_sensor_reading = analogRead(ADC1);
2024-07-15 17:18:23 -04:00
Serial.print("Light:\t\t\t");
2024-07-16 08:50:43 -04:00
Serial.print(light_sensor_reading);
2024-07-16 09:08:46 -04:00
Serial.print(" (");
Serial.print(100.00 - lighting_percentage);
Serial.println("%)");
2024-07-14 01:29:41 -04:00
vTaskDelay(pdMS_TO_TICKS(5000));
2024-07-15 17:02:27 -04:00
}
2024-07-14 01:01:19 -04:00
}
2024-07-14 00:59:39 -04:00
2024-07-13 23:48:32 -04:00
void setup() {
2024-07-16 18:11:08 -04:00
Serial.println("Please wait 2 seconds ...");
2024-07-15 15:52:05 -04:00
delay(2000);
2024-07-15 11:57:59 -04:00
Serial.begin(9600);
2024-07-13 23:48:32 -04:00
Serial.println();
2024-07-15 11:53:08 -04:00
2024-07-13 23:48:32 -04:00
Serial.println("Setting up GPIOs ...");
pinMode(LED_PIN, OUTPUT);
2024-07-16 18:21:34 -04:00
pinMode(LIGHTS_PIN, OUTPUT);
digitalWrite(LIGHTS_PIN, LOW);
2024-07-13 23:48:32 -04:00
2024-07-16 18:13:47 -04:00
Serial.println("GPIO setup done");
xTaskCreate(TaskLightsAutoCode, "Lights auto", 4096, NULL, 4, &TaskLightsAuto);
2024-07-13 23:48:32 -04:00
2024-07-14 00:18:25 -04:00
Serial.println("Setting up BME280 sensor");
2024-07-14 00:25:02 -04:00
Wire.setPins(SDA_PIN, SCL_PIN);
2024-07-14 00:26:02 -04:00
Wire.begin();
2024-07-14 00:27:19 -04:00
//unsigned status = bme.begin(0x76); // 0x76
if (!bme.begin(0x76)) {
2024-07-13 23:48:32 -04:00
Serial.println("Could not find a valid BME/BMP280 sensor, check wiring!");
Serial.print("SensorID was: 0x"); Serial.println(bme.sensorID(),16);
Serial.print(" ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n");
Serial.print(" ID of 0x56-0x58 represents a BMP 280,\n");
Serial.print(" ID of 0x60 represents a BME 280.\n");
Serial.print(" ID of 0x61 represents a BME 680.\n");
2024-07-14 22:44:21 -04:00
int n = 10;
while (1) { //n > 0) {
2024-07-14 00:18:25 -04:00
digitalWrite(LED_PIN, LOW);
2024-07-14 00:50:38 -04:00
delay(150);
2024-07-14 00:18:25 -04:00
digitalWrite(LED_PIN, HIGH);
2024-07-14 00:50:38 -04:00
delay(150);
2024-07-15 12:04:11 -04:00
Serial.println("Could not find a valid BME/BMP280 sensor!");
2024-07-14 22:44:21 -04:00
n--;
2024-07-14 00:18:25 -04:00
}
2024-07-14 00:27:19 -04:00
}
2024-07-14 01:06:28 -04:00
else {
Serial.println("Sensor was found.");
2024-07-14 01:23:05 -04:00
xTaskCreate(TaskStatusLEDCode, "Status LED Task", 4096, NULL, 10, &TaskStatusLED);
xTaskCreate(TaskSensorValuesCode, "Status LED Task", 4096, NULL, 5, &TaskSensorValues);
2024-07-14 01:06:28 -04:00
}
2024-07-14 21:01:25 -04:00
2024-07-15 11:53:08 -04:00
/*// Initialize ePaper display
2024-07-14 21:01:25 -04:00
display.init(115200);
display.fillScreen(GxEPD_BLACK);
display.update();
display.fillScreen(GxEPD_WHITE);
display.setTextColor(GxEPD_BLACK);
display.setFont(&FreeMono9pt7b);
//display.setCursor(205, 10);
//display.print("ESP32_DisplayNode"); // Takes 195 pixels in width
display.setCursor(5, 5);
display.setFont(&TomThumb);
2024-07-15 00:06:36 -04:00
display.print("10.100.50.xxx");
2024-07-15 11:53:08 -04:00
display.update();*/
2024-07-13 23:48:32 -04:00
}
void loop() {
2024-07-14 22:44:21 -04:00
2024-07-15 11:53:08 -04:00
/*Serial.println("Loop");
2024-07-14 22:44:21 -04:00
Serial.print("SS = ");
Serial.println(SS);
2024-07-15 11:49:44 -04:00
Serial.print("MOSI = ");
Serial.println(MOSI);
Serial.print("MISO = ");
Serial.println(MISO);
Serial.print("SCK = ");
Serial.println(SCK);
Serial.print("BUSY = ");
Serial.println(BUSY_PIN);
2024-07-15 11:53:08 -04:00
sleep(1);*/
2024-07-14 22:44:21 -04:00
2024-07-14 18:59:36 -04:00
}
/*
#elif defined(ESP32) && (CONFIG_IDF_TARGET_ESP32C3)
#define DF_GFX_SCK 4
#define DF_GFX_MOSI 6
#define DF_GFX_MISO GFX_NOT_DEFINED
#define DF_GFX_CS 7
#define DF_GFX_DC 2
#define DF_GFX_RST 1
#define DF_GFX_BL 3*/