User Tools

Site Tools


electronics_at_component_level

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
electronics_at_component_level [2018/09/15 08:47]
c0rn3j [ESP8266]
electronics_at_component_level [2019/08/27 11:31] (current)
c0rn3j [LED strips]
Line 10: Line 10:
 Do not leave the iron on when you are not using it. Do not leave the iron on when you are not using it.
  
-Clean the tip by applying solder, wiping it clean on a sponge or steel wool and then coat it with a thin layer of solder to prevent oxidation before you turn it off and leave it.+Clean the tip by applying solder, wiping it clean on a sponge or steel wool and then coat it with a thin layer of solder to prevent oxidation before you turn it off and leave it, or coat it right after turning it off.
  
 If you use a sponge, make sure it's damp but not soaked. If you use a sponge, make sure it's damp but not soaked.
Line 24: Line 24:
 ## LED strips ## LED strips
  
-LED strips from "worst" to "best". Price is according to that.+LED strips from "best" to "worst". Price is according to that.
  
-WS2812B ​> APA102C > APA102+SK9822 =~ APA102 ​> APA102C > WS2812B
  
 ## Capacitors ## Capacitors
Line 82: Line 82:
 You can use the number notation (GPIO10 is defined as '​10'​) or the pin notation, like D0. You can use the number notation (GPIO10 is defined as '​10'​) or the pin notation, like D0.
  
 +<code C++>
   void setup() {   void setup() {
     pinMode(D0, OUTPUT);     pinMode(D0, OUTPUT);
Line 91: Line 92:
     delay(1000); ​           // wait for 1 second.     delay(1000); ​           // wait for 1 second.
   }   }
 +</​code>​
  
 Upload it to the board, it should boot up and the blue LED should start blinking. Upload it to the board, it should boot up and the blue LED should start blinking.
Line 102: Line 104:
 Re-connect the resistor from the 3v3 pin to the SD3 one (GPIO10) and replace the code with this: Re-connect the resistor from the 3v3 pin to the SD3 one (GPIO10) and replace the code with this:
  
 +<code C++>
   const short int LED1 = 10;    const short int LED1 = 10; 
   void setup() {   void setup() {
Line 116: Line 119:
     delay(1000); ​           // wait for 1 second.     delay(1000); ​           // wait for 1 second.
   }   }
 +</​code>​
   ​   ​
 Now it should blink! And since we added serial connection and some debugging output, we should be able to connect to it via some software that can read serial, for example '​screen'​. Note that 115200 is the baud rate, affecting how fast you can send/​receive data. Now it should blink! And since we added serial connection and some debugging output, we should be able to connect to it via some software that can read serial, for example '​screen'​. Note that 115200 is the baud rate, affecting how fast you can send/​receive data.
Line 125: Line 129:
 Now let's make the LED Wi-Fi controllable! Same physical setup, different code. Just change the SSID and password in the example: Now let's make the LED Wi-Fi controllable! Same physical setup, different code. Just change the SSID and password in the example:
  
-<​code>​+<​code ​C++>
 #include <​ESP8266WiFi.h>​ #include <​ESP8266WiFi.h>​
  
-const short int LED1 = 10; +const short int LED1 = 10;
  
-const char* ssid = "mywifiname"; +const char* ssid = "ssid"; 
-const char* password = "securepassword";+const char* password = "password";
 WiFiServer server(80); WiFiServer server(80);
  
 void setup() { void setup() {
-  ​Serial.begin(115200); ​       // connect to the serial port + Serial.begin(115200);​ 
-  pinMode(LED1,​ OUTPUT); + pinMode(LED1,​ OUTPUT); 
-  // WiFi.mode(m):​ set mode to WIFI_AP, WIFI_STA, or WIFI_AP_STA. + // WiFi.mode(m):​ set mode to WIFI_AP, WIFI_STA, or WIFI_AP_STA. 
-  WiFi.mode(WIFI_STA);​ + WiFi.mode(WIFI_STA);​ 
-  delay(10);​ + delay(10);​ 
-  WiFi.begin(ssid,​ password);+ WiFi.begin(ssid,​ password);
  
-  ​while (WiFi.status() != WL_CONNECTED) { + while (WiFi.status() != WL_CONNECTED) { 
-    Serial.println("​Wi-Fi not connected, retrying... "); + Serial.println("​Wi-Fi not connected, retrying... "); 
-    delay(500); // Do not use this delay in SoftAP mode + delay(500); // Do not use this delay in SoftAP mode 
-  +
-  // Start the server + // Start the wifi server 
-  server.begin();​+ server.begin();​
  
-  ​Serial.println("​IP address: "); + Serial.println("​IP address: "); 
-  Serial.println(WiFi.localIP()); ​+ Serial.println(WiFi.localIP());​
 } }
  
 void loop() { void loop() {
-  ​if(WiFi.status() != WL_CONNECTED) { + if(WiFi.status() != WL_CONNECTED) { 
-    Serial.println("​Wi-Fi not connected, retrying... "); + Serial.println("​Wi-Fi not connected, retrying... "); 
-    delay(500); // Do not use this delay in SoftAP mode + delay(500); // Do not use this delay in SoftAP mode 
-  +
-  WiFiClient client = server.available();​ + WiFiClient client = server.available();​ 
-  if (!client) { + if (!client) { 
-//    Serial.println("​No client connected, suiciding."​);​ + //​Serial.println("​No client connected, suiciding."​);​ 
-    return; + return; 
-  } + }
- +
-  int insanity = 0; +
-  // Wait until the client sends some data +
-  while (!client.available()) { +
-    insanity++;​ +
-    if (insanity == 1000) +
-    { +
-      Serial.println("​And nobody came..."​);​ +
-      return; +
-    } +
-    delay(1); +
-    Serial.println("​Waiting for client to send data."​);​ +
-//    client = server.available();​ // Check if the connection didn't fuck up, if yes, kill it. +
-//    if (!client) { +
-//    return; +
-//    } +
-  } +
-  Serial.println("​Client available, receiving data..."​);  ​+
  
-  ​// Read the first line of the request + int insanity = 0; 
-  ​String request ​= client.readStringUntil('​\r'​); + // Wait until the client sends some data 
-  client.flush();+ while (!client.available()) { 
 + insanity++;​ 
 + if (insanity ​== 1000) { 
 + Serial.println("​And nobody came..."​);​ 
 + return;​ 
 +
 + delay(1);​ 
 + Serial.println("​Waiting for client ​to send data."); 
 + //client = server.available(); // Check if the connection didn't fuck up, if yes, kill it. 
 + //if (!client) { 
 + //​return;​ 
 + //} 
 +
 + Serial.println("​Client available, receiving data..."​);
  
-  ​// Match the request + // Read the first line of the request 
-  if (request.indexOf("/​OFF"​) != -1)  + String ​request ​= client.readStringUntil('​\r'​); 
-    digitalWrite(LED1,​ LOW); + client.flush();
-  if (request.indexOf("/​ON"​) != -1) +
-    digitalWrite(LED1, HIGH);+
  
-  ​// Return the response + // Match the request 
-  String html = String("​HTTP/​1.1 200 OK\r\n"​) ++ if (request.indexOf("/​OFF"​) != -1) { 
 + digitalWrite(LED1,​ LOW); 
 +
 + if (request.indexOf("/​ON"​) != -1) { 
 + digitalWrite(LED1,​ HIGH); 
 +
 + // Return the response 
 + String html = String("​HTTP/​1.1 200 OK\r\n"​) +
                 "​Content-Type:​ text/​html\r\n"​ +                 "​Content-Type:​ text/​html\r\n"​ +
                 "​\r\n"​ +                 "​\r\n"​ +
Line 228: Line 232:
                 "</​body>"​ +                 "</​body>"​ +
                 "</​html>";​                 "</​html>";​
-  ​client.print(html);​ + client.print(html);​ 
-  delay(1);+ delay(1);
 } }
 </​code>​ </​code>​
  
-Get the IP either from serial or your DHCP server list. Open it in your browser ​as http:// ​and you should see two buttons that turn the LED off and on.+Get the IP either from serial or your DHCP server list. Open it in your browser and you should see two buttons that turn the LED off and on. 
  
 ### IR ### IR
  
-Now that you know enough to get going, time to dive into something fun, IR. This assumes ​+Now that you know enough to get going, time to dive into something fun, IR. Let's make a thing that can both receive and send. 
 + 
 +You'll need a 2-pin IR LED, 3-pin 1838 IR receiver, 2N2222 NPN transistor, 100 Ohm resistor and some wires. 
 + 
 +{{::​ir_rec_send.png?​800|}} 
 + 
 +You will need to setup the IR library for ESP8266 - https://​github.com/​markszabo/​IRremoteESP8266 
 + 
 +Keep in mind that at least on the NodeMCU board linked in the doc with stuff to buy you can't use GPIO 1,​3,​9,​10,​15,​16 for IR receive(and possibly send), the limitations are mostly explained [here](https://​tttapa.github.io/​ESP8266/​Chap04%20-%20Microcontroller.html). 
 + 
 +I used this code to test which GPIOs I can use for receiving:​ 
 + 
 +<code C++> 
 +#include <​ESP8266WiFi.h>​ 
 + 
 +#include <​IRremoteESP8266.h>​ 
 +#include <​IRrecv.h>​ 
 +#include <​IRsend.h>​ 
 +#include <​IRtimer.h>​ 
 +#include <​IRutils.h>​ 
 +#include <​ir_Argo.h>​ 
 +#include <​ir_Daikin.h>​ 
 +#include <​ir_Fujitsu.h>​ 
 +#include <​ir_Kelvinator.h>​ 
 +#include <​ir_LG.h>​ 
 +#include <​ir_Magiquest.h>​ 
 +#include <​ir_Midea.h>​ 
 +#include <​ir_Mitsubishi.h>​ 
 +#include <​ir_Toshiba.h>​ 
 +#include <​ir_Trotec.h>​ 
 + 
 +const char* ssid = "​ssid";​ 
 +const char* password = "​password";​ 
 +WiFiServer server(80);​ 
 + 
 +int RECV_PIN = 0; //IR IN 
 +IRrecv irrecv(RECV_PIN);​ 
 +decode_results results; 
 + 
 +void setup() { 
 + Serial.begin(115200);​ 
 + irrecv.enableIRIn();​ 
 + 
 + // WiFi.mode(m):​ set mode to WIFI_AP, WIFI_STA, or WIFI_AP_STA. 
 + WiFi.mode(WIFI_STA);​ 
 + delay(10);​ 
 + WiFi.begin(ssid,​ password);​ 
 + 
 + while (WiFi.status() != WL_CONNECTED) { 
 + Serial.println("​Wi-Fi not connected, retrying... "); 
 + delay(500);​ // Do not use this delay in SoftAP mode 
 +
 + // Start the wifi server 
 + server.begin();​ 
 + Serial.println("​IP address: "); 
 + Serial.println(WiFi.localIP());​ 
 + 
 + Serial.print("​RECV_PIN now: "); 
 + Serial.println(RECV_PIN);​ 
 +
 +void loop() { 
 + if (irrecv.decode(&​results)) { 
 + Serial.println((long int)results.value,​ HEX); 
 + irrecv.resume();​ // Receive the next value 
 + return; // Kill the cycle 
 +
 + if(WiFi.status() != WL_CONNECTED) { 
 + Serial.println("​Wi-Fi not connected, retrying... "); 
 + delay(500);​ // Do not use this delay in SoftAP mode 
 +
 + WiFiClient client = server.available();​ 
 + if (!client) { 
 +//​ Serial.println("​No client connected, suiciding."​);​ 
 + return; 
 +
 + 
 + int insanity = 0; 
 + // Wait until the client sends some data 
 + while (!client.available()) { 
 + insanity++;​ 
 + if (insanity == 1000) { 
 + Serial.println("​And nobody came..."​);​ 
 + return;​ 
 +
 + delay(1);​ 
 + Serial.println("​Waiting for client to send data."​);​ 
 +
 + Serial.println("​Client available, receiving data..."​);​ 
 + 
 + // Read the first line of the request 
 + String request = client.readStringUntil('​\r'​);​ 
 + client.flush();​ 
 + 
 + // Match the request 
 + if (request.indexOf("/​NEXT_PIN"​) != -1) { 
 + nextPin();​ 
 +
 + // Return the response 
 + String html = String("​HTTP/​1.1 200 OK\r\n"​) + 
 +                "​Content-Type:​ text/​html\r\n"​ + 
 +                "​\r\n"​ + 
 +                "<​!DOCTYPE HTML>"​ + 
 +                "<​html>"​ + 
 +                "<​head>"​ + 
 +                "<​style media=\"​screen\"​ type=\"​text/​css\">"​ + 
 +                " ​  ​.button {" + 
 +                " ​       background-color:​ #​000000;"​ + 
 +                " ​       color: #​FFFFFF;"​ + 
 +                " ​       padding: 10px;" + 
 +                " ​       border-radius:​ 10px;" + 
 +                " ​       -moz-border-radius:​ 10px;" + 
 +                " ​       -webkit-border-radius:​ 10px;" + 
 +                " ​       margin:​10px"​ + 
 +                " ​   }" 
 +                " ​   .small-btn {" + 
 +                " ​       width: 50px;" + 
 +                " ​       height: 25px;" + 
 +                " ​   }" + 
 +                " ​   .medium-btn {" + 
 +                " ​       width: 70px;" + 
 +                " ​       height: 30px;" + 
 +                " ​   }" + 
 +                " ​   .big-btn {" + 
 +                " ​       width: 90px;" + 
 +                " ​       height: 40px;" + 
 +                " ​   }" + 
 +                "</​style>"​ + 
 +                "</​head>"​ + 
 +                "<​body>"​ + 
 +                "<a href=\"/​NEXT_PIN\"><​div class=\"​button big-btn\">​NEXT_PIN</​div></​a>"​ + 
 +                "</​body>"​ + 
 +                "</​html>";​ 
 + client.print(html);​ 
 + delay(1);​ 
 +
 + 
 +void nextPin() { 
 + irrecv.disableIRIn();​ 
 + delay(100);​ 
 + RECV_PIN = RECV_PIN+1; //IR IN 
 + Serial.print("​RECV_PIN now: "); 
 + Serial.println(RECV_PIN);​ 
 + IRrecv irrecv(RECV_PIN);​ 
 + decode_results results; 
 + irrecv.enableIRIn();​ // Start the receiver 
 + delay(100);​ 
 +
 +</​code>​
electronics_at_component_level.1537001223.txt.gz · Last modified: 2018/09/15 08:47 by c0rn3j