Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- AC Light Control
- Uses up and down buttons to set levels
- makes use of a timer interrupt to set the level of dimming
- */
- //#include <Servo.h>
- #include <ESP8266WiFi.h>
- #include <MQTTClient.h>
- #include <ESP8266WebServer.h>
- #include <ESP8266mDNS.h>
- #include <ESP8266HTTPUpdateServer.h>
- #include <TimerOne.h> // Avaiable from http://www.arduino.cc/playground/Code/Timer1
- volatile int i=0; // Variable to use as a counter of dimming steps. It is volatile since it is passed between interrupts
- volatile boolean zero_cross=0; // Flag to indicate we have crossed zero
- int AC_pin = 3; // Output to Opto Triac
- //int buton1 = 4; // first button at pin 4
- //int buton2 = 5; // second button at pin 5
- int dim2 = 0; // led control
- int dim = 128; // Dimming level (0-128) 0 = on, 128 = 0ff
- int pas = 8; // step for count;
- int freqStep = 75; // This is the delay-per-brightness step in microseconds. It allows for 128 steps
- // If using 60 Hz grid frequency set this to 65
- /* ---------- DO NOT EDIT ANYTHING ABOVE THIS LINE ---------- */
- //Only edit the settings in this section
- /* WIFI Settings */
- // Name of wifi network
- const char* ssid = "ASUS";
- // Password to wifi network
- const char* password = "Stratum@3995";
- /* Web Updater Settings */
- // Host Name of Device
- const char* host = "MK-BlindsControl1";
- // Path to access firmware update page (Not Neccessary to change)
- const char* update_path = "/firmware";
- // Username to access the web update page
- const char* update_username = "admin";
- // Password to access the web update page
- const char* update_password = "admin";
- /* MQTT Settings */
- // Topic which listens for commands
- char* subscribeTopic = "MK-SmartHouse/utilities/MK-BlindsControl1";
- //MQTT Server IP Address
- const char* server = "192.168.10.134";
- //Unique device ID
- const char* mqttDeviceID = "MK-SmartHouseDevice5";
- /* ---------- DO NOT EDIT ANYTHING BELOW THIS LINE ---------- */
- ESP8266WebServer httpServer(80);
- ESP8266HTTPUpdateServer httpUpdater;
- //Servo myservo;
- WiFiClient net;
- MQTTClient client;
- unsigned long lastMillis = 0;
- void connect();
- void setup() { // Begin setup
- Serial.begin(9600);
- //pinMode(buton1, INPUT); // set buton1 pin as input
- //pinMode(buton2, INPUT); // set buton1 pin as input
- pinMode(AC_pin, OUTPUT); // Set the Triac pin as output
- attachInterrupt(0, zero_cross_detect, RISING); // Attach an Interupt to Pin 2 (interupt 0) for Zero Cross Detection
- Timer1.initialize(freqStep); // Initialize TimerOne library for the freq we need
- Timer1.attachInterrupt(dim_check, freqStep); // Go to dim_check procedure every 75 uS (50Hz) or 65 uS (60Hz)
- // Use the TimerOne Library to attach an interrupt
- WiFi.mode(WIFI_STA);
- WiFi.begin(ssid, password);
- client.begin(server, net);
- client.onMessage(messageReceived);
- connect();
- MDNS.begin(host);
- httpUpdater.setup(&httpServer, update_path, update_username, update_password);
- httpServer.begin();
- MDNS.addService("http", "tcp", 80);
- }
- void connect()
- {
- while (WiFi.status() != WL_CONNECTED)
- {
- delay(1000);
- }
- while (!client.connect(mqttDeviceID))
- {
- delay(1000);
- }
- client.subscribe(subscribeTopic);
- }
- void loop()
- {
- client.loop();
- delay(10);
- if(!client.connected())
- {
- connect();
- }
- httpServer.handleClient();
- }
- void messageReceived(String &topic, String &payload, String &payload1)
- {
- String buton1= payload;
- String buton2= payload1;
- // myservo.attach(2);
- //digitalWrite(buton1, HIGH);
- //digitalWrite(buton2, HIGH);
- if (buton1 == "UP")
- {
- if (dim<127)
- {
- dim = dim + pas;
- if (dim>127)
- {
- dim=128;
- }
- }
- }
- if (buton2 == "DOWN")
- {
- if (dim>5)
- {
- dim = dim - pas;
- if (dim<0)
- {
- dim=0;
- }
- }
- }
- while (buton1 == "UP")
- {
- }
- delay(10); // waiting little bit...
- while (buton2 == "DOWN")
- {
- }
- delay(10); // waiting little bit...
- dim2 = 255-2*dim;
- if (dim2<0)
- {
- dim2 = 0;
- }
- Serial.print("dim=");
- Serial.print(dim);
- Serial.print(" dim2=");
- Serial.print(dim2);
- Serial.print(" dim1=");
- Serial.print(2*dim);
- Serial.print('\n');
- delay (100);
- }
- void zero_cross_detect() {
- zero_cross = true; // set flag for dim_check function that a zero cross has occured
- i=0; // stepcounter to 0.... as we start a new cycle
- digitalWrite(AC_pin, LOW);
- }
- // Turn on the TRIAC at the appropriate time
- // We arrive here every 75 (65) uS
- // First check if a flag has been set
- // Then check if the counter 'i' has reached the dimming level
- // if so.... switch on the TRIAC and reset the counter
- void dim_check() {
- if(zero_cross == true) {
- if(i>=dim) {
- digitalWrite(AC_pin, HIGH); // turn on light
- i=0; // reset time step counter
- zero_cross=false; // reset zero cross detection flag
- }
- else {
- i++; // increment time step counter
- }
- }
- }
Add Comment
Please, Sign In to add comment