Guest User

Untitled

a guest
Oct 31st, 2017
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.55 KB | None | 0 0
  1. /*
  2. AC Light Control
  3. Uses up and down buttons to set levels
  4. makes use of a timer interrupt to set the level of dimming
  5. */
  6.  
  7. //#include <Servo.h>
  8. #include <ESP8266WiFi.h>
  9. #include <MQTTClient.h>
  10. #include <ESP8266WebServer.h>
  11. #include <ESP8266mDNS.h>
  12. #include <ESP8266HTTPUpdateServer.h>
  13. #include <TimerOne.h> // Avaiable from http://www.arduino.cc/playground/Code/Timer1
  14.  
  15.  
  16. volatile int i=0; // Variable to use as a counter of dimming steps. It is volatile since it is passed between interrupts
  17. volatile boolean zero_cross=0; // Flag to indicate we have crossed zero
  18. int AC_pin = 3; // Output to Opto Triac
  19. //int buton1 = 4; // first button at pin 4
  20. //int buton2 = 5; // second button at pin 5
  21. int dim2 = 0; // led control
  22. int dim = 128; // Dimming level (0-128) 0 = on, 128 = 0ff
  23. int pas = 8; // step for count;
  24. int freqStep = 75; // This is the delay-per-brightness step in microseconds. It allows for 128 steps
  25. // If using 60 Hz grid frequency set this to 65
  26.  
  27. /* ---------- DO NOT EDIT ANYTHING ABOVE THIS LINE ---------- */
  28.  
  29. //Only edit the settings in this section
  30.  
  31. /* WIFI Settings */
  32. // Name of wifi network
  33. const char* ssid = "ASUS";
  34.  
  35. // Password to wifi network
  36. const char* password = "Stratum@3995";
  37.  
  38. /* Web Updater Settings */
  39. // Host Name of Device
  40. const char* host = "MK-BlindsControl1";
  41.  
  42. // Path to access firmware update page (Not Neccessary to change)
  43. const char* update_path = "/firmware";
  44.  
  45. // Username to access the web update page
  46. const char* update_username = "admin";
  47.  
  48. // Password to access the web update page
  49. const char* update_password = "admin";
  50.  
  51. /* MQTT Settings */
  52. // Topic which listens for commands
  53. char* subscribeTopic = "MK-SmartHouse/utilities/MK-BlindsControl1";
  54.  
  55. //MQTT Server IP Address
  56. const char* server = "192.168.10.134";
  57.  
  58. //Unique device ID
  59. const char* mqttDeviceID = "MK-SmartHouseDevice5";
  60.  
  61.  
  62. /* ---------- DO NOT EDIT ANYTHING BELOW THIS LINE ---------- */
  63.  
  64. ESP8266WebServer httpServer(80);
  65. ESP8266HTTPUpdateServer httpUpdater;
  66.  
  67. //Servo myservo;
  68. WiFiClient net;
  69. MQTTClient client;
  70.  
  71. unsigned long lastMillis = 0;
  72.  
  73. void connect();
  74.  
  75.  
  76. void setup() { // Begin setup
  77. Serial.begin(9600);
  78. //pinMode(buton1, INPUT); // set buton1 pin as input
  79. //pinMode(buton2, INPUT); // set buton1 pin as input
  80. pinMode(AC_pin, OUTPUT); // Set the Triac pin as output
  81. attachInterrupt(0, zero_cross_detect, RISING); // Attach an Interupt to Pin 2 (interupt 0) for Zero Cross Detection
  82. Timer1.initialize(freqStep); // Initialize TimerOne library for the freq we need
  83. Timer1.attachInterrupt(dim_check, freqStep); // Go to dim_check procedure every 75 uS (50Hz) or 65 uS (60Hz)
  84. // Use the TimerOne Library to attach an interrupt
  85.  
  86.  
  87.  
  88. WiFi.mode(WIFI_STA);
  89. WiFi.begin(ssid, password);
  90. client.begin(server, net);
  91. client.onMessage(messageReceived);
  92.  
  93. connect();
  94.  
  95. MDNS.begin(host);
  96.  
  97. httpUpdater.setup(&httpServer, update_path, update_username, update_password);
  98. httpServer.begin();
  99.  
  100. MDNS.addService("http", "tcp", 80);
  101. }
  102.  
  103.  
  104. void connect()
  105. {
  106. while (WiFi.status() != WL_CONNECTED)
  107. {
  108. delay(1000);
  109. }
  110.  
  111. while (!client.connect(mqttDeviceID))
  112. {
  113. delay(1000);
  114. }
  115.  
  116. client.subscribe(subscribeTopic);
  117. }
  118.  
  119.  
  120.  
  121. void loop()
  122. {
  123. client.loop();
  124. delay(10);
  125.  
  126. if(!client.connected())
  127. {
  128. connect();
  129. }
  130.  
  131. httpServer.handleClient();
  132.  
  133. }
  134.  
  135.  
  136. void messageReceived(String &topic, String &payload, String &payload1)
  137. {
  138. String buton1= payload;
  139. String buton2= payload1;
  140. // myservo.attach(2);
  141. //digitalWrite(buton1, HIGH);
  142. //digitalWrite(buton2, HIGH);
  143.  
  144. if (buton1 == "UP")
  145. {
  146. if (dim<127)
  147. {
  148. dim = dim + pas;
  149. if (dim>127)
  150. {
  151. dim=128;
  152. }
  153. }
  154. }
  155. if (buton2 == "DOWN")
  156. {
  157. if (dim>5)
  158. {
  159. dim = dim - pas;
  160. if (dim<0)
  161. {
  162. dim=0;
  163. }
  164. }
  165. }
  166. while (buton1 == "UP")
  167. {
  168. }
  169. delay(10); // waiting little bit...
  170.  
  171. while (buton2 == "DOWN")
  172. {
  173. }
  174. delay(10); // waiting little bit...
  175.  
  176.  
  177. dim2 = 255-2*dim;
  178. if (dim2<0)
  179. {
  180. dim2 = 0;
  181. }
  182.  
  183. Serial.print("dim=");
  184. Serial.print(dim);
  185. Serial.print(" dim2=");
  186. Serial.print(dim2);
  187. Serial.print(" dim1=");
  188. Serial.print(2*dim);
  189. Serial.print('\n');
  190. delay (100);
  191.  
  192. }
  193.  
  194.  
  195. void zero_cross_detect() {
  196. zero_cross = true; // set flag for dim_check function that a zero cross has occured
  197. i=0; // stepcounter to 0.... as we start a new cycle
  198. digitalWrite(AC_pin, LOW);
  199. }
  200.  
  201. // Turn on the TRIAC at the appropriate time
  202. // We arrive here every 75 (65) uS
  203. // First check if a flag has been set
  204. // Then check if the counter 'i' has reached the dimming level
  205. // if so.... switch on the TRIAC and reset the counter
  206. void dim_check() {
  207. if(zero_cross == true) {
  208. if(i>=dim) {
  209. digitalWrite(AC_pin, HIGH); // turn on light
  210. i=0; // reset time step counter
  211. zero_cross=false; // reset zero cross detection flag
  212. }
  213. else {
  214. i++; // increment time step counter
  215. }
  216. }
  217. }
Add Comment
Please, Sign In to add comment