# Voice control lamp with Rgb led & LDR led effects Joshua Villamater - md223aj **Estimated complete time:** 2 - 4 Hours ![](https://i.imgur.com/yDy9Xgb.jpg) ![](https://i.imgur.com/g4oN8UH.jpg) The project is divided in different parts in which the first part will use an AI service controlling a lamp & an app controlling a RGB led. Another where it is quite different from the previous one in which it will be using a few leds to light up with different patterns & transfering brightness data using an IoT platform. **OBJECTIVE** The purpose of this project is to be able to do an IoT project at low cost, Easy to build & use but with complex features. Also learn to build your own lamp. This will give some insights of what an IoT based product can do & what can you make when artificial intelligens is applied to it in a simple way. Another option if AI isn't right for you. This also solves the issue at home going everywhere just to switch off a bunch of lights. # Voice control lamp with RGB led **MATERIALS & cost (swedish kr)** All the electronic components are from electrokit & kjellocompany. Parts used for the lamp came from different local hardware stores. Some links are provided that sends you directly to the order page of each parts on the list. However those that lack some links are been purschase without any online store link to it. Every parts price below are counted in total cost. - Nodemcu esp8266 V3, 79 kr [buy here](https://www.electrokit.com/produkt/nodemcu-v3/) - 4-pin RGB led common anode, 9 kr [buy here](https://www.electrokit.com/produkt/led-rgb-5mm-diffus-gemensam-anod-arbg/) - 3-pin Female to female jumper wire, 7.5 kr [buy here](https://www.electrokit.com/produkt/labbsladd-3-pin-200mm/) - 8x Male to male jumper wires, 29 kr [buy here](https://www.electrokit.com/produkt/labbsladd-20-pin-15cm-hane-hane/) - x3 1k ohm resistors, 3 kr [buy here](https://www.electrokit.com/produkt/motstand-metallfilm-0-6w-1-1kohm-1k/) - x2 Breadboard with 400 connections holes, 118 kr [buy here](https://www.electrokit.com/produkt/kopplingsdack-400-anslutningar/) - 5V Relay module 250VAC / 10A, 39 kr [buy here](https://www.electrokit.com/produkt/relamodul-5v/) - Electrical tape, 39 kr [buy here](https://www.kjell.com/se/produkter/hem-kontor-fritid/kontorstillbehor/tejp/eltejp-19-mm-x-20-m-svart-p40104) - Lightbulb socket e14, 19 kr (bought locally) - E14 lightbulb, 10 kr (bought locally) - Electric Plug, 15 kr (bought locally) - Usb charger & cable, cost nothing if you already have one - Coppar cable about 2 meters or more (highly recommend up to 4 meters in length), 10 kr/meter (2 m in this tutorial) "bought locally" Alternative purchase an arduino kit with some of the parts needed above. [Link here](https://www.kjell.com/se/produkter/el-verktyg/utvecklingskit/arduino/arduino-kit/luxorparts-basic-start-kit-for-arduino-p90632?gclid=Cj0KCQjwrfvsBRD7ARIsAKuDvMPTQaFCX7zBBn7EUQaw9e93zlGNaENAzLCPfasheCK99z_TnMTxxd0aAkLZEALw_wcB&gclsrc=aw.ds) **COMPUTER SETUP** Step 1: Download arduino IDE software @ https://www.arduino.cc/ Step 2: Install arduino IDE in your computer & Open Arduino IDE Step 3: Go to Tools --> "Manage Libraries" & then search for blynk & install the latest version ![](https://i.imgur.com/qf76KUD.png) Step 4: Go to File --> Preferences at "Arduino Boards Manager URLs" add the link below & click ok: http://arduino.esp8266.com/stable/package_esp8266com_index.json Step 5: Close Arduino IDE & re-open Step 6: Go to Tools --> Board & choose Nodemcu 1.0 Step 7: Go to Tools --> Port & choose the one where the Nodemcu is connected Check carefully that everything looks exactly like this: ![](https://i.imgur.com/P4WUs1v.png) Step 7: Download Blynk to IOS or Android & follow these steps @ https://blynk.io/en/getting-started Step 8: Create an account then follow the steps to create a dashboard @ https://io.adafruit.com/ Step 9: Create an account & follow the steps @ https://ifttt.com/. in this case choose google assistant & adafruit as services for the applets Step 10: After downloadning blynk app to your smartphone, created an account at adafruit.io & ifttt.com. Go open a new sketch in arduino IDE Copy & paste the code below: ``` //Adafruit #include <ESP8266WiFi.h> #include <Adafruit_MQTT.h> #include <Adafruit_MQTT_Client.h> //Adafruit io inställningar #define AIO_SERVER "io.adafruit.com" #define AIO_SERVERPORT 1883 #define AIO_USERNAME "AIO_USERNAME" #define AIO_KEY " AIO_KEY " //Glödlampa pin #define BIG_LED D0 //Blynk #include <BlynkSimpleEsp8266.h> char auth[] = "Blynk token"; char ssid[] = "wifi"; char pass[] = "password"; // Blynk end ---> //Adafruit io delar WiFiClient client; Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_USERNAME, AIO_KEY); //Adafruit feeds Adafruit_MQTT_Subscribe light = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/lamp"); void MQTT_connect(); void setup() { //Blynk anslutning Blynk.begin(auth, ssid, pass); //Adafruit pinMode(BIG_LED, OUTPUT); Serial.begin(115200); delay(100); Serial.println(F("Adafruit MQTT demo")); //Anslutningsinfo Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println("wifi name"); //Nätverksanslutning WiFi.begin("wifi", "password"); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(); Serial.println("Wifi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); //Lampan mqtt.subscribe(&light); } uint32_t x=0; void loop() { //Blynk app Blynk.run(); MQTT_connect(); //Lamp switch adafruit.io Adafruit_MQTT_Subscribe *subscription; while ((subscription = mqtt.readSubscription(5000))){ if (subscription == &light){ Serial.print(F("On-off button: ")); Serial.println((char * )light.lastread); delay(500); if (strcmp((char *)light.lastread,"Disable") == 0){ digitalWrite(BIG_LED,LOW); } else if(strcmp((char *)light.lastread,"Activate") == 0){ digitalWrite(BIG_LED,HIGH); } } }//while //MQTT ej ansluten if(! mqtt.ping()){ mqtt.disconnect(); } }//loop() //MQTT anslutning void MQTT_connect(){ int8_t ret; if(mqtt.connected()){ return; } Serial.print("Connecting to MQTT..."); //Reset uint8_t retries = 3; while ((ret = mqtt.connect()) != 0){ Serial.println(mqtt.connectErrorString(ret)); Serial.println("Retrying MQTT connection in 5 seconds..."); mqtt.disconnect(); delay(5000); retries--; if(retries == 0){ } } }//Void() ``` Step 11: Save & upload to nodeMCU **Putting everything together:** - Begin with cutting open the cable to free up some centimeters of coppar wires. Later on remove some of the wires covering the coppar threads. Then twist the threads to form some kind of rope formation ![](https://i.imgur.com/ybiCcq2.jpg) - Make sure to check the markings Where L = Load & N = Neutral. Insert the one wire in each mounting hole (The gold colored ones) Remember which wire is connected to neutral & which one is connected to load ![](https://i.imgur.com/J66SzAh.jpg) - Begin cutting up the other end of the cable & do exactly as last time but. The wires should be a bit longer this time. ![](https://i.imgur.com/3SeU2Cj.jpg) - Open the Electrical plug like this ![](https://i.imgur.com/MaxdU4d.jpg) - If everything is put in the right way it should like this (Neutral wire from the left side of the plug) ![](https://i.imgur.com/wBZ8QM2.jpg) - Put all the left out parts together & you should get something like this ![](https://i.imgur.com/9js036V.jpg) - Test the lamp first before doing any modifications to it. We are dealing with 240 Volts rated as high voltage. Be extremely caution - If it works fine then add the relay module. Choose a length of the cable cut open the side where the load wire is located cut it in the middle strip out the copper threads twist them to form a rope shape and add it to the lamp like this ![](https://i.imgur.com/CbHZc9L.jpg) - the load wire from the plug is connected to common terminal & the other one is connected to the normally open (NO) which means the terminal will let voltage flow when signal (S) is high. The empty terminal is for normally close in this case will prevent voltage flow if signal (S) is high. Meaning lamp will switch off if the relay module is on - On the relay module other side there three pins which are mark (+) anode (-) cathode & (s) for signal - Pin connections between the nodeMcu & RGB led. Resistors will be use on the cathodes ![](https://i.imgur.com/nE37Hiu.png) - It will look like this if the connections are correct ![](https://i.imgur.com/MqLxgAZ.jpg) - Use the 3-pin Female to female jumper wire & connect one end to the relay module. Use the other end to plug in some jumper wires put electrical tape to secure the jumpers wire on the 3 pin female cable. Then connect the loose pins to the breadboard ![](https://i.imgur.com/7CdiaWa.png) When everything is in place ![](https://i.imgur.com/yDy9Xgb.jpg) It's all done have fun! # LDR led effects **MATERIALS & cost (Euro & swedish kr)** The pycom hardwares are usually purchase online overseas but became possible to purchase locally thanks to a deal between LnU & electrokit. the other stuff that are used in this project came from electrokit as well & some from kjellocompany. Every parts price below are counted in total cost. - Lopy4, €34.95 [buy here](https://pycom.io/product/lopy4/?gclid=CjwKCAjwjLD4BRAiEiwAg5NBFigySgo44PFhrTNPM2xYIWLywh996mRIw4zuD-mTZavUBehzoQA5oxoC--UQAvD_BwE) - Expansionsboard 3.1, €16 [buy here](https://pycom.io/product/expansion-board-3-0/) - Breadboard with 400 connection holes, 59 kr [buy here](https://www.electrokit.com/produkt/kopplingsdack-400-anslutningar/) - Light depended resitor, 8 kr [buy here](https://www.electrokit.com/produkt/fotomotstand-cds-2-5-kohm/) - 6x 1k ohm resistors, 6 kr [buy here](https://www.electrokit.com/produkt/motstand-metallfilm-0-6w-1-1kohm-1k/) - 5x leds, 8.5 kr [buy here](https://www.electrokit.com/produkt/led-rod-5mm-standard-diffus/) - 9x Male to male jumper wires, 29 kr [buy here](https://www.electrokit.com/produkt/labbsladd-20-pin-15cm-hane-hane/) Arduino kit from kjellocompany as alternative [link here](https://www.kjell.com/se/produkter/el-verktyg/utvecklingskit/arduino/arduino-kit/luxorparts-basic-start-kit-for-arduino-p90632?gclid=Cj0KCQjwrfvsBRD7ARIsAKuDvMPTQaFCX7zBBn7EUQaw9e93zlGNaENAzLCPfasheCK99z_TnMTxxd0aAkLZEALw_wcB&gclsrc=aw.ds) The pycom devices cost instead 879 kr as pair which included an antenna if needed. Which is compatible to use with lopy4's built-in network modules. **COMPUTER SETUP** Step 1: Register an account at https://sso.pycom.io/ Step 2: Add device --> Add device via usb ![](https://i.imgur.com/NS9LmG8.png) Step 3: Choose lopy4 ![](https://i.imgur.com/r7kbxrz.png) Step 5: Choose a network in this tutorial ![](https://i.imgur.com/L5alhUa.png) Step 6: Configure wifi connection. if no wifi exist add one in the settings below ![](https://i.imgur.com/ENg9WdV.png) Step 7: Do the following steps ![](https://i.imgur.com/wmMVGZD.png) ![](https://i.imgur.com/GUJzRlL.png) [](https://i.imgur.com/a0wWxs1.png) ![](https://i.imgur.com/a0wWxs1.png) **Putting everything together** - Just like the picture further below its lined up as led01 from the left ![](https://i.imgur.com/WvUAYM4.png) ![](https://i.imgur.com/g4oN8UH.jpg) ![](https://i.imgur.com/b9uz5SJ.jpg) ![](https://i.imgur.com/YPpWdEa.jpg) You might have notice that the device is sitting on top of a piece of tissue. Its just to prevent the underneat of the device from scratching the table. - After setting everything up from above upload the code using atom software & see data running: ``` from machine import ADC from machine import Pin import time import _thread import _thread LED01 = Pin("P12", mode=Pin.OUT) LED02 = Pin("P11", mode=Pin.OUT) LED03 = Pin("P7", mode=Pin.OUT) LED04 = Pin("P5", mode=Pin.OUT) LED05 = Pin("P4", mode=Pin.OUT) Sensor = Pin("P17", mode=Pin.IN) adc = ADC(bits=10) # create an ADC object bits=10 means range 0-1024 the lower value the less light detected apin = adc.channel(attn=ADC.ATTN_11DB, pin=Sensor) # create an analog pin on P17; attn=ADC.ATTN_11DB measures voltage from 0.1 to 3.3v while True: bright = apin() # read an analog value # Data visiualisering med wifi pybytes.send_signal(1, bright) print("Brightness is: ", bright,"lumens") time.sleep(0.05) # wait 1 sec # LED Mönster if bright > 700: # Mönster 1 # Vänster ---> LED01.value(1) # LED01 time.sleep(0.1) LED01.value(0) time.sleep(0.1) LED02.value(1) # lED02 time.sleep(0.1) LED02.value(0) time.sleep(0.1) LED03.value(1) # LED03 time.sleep(0.1) LED03.value(0) time.sleep(0.1) LED04.value(1) # LED04 time.sleep(0.1) LED04.value(0) time.sleep(0.1) LED05.value(1) # LED05 time.sleep(0.1) # <--- Höger LED05.value(1) # LED05 time.sleep(0.1) LED05.value(0) time.sleep(0.1) LED04.value(1) # lED04 time.sleep(0.1) LED04.value(0) time.sleep(0.1) LED03.value(1) # LED03 time.sleep(0.1) LED03.value(0) time.sleep(0.1) LED02.value(1) # LED02 time.sleep(0.1) LED02.value(0) time.sleep(0.1) LED01.value(1) # LED01 time.sleep(0.05) elif bright > 500 and bright <= 700: # Mönster 2 LED01.value(0) # LED02 - LED04 LED02.value(1) LED03.value(1) LED04.value(1) LED05.value(0) time.sleep(1) LED01.value(1) # LED01 & LED05 LED02.value(0) LED03.value(0) LED04.value(0) LED05.value(1) time.sleep(1) elif bright > 300 and bright <= 500: # Mönster 3 LED01.value(1) # LED01 & LED05 LED05.value(1) time.sleep(1) LED01.value(0) LED05.value(0) LED02.value(1) # LED02 & LED04 LED04.value(1) time.sleep(1) LED02.value(0) LED04.value(0) LED03.value(1) # lED03 time.sleep(1) LED03.value(0) else: # Mönster 4 LED04.value(1) # LED04 & LED05 LED05.value(1) time.sleep(1) LED04.value(0) LED05.value(0) LED01.value(1) # LED01 & LED02 LED02.value(1) time.sleep(1) LED01.value(0) LED02.value(0) time.sleep(1) LED02.value(1) # LED01, LED02 & LED03 LED03.value(1) LED04.value(1) time.sleep(1) LED02.value(0) LED03.value(0) LED04.value(0) time.sleep(1) _thread.start_new_thread(bright) # Startar en thread ``` LDR sensing lumens data: ![](https://i.imgur.com/pUf99xk.png) - In https://pybytes.pycom.io/ create a new project & do the following steps to create a dashboard. Now you're done enjoy! ![](https://i.imgur.com/jvSIN5t.png) **Platforms** - Adafruit io - Blynk iot - Google assistant - ifttt - pybytes The reason why these platforms above are chosen for the projects is a matter of fact that they come in handy when it comes to diversity of supported hardware. Another thing is that they came with a user-friendly interface in which is non-time consuming to learn. Very useful in ocassions where time is very limited in other words when things need to be done immediately. Reliablity of there functions? Well some of them well not all of them work pretty well when it comes to stability. Adafruit io, Blynk iot & specially pybytes worked fine. Google assistant works as good like those on the list but certain problem was that it still validates non existing commands from the applets made from ifttt platform. Ifttt comes with it's own problems however. Some applets remain visible after disposal. Navigating throught the site does not work smoothly. when you from tab to tab the webpage just freeze & has to be refresh for almost each clicks for it to respond. It took a bit of time over. But otherwise in the end everything just worked fined. Blynk has also some issues with the responds. Having experience some late reaction when it comes manually controlling the rgb with the app. But in theory it does that normally because of the fact that 2 separate connections are done by the same wifi module which makes the other connection slow. 1 is being use for blynk while the other 1 is being use to communicate with adafruit io from google assistant. **Data transmitting & presentation** Light brighness data is being send throught the dashboard every second. So the leds will led pattern will switch as often as possible. Wifi became a prior choice. It was suppose to be sigfox at the begining but it demand the user a bussiness account & LoRa signals were out of reach. Lopy4 lacks Gsm network therefor wifi was the only solution for transmitting data to a plattform. MQTT was used to send on/off commands for the lamp. While Rgb led is being controlled simplely with wifi connections & the same way is used to send data to pybytes. From an analize of the data being held in database it seems like it only hold up to a 5 minute data. That's because data transmitting happens every second which is 60 data transmits per minute. Pybytes might not have that kind of capacity to hold hundreds or even thousands of data. ![](https://i.imgur.com/G6A6iFv.png) ![](https://i.imgur.com/gKlYV4Y.png) ![](https://i.imgur.com/AND94vX.png) In that case why did pybytes was use in the first place during the project? Well that's because it just came with the hardware & is adapt to work just for it. Unlike other plattforms that will be needing some adjustments to able to work with the pycom device itself & it also provides it's own database. **Database** The database that is provided also by pybytes was chosen in the fact that nothing else was needed to configure. As soon as the dashboard was set up that show data being transferred. Its automatically saved, doing so saved a lot of time. **Opions & thoughts** I'm very pleased of how things went in the end. I really felt I learned new things, found new ways to make stuff like this & will probably continue on doing things like these in the future. I couldn't think of anything else than the resaults were stunning. **Voice control lamp with RGB led video presentation link** https://www.youtube.com/watch?v=OIEXjt1bCwI **LDR light effects video presentation link** https://www.youtube.com/watch?v=dwzUuhws97c