Voice Controlled Curtain Using Alexa and ESP32
Curtain can be raised or lowered using voice or smartphone application.
Components and supplies
1
5 v power supply
1
SparkFun ESP32 Thing
1
2 relay board
1
3 leds module
1
connectors
Apps and platforms
1
sinric
1
Alexa Skills Kit
Project description
Code
Alexa controlled curtain
c_cpp
refer to github Sinric for instructions to create the sw environment
1[code] 2#include <Arduino.h> 3 4#include <WiFi.h> 5#include <WiFiMulti.h> 6#include <WebSocketsClient.h> 7#include <ArduinoJson.h> // get it from https://arduinojson.org/ or install via Arduino library manager 8#include <SimpleTimer.h> 9#include <Time.h> 10WiFiMulti WiFiMulti; 11WebSocketsClient webSocket; 12 13//****************************************************************** 14 15#define MyApiKey "xxxxxxxxxxxxxxxxxxxxxxxx" // TODO: Change to your sinric API Key. Your API Key is displayed on sinric.com dashboard 16//#define MySSID "xxxxxxxx" // TODO: Change to your Wifi network SSID 17//#define MyWifiPassword "xxxxxxxx" // TODO: Change to your Wifi network password 18//#define MyDeviceID "xxxxxxxxxxxxxxxxxxxxxxx" 19#define MySSID "xxxxxxxx" 20#define MyWifiPassword "xxxxxx" 21#define LED 2 22#define red 27 23#define green 26 24int connec_ted = 2; 25// assegnazione dei pin esp32 26int status_on = 25; //era 25 27int lower = 32; 28int raise_up = 33; 29int connection = LOW; 30int action_time = 35; //35 times 1 second 31int what_to_do; 32int traffic_light = LOW; 33int do_something = LOW; 34 35 36SimpleTimer timer; 37 38 39 40//*********************************************************************************************** 41 42void setup() { 43 Serial.begin(115200); 44 45 WiFiMulti.addAP(MySSID, MyWifiPassword); 46 Serial.println("alexa_curtain 1.1"); 47 Serial.print("Connecting to Wifi: "); 48 Serial.println(MySSID); 49 pinMode(connec_ted, OUTPUT); 50 pinMode(LED, OUTPUT); 51 pinMode(red, OUTPUT); 52 pinMode(green, OUTPUT); 53 pinMode (status_on, OUTPUT); 54 pinMode (raise_up, OUTPUT); 55 pinMode (lower, OUTPUT); 56 digitalWrite(raise_up, LOW); 57 digitalWrite(lower, LOW); 58 digitalWrite(connec_ted, HIGH); 59 digitalWrite(red, HIGH); 60 digitalWrite(green, HIGH); 61 62 do_something = LOW; // action switch on high ther is something to do 63 64 xTaskCreatePinnedToCore(ActionTask, "ActionTask ", 4000, NULL, 5, NULL, 0); //Task for Core 1 priority set at 5 65 // Waiting for Wifi connect 66 while (WiFiMulti.run() != WL_CONNECTED) { 67 delay(500); 68 if (connection == LOW) 69 { 70 digitalWrite(connec_ted, HIGH); 71 digitalWrite(status_on, LOW); 72 connection = HIGH; 73 } else { 74 digitalWrite(connec_ted, LOW); 75 digitalWrite(status_on, HIGH); 76 connection = LOW; 77 } 78 Serial.print("."); 79 } 80 if (WiFiMulti.run() == WL_CONNECTED) { 81 Serial.println(""); 82 Serial.print("WiFi connected. "); 83 Serial.print("IP address: "); 84 Serial.println(WiFi.localIP()); 85 digitalWrite(connec_ted, HIGH); 86 digitalWrite(status_on, LOW); 87 } 88 89 // server address, port and URL 90 webSocket.begin("iot.sinric.com", 80, "/"); 91 92 // event handler 93 webSocket.onEvent(webSocketEvent); 94 webSocket.setAuthorization("apikey", MyApiKey); 95} 96//*********************************************************************************************** 97 98void loop() { 99 webSocket.loop(); 100 delay(1); 101 102} 103 104//*********************************************************************************************** 105 106 107void turnOn(String MyDeviceID) { 108 if (MyDeviceID == "mysinric deviceid") // Device ID of first device 109 { 110 Serial.print("Turn ON first device with ID: "); 111 Serial.println(MyDeviceID); 112 } 113 114} 115 116//*********************************************************************************************** 117 118void turnOff(String MyDeviceID) { 119 if (MyDeviceID == "mysinric deviceid") // Device ID of device 120 { 121 Serial.print("Turn OFF first device with ID: "); 122 Serial.println(MyDeviceID); 123 } 124} 125 126 127//*********************************************************************************************** 128 129char minus = '-'; 130 131int n; 132 133void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) 134 135{ 136 switch (type) { 137 case WStype_DISCONNECTED: 138 { Serial.printf("[WSc] Webservice DISCONNECTED from sinric.com!\ 139"); 140 } break; 141 case WStype_CONNECTED: { 142 Serial.printf("[WSc] Service connected to sinric.com at url: %s\ 143", payload); 144 Serial.printf("Waiting for commands from sinric.com ...\ 145"); 146 } 147 break; 148 case WStype_TEXT: { 149 Serial.printf("[WSc] get text: %s\ 150", payload); 151 // Example payload for ON : 152 //{"deviceId":"5a2b908a74a6703928abf38b","powerState":"ON"} 153 DynamicJsonBuffer jsonBuffer (4000); 154 JsonObject& json = jsonBuffer.parseObject((char*)payload); 155 String deviceId = json ["deviceId"]; 156 String action = json ["action"]; 157 String value = json ["value"]; 158 159 if (deviceId == "mysinric deviceid") // Device ID of first device 160 { 161 if (traffic_light == LOW) 162 { if ( action == "setPowerState") 163 { Serial.println ("entry from power"); 164 if (value == "ON") 165 { what_to_do = HIGH; //raise_up 166 do_something = HIGH; 167 } 168 if (value == "OFF") 169 { what_to_do = LOW; //lower 170 do_something = HIGH; 171 } 172 173 } if (action == "SetBrightness" ) 174 175 { Serial.println ("entry from brightness"); // if below 50% lower otherwise raise 176 Serial.print (value ); 177 int n = (value [0] - 48) * 10; 178 n = n + (value [1] - 48); 179 Serial.println (n ); 180 Serial.print (" value 1 "); 181 Serial.println (value [1] ); 182 if (n > 50 ) 183 { 184 what_to_do = HIGH; //raise_up 185 do_something = HIGH; 186 Serial.println ("raise_up from brightness"); 187 } 188 if (n <= 50 ) 189 { what_to_do = LOW; //lower 190 do_something = HIGH; Serial.println ("lower from brightness"); 191 } 192 193 } 194 195 if ( action == "AdjustBrightness") 196 197 { Serial.println ("entry from adjustbrightness"); 198 Serial.print (value ); 199 if ( value [0] != minus) 200 { 201 what_to_do = HIGH; //raise_up 202 do_something = HIGH; 203 Serial.println ("raise_up from brightness"); 204 } 205 if ( value [0] == minus) 206 { what_to_do = LOW; //lower 207 do_something = HIGH; Serial.println ("lower from brightness"); 208 } 209 210 } 211 } else { 212 traffic_light = LOW; 213 digitalWrite(raise_up, LOW);// stop action 214 digitalWrite(lower, LOW); 215 } 216 } 217 } break; 218 case WStype_BIN: 219 { Serial.printf("[WSc] get binary length: %u\ 220", length); 221 } break; 222 } 223 224} 225 226 227 228//**************************************************** 229 230void ActionTask (void * pvParameters) 231 232{ 233 while (1) 234 { 235 if (do_something == HIGH) 236 { traffic_light = HIGH; 237 digitalWrite(LED, HIGH); 238 digitalWrite(status_on, HIGH); 239 { if (what_to_do == HIGH) // raise_up curtain 240 { digitalWrite(red, LOW); 241 digitalWrite(raise_up, HIGH); //funziona con logica inversa 242 actiontime (); 243 digitalWrite(raise_up, LOW); 244 digitalWrite(red, HIGH); 245 } 246 else // lower curtain 247 { 248 if (traffic_light == HIGH) 249 { digitalWrite(green, LOW); 250 digitalWrite(lower, HIGH); //this depend on the relay board switch it could be low or high for operate 251 actiontime (); 252 } 253 digitalWrite(lower, LOW); 254 digitalWrite(green, HIGH); 255 } 256 257 } 258 digitalWrite(LED, LOW); 259 digitalWrite(status_on, LOW); 260 traffic_light = LOW; 261 do_something = LOW; 262 } 263 264 265 vTaskDelay(2); 266 } 267} 268 269 270//*********************************************************************************************** 271 272void actiontime () 273{ for (int i = 0; i < action_time; i++) 274 { if (traffic_light == HIGH) 275 vTaskDelay(1000); 276 } 277} 278 279//*********************************************************************************************** 280 281 282 283 284 285[/code]
Alexa controlled curtain
c_cpp
Refer to sinric page on github for instruction. Then go to sinric.com and create account and devices. copy MyApi.ey and MyDeviceID in the code. Insert your wifi network name and password.
1[code] 2#include <Arduino.h> 3 4#include <WiFi.h> 5#include <WiFiMulti.h> 6#include 7 <WebSocketsClient.h> 8#include <ArduinoJson.h> // get it from https://arduinojson.org/ 9 or install via Arduino library manager 10#include "pitches.h" 11#include <SimpleTimer.h> 12#include 13 <Time.h> 14WiFiMulti WiFiMulti; 15WebSocketsClient webSocket; 16 17//****************************************************************** 18 19#define 20 MyApiKey "xxxxxxxxxxxxxxxxxxxxxxxx" // TODO: Change to your sinric API Key. Your 21 API Key is displayed on sinric.com dashboard 22//#define MySSID "xxxxxxxx" // 23 TODO: Change to your Wifi network SSID 24//#define MyWifiPassword "xxxxxxxx" 25 // TODO: Change to your Wifi network password 26//#define MyDeviceID "xxxxxxxxxxxxxxxxxxxxxxx" 27#define 28 MySSID "xxxxxxxx" 29#define MyWifiPassword "xxxxxx" 30#define LED 2 31#define 32 red 27 33#define green 26 34int connec_ted = 2; 35// assegnazione dei pin 36 esp32 37int status_on = 25; //era 25 38int lower = 32; 39int raise_up = 33; 40int 41 connection = LOW; 42int action_time = 35; //35 times 1 second 43int what_to_do; 44int 45 traffic_light = LOW; 46int do_something = LOW; 47 48 49SimpleTimer timer; 50 51 52 53//*********************************************************************************************** 54 55void 56 setup() { 57 Serial.begin(115200); 58 59 WiFiMulti.addAP(MySSID, MyWifiPassword); 60 61 Serial.println("alexa_curtain 1.1"); 62 Serial.print("Connecting to Wifi: 63 "); 64 Serial.println(MySSID); 65 pinMode(connesso, OUTPUT); 66 pinMode(LED, 67 OUTPUT); 68 pinMode(red, OUTPUT); 69 pinMode(green, OUTPUT); 70 pinMode (status_on, 71 OUTPUT); 72 pinMode (raise_up, OUTPUT); 73 pinMode (lower, OUTPUT); 74 digitalWrite(raise_up, 75 LOW); 76 digitalWrite(lower, LOW); 77 digitalWrite(connesso, HIGH); 78 digitalWrite(red, 79 HIGH); 80 digitalWrite(green, HIGH); 81 82 do_something = LOW; // action 83 switch on high ther is something to do 84 85 xTaskCreatePinnedToCore(ActionTask, 86 "ActionTask ", 4000, NULL, 5, NULL, 0); //Task for Core 1 priority set at 5 87 88 // Waiting for Wifi connect 89 while (WiFiMulti.run() != WL_connected) { 90 91 delay(500); 92 if (connection == LOW) 93 { 94 digitalWrite(connesso, 95 HIGH); 96 digitalWrite(status_on, LOW); 97 connection = HIGH; 98 } 99 else { 100 digitalWrite(connesso, LOW); 101 digitalWrite(status_on, HIGH); 102 103 connection = LOW; 104 } 105 Serial.print("."); 106 } 107 if (WiFiMulti.run() 108 == WL_connected) { 109 Serial.println(""); 110 Serial.print("WiFi connected. 111 "); 112 Serial.print("IP address: "); 113 Serial.println(WiFi.localIP()); 114 115 digitalWrite(connesso, HIGH); 116 digitalWrite(status_on, LOW); 117 } 118 119 120 // server address, port and URL 121 webSocket.begin("iot.sinric.com", 80, "/"); 122 123 124 // event handler 125 webSocket.onEvent(webSocketEvent); 126 webSocket.setAuthorization("apikey", 127 MyApiKey); 128} 129//*********************************************************************************************** 130 131void 132 loop() { 133 webSocket.loop(); 134 delay(1); 135 136} 137 138//*********************************************************************************************** 139 140 141void 142 turnOn(String MyDeviceID) { 143 if (MyDeviceID == "mysinric deviceid") // Device 144 ID of first device 145 { 146 Serial.print("Turn ON first device with ID: "); 147 148 Serial.println(MyDeviceID); 149 } 150 151} 152 153//*********************************************************************************************** 154 155void 156 turnOff(String MyDeviceID) { 157 if (MyDeviceID == "mysinric deviceid") // Device 158 ID of device 159 { 160 Serial.print("Turn OFF first device with ID: "); 161 162 Serial.println(MyDeviceID); 163 } 164} 165 166 167//*********************************************************************************************** 168 169char 170 minus = '-'; 171 172int n; 173 174void webSocketEvent(WStype_t type, uint8_t * payload, 175 size_t length) 176 177{ 178 switch (type) { 179 case WStype_DISconnec_ted: 180 181 { Serial.printf("[WSc] Webservice disconnec_ted from sinric.com!\ 182"); 183 184 } break; 185 case WStype_connec_ted: { 186 Serial.printf("[WSc] 187 Service connected to sinric.com at url: %s\ 188", payload); 189 Serial.printf("Waiting 190 for commands from sinric.com ...\ 191"); 192 } 193 break; 194 case WStype_TEXT: 195 { 196 Serial.printf("[WSc] get text: %s\ 197", payload); 198 // Example 199 payload for ON : 200 //{"deviceId":"5a2b908a74a6703928abf38b","powerState":"ON"} 201 202 DynamicJsonBuffer jsonBuffer (4000); 203 JsonObject& json = jsonBuffer.parseObject((char*)payload); 204 205 String deviceId = json ["deviceId"]; 206 String action = json ["action"]; 207 208 String value = json ["value"]; 209 210 if (deviceId == "mysinric 211 deviceid") // Device ID of first device 212 { 213 if (traffic_light 214 == LOW) 215 { if ( action == "setPowerState") 216 { Serial.println 217 ("entry from power"); 218 if (value == "ON") 219 { 220 what_to_do = HIGH; //raise_up 221 do_something = HIGH; 222 } 223 224 if (value == "OFF") 225 { what_to_do = LOW; //lower 226 227 do_something = HIGH; 228 } 229 230 } if 231 (action == "SetBrightness" ) 232 233 { Serial.println ("entry from 234 brightness"); // if below 50% lower otherwise raise 235 Serial.print 236 (value ); 237 int n = (value [0] - 48) * 10; 238 n = n 239 + (value [1] - 48); 240 Serial.println (n ); 241 Serial.print 242 (" value 1 "); 243 Serial.println (value [1] ); 244 if 245 (n > 50 ) 246 { 247 what_to_do = HIGH; //raise_up 248 249 do_something = HIGH; 250 Serial.println ("raise_up 251 from brightness"); 252 } 253 if (n <= 50 ) 254 { 255 what_to_do = LOW; //lower 256 do_something = HIGH; Serial.println 257 ("lower from brightness"); 258 } 259 260 } 261 262 if 263 ( action == "AdjustBrightness") 264 265 { Serial.println ("entry from 266 adjustbrightness"); 267 Serial.print (value ); 268 if 269 ( value [0] != minus) 270 { 271 what_to_do = HIGH; //raise_up 272 273 do_something = HIGH; 274 Serial.println ("raise_up 275 from brightness"); 276 } 277 if ( value [0] == minus) 278 279 { what_to_do = LOW; //lower 280 do_something = HIGH; 281 Serial.println ("lower from brightness"); 282 } 283 284 } 285 286 } else { 287 traffic_light = LOW; 288 digitalWrite(raise_up, 289 LOW);// stop action 290 digitalWrite(lower, LOW); 291 } 292 293 } 294 } break; 295 case WStype_BIN: 296 { Serial.printf("[WSc] 297 get binary length: %u\ 298", length); 299 } break; 300 } 301 302} 303 304 305 306//**************************************************** 307 308void 309 ActionTask (void * pvParameters) 310 311{ 312 while (1) 313 { 314 if (do_something 315 == HIGH) 316 { traffic_light = HIGH; 317 digitalWrite(LED, HIGH); 318 digitalWrite(status_on, 319 HIGH); 320 { if (what_to_do == HIGH) // raise_up curtain 321 { 322 digitalWrite(red, LOW); 323 digitalWrite(raise_up, HIGH); //funziona 324 con logica inversa 325 actiontime (); 326 digitalWrite(raise_up, 327 LOW); 328 digitalWrite(red, HIGH); 329 } 330 else // 331 lower curtain 332 { 333 if (traffic_light == HIGH) 334 { 335 digitalWrite(green, LOW); 336 digitalWrite(lower, HIGH); //this depend 337 on the relay board switch it could be low or high for operate 338 actiontime 339 (); 340 } 341 digitalWrite(lower, LOW); 342 digitalWrite(green, 343 HIGH); 344 } 345 346 } 347 digitalWrite(LED, LOW); 348 digitalWrite(status_on, 349 LOW); 350 traffic_light = LOW; 351 do_something = LOW; 352 } 353 354 355 356 vTaskDelay(2); 357 } 358} 359 360 361//*********************************************************************************************** 362 363void 364 actiontime () 365{ for (int i = 0; i < action_time; i++) 366 { if (traffic_light 367 == HIGH) 368 vTaskDelay(1000); 369 } 370} 371 372//*********************************************************************************************** 373 374 375 376 377 378[/code]
Alexa controlled curtain
c_cpp
refer to github Sinric for instructions to create the sw environment
1[code] 2#include <Arduino.h> 3 4#include <WiFi.h> 5#include <WiFiMulti.h> 6#include 7 <WebSocketsClient.h> 8#include <ArduinoJson.h> // get it from https://arduinojson.org/ 9 or install via Arduino library manager 10#include <SimpleTimer.h> 11#include <Time.h> 12WiFiMulti 13 WiFiMulti; 14WebSocketsClient webSocket; 15 16//****************************************************************** 17 18#define 19 MyApiKey "xxxxxxxxxxxxxxxxxxxxxxxx" // TODO: Change to your sinric API Key. Your 20 API Key is displayed on sinric.com dashboard 21//#define MySSID "xxxxxxxx" // 22 TODO: Change to your Wifi network SSID 23//#define MyWifiPassword "xxxxxxxx" 24 // TODO: Change to your Wifi network password 25//#define MyDeviceID "xxxxxxxxxxxxxxxxxxxxxxx" 26#define 27 MySSID "xxxxxxxx" 28#define MyWifiPassword "xxxxxx" 29#define LED 2 30#define 31 red 27 32#define green 26 33int connec_ted = 2; 34// assegnazione dei pin 35 esp32 36int status_on = 25; //era 25 37int lower = 32; 38int raise_up = 33; 39int 40 connection = LOW; 41int action_time = 35; //35 times 1 second 42int what_to_do; 43int 44 traffic_light = LOW; 45int do_something = LOW; 46 47 48SimpleTimer timer; 49 50 51 52//*********************************************************************************************** 53 54void 55 setup() { 56 Serial.begin(115200); 57 58 WiFiMulti.addAP(MySSID, MyWifiPassword); 59 60 Serial.println("alexa_curtain 1.1"); 61 Serial.print("Connecting to Wifi: 62 "); 63 Serial.println(MySSID); 64 pinMode(connec_ted, OUTPUT); 65 pinMode(LED, 66 OUTPUT); 67 pinMode(red, OUTPUT); 68 pinMode(green, OUTPUT); 69 pinMode (status_on, 70 OUTPUT); 71 pinMode (raise_up, OUTPUT); 72 pinMode (lower, OUTPUT); 73 digitalWrite(raise_up, 74 LOW); 75 digitalWrite(lower, LOW); 76 digitalWrite(connec_ted, HIGH); 77 digitalWrite(red, 78 HIGH); 79 digitalWrite(green, HIGH); 80 81 do_something = LOW; // action 82 switch on high ther is something to do 83 84 xTaskCreatePinnedToCore(ActionTask, 85 "ActionTask ", 4000, NULL, 5, NULL, 0); //Task for Core 1 priority set at 5 86 87 // Waiting for Wifi connect 88 while (WiFiMulti.run() != WL_CONNECTED) { 89 90 delay(500); 91 if (connection == LOW) 92 { 93 digitalWrite(connec_ted, 94 HIGH); 95 digitalWrite(status_on, LOW); 96 connection = HIGH; 97 } 98 else { 99 digitalWrite(connec_ted, LOW); 100 digitalWrite(status_on, 101 HIGH); 102 connection = LOW; 103 } 104 Serial.print("."); 105 } 106 107 if (WiFiMulti.run() == WL_CONNECTED) { 108 Serial.println(""); 109 Serial.print("WiFi 110 connected. "); 111 Serial.print("IP address: "); 112 Serial.println(WiFi.localIP()); 113 114 digitalWrite(connec_ted, HIGH); 115 digitalWrite(status_on, LOW); 116 } 117 118 119 // server address, port and URL 120 webSocket.begin("iot.sinric.com", 80, "/"); 121 122 123 // event handler 124 webSocket.onEvent(webSocketEvent); 125 webSocket.setAuthorization("apikey", 126 MyApiKey); 127} 128//*********************************************************************************************** 129 130void 131 loop() { 132 webSocket.loop(); 133 delay(1); 134 135} 136 137//*********************************************************************************************** 138 139 140void 141 turnOn(String MyDeviceID) { 142 if (MyDeviceID == "mysinric deviceid") // Device 143 ID of first device 144 { 145 Serial.print("Turn ON first device with ID: "); 146 147 Serial.println(MyDeviceID); 148 } 149 150} 151 152//*********************************************************************************************** 153 154void 155 turnOff(String MyDeviceID) { 156 if (MyDeviceID == "mysinric deviceid") // Device 157 ID of device 158 { 159 Serial.print("Turn OFF first device with ID: "); 160 161 Serial.println(MyDeviceID); 162 } 163} 164 165 166//*********************************************************************************************** 167 168char 169 minus = '-'; 170 171int n; 172 173void webSocketEvent(WStype_t type, uint8_t * payload, 174 size_t length) 175 176{ 177 switch (type) { 178 case WStype_DISCONNECTED: 179 180 { Serial.printf("[WSc] Webservice DISCONNECTED from sinric.com!\ 181"); 182 183 } break; 184 case WStype_CONNECTED: { 185 Serial.printf("[WSc] 186 Service connected to sinric.com at url: %s\ 187", payload); 188 Serial.printf("Waiting 189 for commands from sinric.com ...\ 190"); 191 } 192 break; 193 case WStype_TEXT: 194 { 195 Serial.printf("[WSc] get text: %s\ 196", payload); 197 // Example 198 payload for ON : 199 //{"deviceId":"5a2b908a74a6703928abf38b","powerState":"ON"} 200 201 DynamicJsonBuffer jsonBuffer (4000); 202 JsonObject& json = jsonBuffer.parseObject((char*)payload); 203 204 String deviceId = json ["deviceId"]; 205 String action = json ["action"]; 206 207 String value = json ["value"]; 208 209 if (deviceId == "mysinric 210 deviceid") // Device ID of first device 211 { 212 if (traffic_light 213 == LOW) 214 { if ( action == "setPowerState") 215 { Serial.println 216 ("entry from power"); 217 if (value == "ON") 218 { 219 what_to_do = HIGH; //raise_up 220 do_something = HIGH; 221 } 222 223 if (value == "OFF") 224 { what_to_do = LOW; //lower 225 226 do_something = HIGH; 227 } 228 229 } if 230 (action == "SetBrightness" ) 231 232 { Serial.println ("entry from 233 brightness"); // if below 50% lower otherwise raise 234 Serial.print 235 (value ); 236 int n = (value [0] - 48) * 10; 237 n = n 238 + (value [1] - 48); 239 Serial.println (n ); 240 Serial.print 241 (" value 1 "); 242 Serial.println (value [1] ); 243 if 244 (n > 50 ) 245 { 246 what_to_do = HIGH; //raise_up 247 248 do_something = HIGH; 249 Serial.println ("raise_up 250 from brightness"); 251 } 252 if (n <= 50 ) 253 { 254 what_to_do = LOW; //lower 255 do_something = HIGH; Serial.println 256 ("lower from brightness"); 257 } 258 259 } 260 261 if 262 ( action == "AdjustBrightness") 263 264 { Serial.println ("entry from 265 adjustbrightness"); 266 Serial.print (value ); 267 if 268 ( value [0] != minus) 269 { 270 what_to_do = HIGH; //raise_up 271 272 do_something = HIGH; 273 Serial.println ("raise_up 274 from brightness"); 275 } 276 if ( value [0] == minus) 277 278 { what_to_do = LOW; //lower 279 do_something = HIGH; 280 Serial.println ("lower from brightness"); 281 } 282 283 } 284 285 } else { 286 traffic_light = LOW; 287 digitalWrite(raise_up, 288 LOW);// stop action 289 digitalWrite(lower, LOW); 290 } 291 292 } 293 } break; 294 case WStype_BIN: 295 { Serial.printf("[WSc] 296 get binary length: %u\ 297", length); 298 } break; 299 } 300 301} 302 303 304 305//**************************************************** 306 307void 308 ActionTask (void * pvParameters) 309 310{ 311 while (1) 312 { 313 if (do_something 314 == HIGH) 315 { traffic_light = HIGH; 316 digitalWrite(LED, HIGH); 317 digitalWrite(status_on, 318 HIGH); 319 { if (what_to_do == HIGH) // raise_up curtain 320 { 321 digitalWrite(red, LOW); 322 digitalWrite(raise_up, HIGH); //funziona 323 con logica inversa 324 actiontime (); 325 digitalWrite(raise_up, 326 LOW); 327 digitalWrite(red, HIGH); 328 } 329 else // 330 lower curtain 331 { 332 if (traffic_light == HIGH) 333 { 334 digitalWrite(green, LOW); 335 digitalWrite(lower, HIGH); //this depend 336 on the relay board switch it could be low or high for operate 337 actiontime 338 (); 339 } 340 digitalWrite(lower, LOW); 341 digitalWrite(green, 342 HIGH); 343 } 344 345 } 346 digitalWrite(LED, LOW); 347 digitalWrite(status_on, 348 LOW); 349 traffic_light = LOW; 350 do_something = LOW; 351 } 352 353 354 355 vTaskDelay(2); 356 } 357} 358 359 360//*********************************************************************************************** 361 362void 363 actiontime () 364{ for (int i = 0; i < action_time; i++) 365 { if (traffic_light 366 == HIGH) 367 vTaskDelay(1000); 368 } 369} 370 371//*********************************************************************************************** 372 373 374 375 376 377[/code]
Downloadable files
courtain_quZZxX8Yex.png
courtain_quZZxX8Yex.png

courtain_quZZxX8Yex.png
courtain_quZZxX8Yex.png

Comments
Only logged in users can leave comments