Last active
December 3, 2022 11:55
-
-
Save SimedruF/3cbc30156e79b9709e3984b6d74ad678 to your computer and use it in GitHub Desktop.
esp32_GY49_MAX44009
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Florin Simedru | |
Complete project details at https://automatic-house.blogspot.com | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files. | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
*/ | |
/* | |
; PlatformIO Project Configuration File | |
; | |
; Build options: build flags, source filter | |
; Upload options: custom upload port, speed and extra flags | |
; Library options: dependencies, extra library storages | |
; Advanced options: extra scripting | |
; | |
; Please visit documentation for the other options and examples | |
; https://docs.platformio.org/page/projectconf.html | |
[platformio] | |
default_envs = esp32doit-devkit-v1 | |
[env] | |
monitor_port = COM7 | |
[env:esp32doit-devkit-v1] | |
platform = espressif32 | |
board = esp32doit-devkit-v1 | |
framework = arduino | |
monitor_speed = 115200 | |
upload_port = COM7 | |
lib_deps = mbed-components/[email protected]+sha.cf4d7779d9d6 | |
*/ | |
#include <Arduino.h> | |
#include "BluetoothSerial.h" | |
#include<Wire.h> | |
// Check if Bluetooth configs are enabled | |
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED) | |
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it | |
#endif | |
// MAX44009 I2C address is 0x4A(74) | |
#define Addr 0x4A | |
#define LED_PIN LED_BUILTIN | |
// Bluetooth Serial object | |
BluetoothSerial SerialBT; | |
// Handle received and sent messages | |
String message = ""; | |
char incomingChar; | |
String lightString; | |
// Timer: auxiliar variables | |
unsigned long previousMillis = 0; // Stores last time temperature was published | |
const long interval = 10000; // interval at which to publish sensor readings | |
void setup() | |
{ | |
// Initialise I2C communication as MASTER | |
Wire.begin(); | |
// Initialise serial communication, set baud rate = 9600 | |
Serial.begin(115200); | |
// Bluetooth device name | |
SerialBT.begin("ESP32_LIGH_SENZOR"); | |
// Start I2C Transmission | |
Wire.beginTransmission(Addr); | |
// Select configuration register | |
Wire.write(0x02); | |
// Continuous mode, Integration time = 800 ms | |
Wire.write(0x40); | |
// Stop I2C transmission | |
Wire.endTransmission(); | |
/* Initialize Buildin led pin (D2) for output */ | |
pinMode(LED_PIN,OUTPUT); | |
delay(300); | |
} | |
void loop() | |
{ | |
unsigned int data[2]; | |
unsigned long currentMillis = millis(); | |
// Start I2C Transmission | |
Wire.beginTransmission(Addr); | |
// Select data register | |
Wire.write(0x03); | |
// Stop I2C transmission | |
Wire.endTransmission(); | |
// Request 2 bytes of data | |
Wire.requestFrom(Addr, 2); | |
// Read 2 bytes of data | |
// luminance msb, luminance lsb | |
if (Wire.available() == 2) | |
{ | |
data[0] = Wire.read(); | |
data[1] = Wire.read(); | |
} | |
// Convert the data to lux | |
int exponent = (data[0] & 0xF0) >> 4; | |
int mantissa = ((data[0] & 0x0F) << 4) | (data[1] & 0x0F); | |
float luminance = pow(2, exponent) * mantissa * 0.72; | |
// Output data to serial monitor | |
lightString = "Ambient Light luminance : "+String(luminance)+" lux"; | |
Serial.println(lightString); | |
// Send light luminance readings | |
if (currentMillis - previousMillis >= interval){ | |
previousMillis = currentMillis; | |
SerialBT.println(lightString); | |
} | |
// Read received messages (LED control command) | |
if (SerialBT.available()){ | |
char incomingChar = SerialBT.read(); | |
if (incomingChar != '\n'){ | |
message += String(incomingChar); | |
} | |
else{ | |
message = ""; | |
} | |
} | |
if(message.isEmpty()==false) | |
{ | |
// Check received message and control output accordingly | |
if (message.equals("led_on") && message.endsWith("n")) | |
{ | |
digitalWrite(LED_PIN, HIGH); | |
Serial.println(message); | |
} | |
else if (message.equals("led_off")&& message.endsWith("f")) | |
{ | |
digitalWrite(LED_PIN, LOW); | |
Serial.println(message); | |
} | |
} | |
delay(500); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment