#include <RBDdimmer.h>
#include <Stdio.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "CTBot.h"
#include "ESP8266WiFi.h"
CTBot myBot;
const int relay = 4;
const int outputPin = 5;
const int zerocross = 7;
#define DELAY 1000
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
#define OLED_RESET 13
#define LED_PIN 14
Adafruit_SSD1306 display(OLED_RESET);
String data;
char c;
#define BH1750_POWER_DOWN 0x00 // No active state
#define BH1750_POWER_ON 0x01 // Waiting for measurement command
#define BH1750_RESET 0x07
#define CONTINUOUS_HIGH_RES_MODE 0x10 // Measurement at 1 lux resolution. Measurement time is approx 120ms
#define CONTINUOUS_HIGH_RES_MODE_2 0x11 // Measurement at 0.5 lux resolution. Measurement time is approx 120ms
#define CONTINUOUS_LOW_RES_MODE 0x13 // Measurement at 4 lux resolution. Measurement time is approx 16ms
#define ONE_TIME_HIGH_RES_MODE 0x20 // Measurement at 1 lux resolution. Measurement time is approx 120ms
#define ONE_TIME_HIGH_RES_MODE_2 0x21 // Measurement at 0.5 lux resolution. Measurement time is approx 120ms
#define ONE_TIME_LOW_RES_MODE 0x23 // Measurement at 4 lux resolution. Measurement time is approx 16ms
#define BH1750_1_ADDRESS 0x23 // Sensor 1 connected to GND
#define BH1750_2_ADDRESS 0x5C // Sensor 2 connected to VCC
int outVal = 0;
dimmerLamp dimmer(outputPin,zerocross);
int16_t s_en = 0;
int16_t s0 = 1;
int16_t s1 = 2;
int16_t s2 = 3;
int16_t RawData;
int16_t SensorValue[4];
void setup() {
Wire.begin();
dimmer.begin(NORMAL_MODE, ON);
Serial.begin(115200); // Baud Rate
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.display();
delay(1);
display.clearDisplay();
Serial.println("Starting TelegramBot...");
myBot.wifiConnect(ssid, pass);
myBot.setTelegramToken(token);
if (myBot.testConnection())
Serial.println("\ntestConnection OK");
else
Serial.println("\ntestConnection NOK");
pinMode(s_en, OUTPUT);
pinMode(s0, OUTPUT);
pinMode(s1, OUTPUT);
pinMode(s2, OUTPUT);
digitalWrite(s_en, HIGH);
digitalWrite(s0, HIGH);
digitalWrite(s1, HIGH);
digitalWrite(s2, HIGH);
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, HIGH);
}
void init_BH1750(int ADDRESS, int MODE){
//BH1750 Initializing & Reset
Wire.beginTransmission(ADDRESS);
Wire.write(MODE); // PWR_MGMT_1 register
Wire.endTransmission(true);
}
void RawData_BH1750(int ADDRESS){
Wire.beginTransmission(ADDRESS);
Wire.requestFrom(ADDRESS,2,true); // request a total of 2 registers
RawData = Wire.read() << 8 | Wire.read(); // Read Raw Data of BH1750
Wire.endTransmission(true);
}
int readMux(int channel){
int controlPin[] = {s0, s1, s2};
int muxChannel[8][3] = {
{0,0,0}, //channel 0
{1,0,0}, //channel 1
{0,1,0}, //channel 2
{1,1,0}, //channel 3
{0,0,1}, //channel 4
{1,0,1}, //channel 5
{0,1,1}, //channel 6
{1,1,1}, //channel 7 //loop through the 3 Signals
};
for(int i = 0; i < 3; i++){ // Connecting MUX Channel
digitalWrite(controlPin[i], muxChannel[channel][i]);
}
return;
}
void loop() {
TBMessage msg;
if (myBot.getNewMessage(msg)) {
if (msg.text.equalsIgnoreCase("RELAY ON")) {
myBot.sendMessage(msg.sender.id, "RELAY is now ON"); //kirim pesan ke bot telegram
Serial.print("RELAY ON");
}
else if (msg.text.equalsIgnoreCase("RELAY OFF")) {
myBot.sendMessage(msg.sender.id, "RELAY is now OFF");
Serial.print("RELAY OFF");
}
delay(500);
}
digitalWrite(s_en, LOW); // Enabling the Enable Switch
for(int i = 0; i < 4; i++){
readMux(i);
init_BH1750(BH1750_1_ADDRESS, CONTINUOUS_HIGH_RES_MODE);
delay(120);
RawData_BH1750(BH1750_1_ADDRESS);
SensorValue[i] = RawData / 1.2;
delay(20);
}
Serial.print("Sensor_1 = "); Serial.print(SensorValue[0]);
Serial.print(" | Sensor_2 = "); Serial.print(SensorValue[1]);
Serial.print(" | Sensor_3 = "); Serial.print(SensorValue[2]);
Serial.print(" | Sensor_4 = "); Serial.println(SensorValue[3]);
int x= SensorValue[0] + SensorValue[1] + SensorValue[2] + SensorValue[3];
int y= 4;
int z= x / y;
Serial.print("Lux = ");Serial.println(z);
if (z =500){
outVal =0;
}
else {
if (z<500)
while (z<500){
outVal -=1;
delay(DELAY);
}
}
if (z >500)
while (z>500){
outVal += 1;
delay(DELAY);
}
dimmer.setPower(outVal);
Serial.print("Dimmer = "); Serial.println(outVal);
delay(DELAY);
}
i'm sorry, this the full sketch