Arduino Keyboard commands using Esp8266 and Blynk

Hey there Blynkers!
here is a video of my latest project using Blynk:
An arduino pro micro connected to an Esp8266 sending Keyboard commands to the computer over Wifi! hope you enjoy it :smile:
Arduino Keyboard commands using Esp8266 and Blynk

6 Likes

This is freaking cool! I was waiting for someone to control computer with Blynk!

Thank you for sharing! May be you can share the link to the code you are using? Someone would be definitely interested in it.

very nice would be interested to see the code also …

I will post the code here in the next couple of days.
Thanks for the support and i’m looking forward to getting my hands on more widgets!

Hey guys, this is the code to the project, just be sure to change the Authentication toke, ssid, password
and to add buttons on your app from pins V0 to V15:

/* this code is for the Esp-201.

connections:
Arduino --------------------------- Esp-201

VCC-----Voltage Regulator-----VCC/3.3V
VCC----Voltage Regulator—CH_PD
GND---------------------------GND
GND---------------------------GPIO15
GPIO10------------------------GPIO2
GPIO7-------------------------GPIO13
GPIO6-------------------------GPIO12
GPIO5-------------------------GPIO14
GPIO2-------------------------GPIO5

*/

#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
int pins ={5,14,12,13};
int decide = 2;
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth = “your authentication code”;
String str=“”;

BLYNK_WRITE(0) // Virtual pin 0
{
if(param.asInt()!=0){
activate(0);
}
}

BLYNK_WRITE(1) // Virtual pin 1
{
if(param.asInt()!=0){
activate(1);
}
}

BLYNK_WRITE(2) // Virtual pin 2
{
if(param.asInt()!=0){
activate(2);
}
}

BLYNK_WRITE(3) // Virtual pin 3
{
if(param.asInt()!=0){
activate(3);
}
}

BLYNK_WRITE(4) // Virtual pin 4
{
if(param.asInt()!=0){
activate(4);
}
}

BLYNK_WRITE(5) // Virtual pin 5
{
if(param.asInt()!=0){
activate(5);
}
}

BLYNK_WRITE(6) // Virtual pin 6
{
if(param.asInt()!=0){
activate(6);
}
}

BLYNK_WRITE(7) // Virtual pin 7
{
if(param.asInt()!=0){
activate(7);
}
}

BLYNK_WRITE(8) // Virtual pin 8
{
if(param.asInt()!=0){
activate(8);
}
}

BLYNK_WRITE(9) // Virtual pin 9
{
if(param.asInt()!=0){
activate(9);
}
}

BLYNK_WRITE(10) // Virtual pin 10
{
if(param.asInt()!=0){
activate(10);
}
}

BLYNK_WRITE(11) // Virtual pin 11
{
if(param.asInt()!=0){
activate(11);
}
}

BLYNK_WRITE(12) // Virtual pin 12
{
if(param.asInt()!=0){
activate(12);
}
}

BLYNK_WRITE(13) // Virtual pin 13
{
if(param.asInt()!=0){
activate(13);
}
}

BLYNK_WRITE(14) // Virtual pin 14
{
if(param.asInt()!=0){
activate(14);
}
}

BLYNK_WRITE(15) // Virtual pin 15
{
if(param.asInt()!=0){
activate(15);
}
}
void setup()
{

pinMode(decide,OUTPUT);
digitalWrite(decide,LOW);
Serial.begin(9600);
Blynk.begin(auth, “your ssid”, “your password”);
}

void loop()
{
Blynk.run();
pinMode(decide,OUTPUT);
digitalWrite(decide,LOW);

}

void activate(int x){ // this function receives a number, turns it into a binary string and lights up the corresponding pins.
str = String(x,BIN);
pinMode(decide,OUTPUT);
digitalWrite(decide,LOW);
for(int i = 0; i <(sizeof(pins)/sizeof(int));i++){ // first turn them all LOW
pinMode(pins[i],OUTPUT);
digitalWrite(pins[i],LOW);
}

for(int i = 0;i<str.length();i++){ // turn HIGH the ones that should be on
  if(str.charAt(str.length()-1-i)=='1')
    digitalWrite(pins[i],HIGH);
}
digitalWrite(decide,HIGH);
delay(10);
digitalWrite(decide,LOW); 

}

and the code for the arduino pro micro:

/* this code is for the Arduino pro micro.

connections:
Arduino                        Esp-201

VCC-----Voltage Regulator-----VCC/3.3V
VCC----Voltage Regulator-----CH_PD
GND---------------------------GND
GND---------------------------GPIO15
GPIO10------------------------GPIO2
GPIO7-------------------------GPIO13
GPIO6-------------------------GPIO12
GPIO5-------------------------GPIO14
GPIO2-------------------------GPIO5

*/







int pins [] ={2,5,6,7};
int decide=10;
String str="";
String text = "";
int x;
void setup() {
   for(int i = 0; i <(sizeof(pins)/sizeof(int));i++){
      pinMode(pins[i],INPUT);
   }
   pinMode(decide,INPUT);
   Serial.begin(9600);
   while(digitalRead(decide)==1){}
}

void loop() {
 
  int x= digitalRead(decide);
  if(x==1){
    for(int i = 0; i<(sizeof(pins)/sizeof(int));i++){
      if(digitalRead(pins[i])==1)
        str="1"+str;
      else
        str="0"+str;
    }
    
  x=((((int)str.charAt(0))-48)*8)+((((int)str.charAt(1))-48)*4)+((((int)str.charAt(2))-48)*2)+((((int)str.charAt(3))-48)*1); //convert the Esp-201 pin values to a binary string
  action(x);
  str="";
  while(digitalRead(decide)==1){}
  
 }
}

void action(int x){
 switch(x){ //this is where you put your different Keyboard functions, correlating to the corresponding button on the Blynk app.
 
   case 0: // Tab key
     Keyboard.write(KEY_TAB);
   break;
   
   case 1: // Up Arrow
     Keyboard.write(KEY_UP_ARROW);
   break;
   
   case 2: // Hello World
     Keyboard.print("HELLO WORLD ");
   break;
   
   case 3: // Select All
     Keyboard.press(KEY_LEFT_CTRL  );
     Keyboard.write('a');
     Keyboard.release(KEY_LEFT_CTRL  );
   break;
   
   case 4: // Left Arrow
     Keyboard.write(KEY_LEFT_ARROW);
   break;
   
   case 5: // Down Arrow
     Keyboard.write(KEY_DOWN_ARROW);
   break;
   
    case 6: // Right Arrow
     Keyboard.write(KEY_RIGHT_ARROW);
    break;
    
    case 7: // Change language 
     Keyboard.press(KEY_LEFT_SHIFT);
     Keyboard.write(KEY_LEFT_ALT);
     Keyboard.release(KEY_LEFT_SHIFT);
    break;
    
    case 8: // Press Enter
     Keyboard.write(KEY_RETURN);
    break;
     
    case 9: // Press Backspace
     Keyboard.write(KEY_BACKSPACE);
    break;
    
    case 10:
   
    break;
    
    case 11:
    
    break;
    
    case 12: // Open notepad
     Keyboard.press(KEY_LEFT_GUI);
     Keyboard.write('r');
     Keyboard.release(KEY_LEFT_GUI);
     delay(100);
     Keyboard.print("notepad");
     Keyboard.write(KEY_RETURN);
    break;
    
    case 13: // Matrix text
     text = "Wake up, Neo...";
     type(text);
     delay(3000);
    for(int i = 0;i < text.length();i++){
     Keyboard.write(KEY_BACKSPACE);
    }
     text = "The Matrix has you...";
     type(text);
     delay(3000);
    for(int i = 0;i < text.length();i++){
     Keyboard.write(KEY_BACKSPACE);
    }
    text = "Follow the white rabbit.";
     type(text);
     delay(3000);
      for(int i = 0;i < text.length();i++){
     Keyboard.write(KEY_BACKSPACE);
    }
    text = "Knock, knock, Neo.";
     type(text);
     delay(3000);
        for(int i = 0;i < text.length();i++){
     Keyboard.write(KEY_BACKSPACE);
    }
    break;
     
    case 14:// Close window
     Keyboard.press(KEY_LEFT_ALT);
     Keyboard.write(KEY_F4);
     Keyboard.release(KEY_LEFT_ALT);
    break;
    
    case 15:

    break;
    
    
  }
  
  
  
}

void type(String str){
  for(int i=0;i<str.length();i++){
    Keyboard.write(str.charAt(i));
    delay(75);
  }
  
}

enjoy.

1 Like

Hi, I am trying to compile your code using the Arduino IDE 1.6.4 and selecting the generic ESP8266 as the target board, but the compiling process fails:

Build options changed, rebuilding all
In file included from /Users/lveronese/Documents/Arduino/libraries/blynk-library-0.2.1/BlynkSimpleEsp8266.h:15:0,
from ESP8266_Standalone.ino:26:
/Users/lveronese/Documents/Arduino/libraries/blynk-library-0.2.1/BlynkApiArduino.h:44:6: warning: always_inline function might not be inlinable [-Wattributes]
void BlynkApi::processCmd(const void* buff, size_t len)
^
In file included from /Users/lveronese/Documents/Arduino/libraries/blynk-library-0.2.1/BlynkSimpleEsp8266.h:16:0,
from ESP8266_Standalone.ino:26:
/Users/lveronese/Documents/Arduino/libraries/blynk-library-0.2.1/Blynk/BlynkProtocol.h:159:6: warning: always_inline function might not be inlinable [-Wattributes]
void BlynkProtocol::processInput(void)
^
In file included from /Users/lveronese/Documents/Arduino/libraries/blynk-library-0.2.1/Blynk/BlynkDebug.h:17:0,
from /Users/lveronese/Documents/Arduino/libraries/blynk-library-0.2.1/utility/BlynkDebug.cpp:10:
/Users/lveronese/Library/Arduino15/packages/esp8266/hardware/esp8266/1.6.4-673-g8cd3697/tools/sdk//include/ets_sys.h:104:5: error: previous declaration of ‘int atoi(const char*)’ with ‘C++’ linkage
int atoi(const char *nptr);
^
In file included from /Users/lveronese/Library/Arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/1.20.0-26-gb404fb9/xtensa-lx106-elf/include/stdint.h:12:0,
from /Users/lveronese/Library/Arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/1.20.0-26-gb404fb9/lib/gcc/xtensa-lx106-elf/4.8.2/include/stdint.h:9,
from /Users/lveronese/Library/Arduino15/packages/esp8266/hardware/esp8266/1.6.4-673-g8cd3697/tools/sdk//include/c_types.h:8,
from /Users/lveronese/Library/Arduino15/packages/esp8266/hardware/esp8266/1.6.4-673-g8cd3697/tools/sdk//include/ets_sys.h:11,
from /Users/lveronese/Documents/Arduino/libraries/blynk-library-0.2.1/Blynk/BlynkDebug.h:17,
from /Users/lveronese/Documents/Arduino/libraries/blynk-library-0.2.1/utility/BlynkDebug.cpp:10:
/Users/lveronese/Library/Arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/1.20.0-26-gb404fb9/xtensa-lx106-elf/include/stdlib.h:70:5: error: conflicts with new declaration with ‘C’ linkage
int _EXFUN(atoi,(const char *__nptr));
^
In file included from /Users/lveronese/Documents/Arduino/libraries/blynk-library-0.2.1/Blynk/BlynkDebug.h:17:0,
from /Users/lveronese/Documents/Arduino/libraries/blynk-library-0.2.1/utility/BlynkDebug.cpp:10:
/Users/lveronese/Library/Arduino15/packages/esp8266/hardware/esp8266/1.6.4-673-g8cd3697/tools/sdk//include/ets_sys.h:119:6: error: previous declaration of ‘void ets_intr_lock()’ with ‘C++’ linkage
void ets_intr_lock();
^
In file included from /Users/lveronese/Documents/Arduino/libraries/blynk-library-0.2.1/Blynk/BlynkDebug.h:26:0,
from /Users/lveronese/Documents/Arduino/libraries/blynk-library-0.2.1/utility/BlynkDebug.cpp:10:
/Users/lveronese/Library/Arduino15/packages/esp8266/hardware/esp8266/1.6.4-673-g8cd3697/cores/esp8266/Arduino.h:122:20: error: conflicts with new declaration with ‘C’ linkage
void ets_intr_lock();
^
In file included from /Users/lveronese/Documents/Arduino/libraries/blynk-library-0.2.1/Blynk/BlynkDebug.h:17:0,
from /Users/lveronese/Documents/Arduino/libraries/blynk-library-0.2.1/utility/BlynkDebug.cpp:10:
/Users/lveronese/Library/Arduino15/packages/esp8266/hardware/esp8266/1.6.4-673-g8cd3697/tools/sdk//include/ets_sys.h:120:6: error: previous declaration of ‘void ets_intr_unlock()’ with ‘C++’ linkage
void ets_intr_unlock();
^
In file included from /Users/lveronese/Documents/Arduino/libraries/blynk-library-0.2.1/Blynk/BlynkDebug.h:26:0,
from /Users/lveronese/Documents/Arduino/libraries/blynk-library-0.2.1/utility/BlynkDebug.cpp:10:
/Users/lveronese/Library/Arduino15/packages/esp8266/hardware/esp8266/1.6.4-673-g8cd3697/cores/esp8266/Arduino.h:123:22: error: conflicts with new declaration with ‘C’ linkage
void ets_intr_unlock();
^
Error compiling.

It seems the include file is wrong, but I have no clues about how to fix it: can you help me ?

Thanks a lot
Luciano

is this the first time your’e compiling with the 1.6.4 ide?
try going to the BlynkDebug.h file and commenting out these 2 lines:

#ifdef ESP8266
 //   #include "ets_sys.h"
 //   #include "os_type.h"
    #include "mem.h"
#else

worked for me, let me know if it worked :slight_smile:

Well, now the program compiles, but I have warnings:

Build options changed, rebuilding all
In file included from /Users/lveronese/Documents/Arduino/libraries/blynk-library/BlynkSimpleEsp8266.h:15:0,
from ESP8266_Standalone.ino:27:
/Users/lveronese/Documents/Arduino/libraries/blynk-library/BlynkApiArduino.h:44:6: warning: always_inline function might not be inlinable [-Wattributes]
void BlynkApi::processCmd(const void* buff, size_t len)
^
In file included from /Users/lveronese/Documents/Arduino/libraries/blynk-library/BlynkSimpleEsp8266.h:16:0,
from ESP8266_Standalone.ino:27:
/Users/lveronese/Documents/Arduino/libraries/blynk-library/Blynk/BlynkProtocol.h:161:6: warning: always_inline function might not be inlinable [-Wattributes]
void BlynkProtocol::processInput(void)

I am wondering what the changes you suggest actually do…

I did not specify in my previous post that I am using the standard 1.6.4 Arduino IDE and the board file specified here https://github.com/esp8266/Arduino.

Do you have warnings ? And what IDE do you use?

Thanks
Luciano

I actually dont know what they do, I’m also using the Arduino Ide 1.6.4 and followed a post here and that’s the only way it worked for me.

Why do you need the arduino micro at all? Couldn’t you just have everything done on the ESP?

Because the esp can’t send keystrokes to the computer

Hello,

Sorry for stealing this old thread but I can’t find what I want.

I would like to do exactly the inverse of this, I would like to use my keyboard of gamepad that are connected to my desktop to send commands to the ESP8266.

Does any of you have some hints?

Thank you in advance

I would recommend you creating a new topic.