Please select an option to see content specific to your location and shop online.

Project

Control your wireless computer mouse using XinaBox and OKdo IoT

Scroll down

I need a DiY mouse that sends data over Wi-fi to an IoT platform, which will send it back to my laptop / computer”… said nobody, ever.

And yet, imagine: you are deep inside an all-night coding-session, in the zone, creative juices bursting, and your wireless mouse runs out of power. No spare batteries!!! Well, now you can build your own mouse in less time than it takes to zip over to the local 24 hour convenience store and buy new AAAs!

Hardware Setup

Connect xChips CW01, SH01, SI01 and IP01/IP02 together using the XC10 xBus connectors. You may connect it as shown in the diagram below. Please see this guide on how to assemble xChips generally.

Then connect IP01/IP02 to the usb port of PC to upload the code.

Software Setup

XinaBox xChip CW01 Setup:

  1. Install Arduino IDE to PC.
  2. Install these cores/libraries to Arduino:


Note: If you are not familiar with how to install cores/libraries, please refer to the link: Installing Arduino libraries and Installing Arduino Cores

PC setup:

  1. Install mouse and  allthingstalk-python libraries to PC using the command in terminal- shell.
pip3 install mouse pip3 install allthingstalk

The Code

Code for CW01 – Arduino:

#define JSON
 
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
 
#include <xCore.h>
#include <xSI01.h>
#include <xSH01.h>
 
// Define http and mqtt endpoints
#define http "api.allthingstalk.io" // API endpoint
#define mqtt "api.allthingstalk.io" // broker
 
#include <ATT_IOT.h>
#include <SPI.h> // required to have support for signed/unsigned long type.
 
String TOKEN="Device TOKEN";
String DEVICE_ID="Device ID";
 
WiFiClient espClient;
ATTDevice device(DEVICE_ID,TOKEN);
void callback(char* topic, byte* payload, unsigned int length);
PubSubClient pubSub(mqtt, 1883, callback, espClient);
 
xSI01 SI01;
xSH01 SH01;
long x,y; //mouse movement parameters
 
void setup()
{
  Serial.begin(9600);  // Init serial link for debugging
    // Set the I2C Pins for CW01
  #ifdef ESP8266
    Wire.pins(2, 14);
    Wire.setClockStretchLimit(15000);
  #endif
  
  // Start the I2C Comunication
  Wire.begin();
  
  // Start the  SI01 Sensor
  SI01.begin();
  SH01.begin();
  delay(2000);
  
  // Enter your WiFi credentials here!
  setupWiFi("INTERACTIVE-BRAINS3", "AllahMohammad110");
  
  while(!device.connect(&espClient, http))  // Connect to AllThingsTalk
    Serial.println("retrying");
 
  // Create device assets
  device.addAsset("mCtrlX", "MouseControlX", "Control Mouse X", "sensor", "{"type": "number"}");
 
  device.addAsset("mCtrlY", "MouseControlY", "Control Mouse Y", "sensor", "{"type": "number"}");
 
  device.addAsset("lClick", "LeftClick", "Left Click", "sensor", "{"type": "number"}");
 
  device.addAsset("rClick", "RightClick", "Right Click", "sensor", "{"type": "number"}");
  
   while(!device.subscribe(pubSub))  // Subscribe to mqtt
    Serial.println("retrying"); 
}
void setupWiFi(const char* ssid, const char* password)
{
  delay(10);
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println();
  Serial.println("WiFi connected");
}
 
unsigned long prevTime;
unsigned int prevVal = 0;
 
void loop()
{
  float ax,ay;
   
  SI01.poll();
  SH01.poll();
 
  //incl=SI01.getRoll();
  
  ax=SI01.getAX()*1000;
  ay=SI01.getAY()*1000;
 
  if(abs(ax)<=100)x=0;
  if(abs(ay)<=100)y=0;
 
  if(ax>100)x=map(ax,100,1000,0,10);
  if(ax<-100)x=map(ax,-100,-1000,0,-10);
 
  if(ay>100)y=map(ay,100,1000,0,10);
  if(ay<-100)y=map(ay,-100,-1000,0,-10);
   
  unsigned long curTime = millis();
  if (curTime > (prevTime + 10))  // Update and send counter value every 10 miliseconds
  {
    device.send(String(x), "mCtrlX");
    device.send(String(y), "mCtrlY");
 
    prevTime = curTime;
  }
 
  if(SH01.triangleTouched())device.send("1","lClick");
  if(SH01.crossTouched())device.send("1","rClick");
  
  device.process();
}
void callback(char* topic, byte* payload, unsigned int length) 
{ 
  String assetName = device.getAssetName(topic, strlen(topic));
  Serial.print("Data arrived from asset: ");
  Serial.println(assetName);
 

Code for PC – Python:

import random
 
import mouse as m
 
from time import sleep
 
import allthingstalk as talk
 
import logging
 
logging.basicConfig()
 
# logging.getLogger('allthingstalk').setLevel(logging.DEBUG)
 
class mouseControl(talk.Device):
 
    mCtrlX = talk.NumberAsset()
 
    mCtrlY = talk.NumberAsset()
 
    lClick = talk.NumberAsset()
 
    rClick = talk.NumberAsset()
 
    shutdown = talk.StringAsset(kind=talk.Asset.ACTUATOR)
 
x = 0
 
y = 0
 
lClk = 0
 
rClk = 0
 
client = talk.Client('DEVICE TOKEN')
 
mouse = mouseControl(client=client, id='DEVICE ID')
 
@mouseControl.feed.mCtrlX
 
def log_mouseX(device, value, at):
 
    global x
 
    x = value
 
@mouseControl.feed.mCtrlY
 
def log_mouseY(device, value, at):
 
    global y
 
    y = value
 
@mouseControl.feed.lClick
 
def log_lClk(device, value, at):
 
    global lClk
 
    lClk=1
 
@mouseControl.feed.rClick
 
def log_rClk(device, value, at):
 
    global rClk
 
    rClk=1
 
shutdown = False
 
@mouseControl.command.shutdown
 
def on_shutdown(device, value, at):
 
    global shutdown
 
    print('Shutting down')
 
    shutdown = True
 
while not shutdown:
 
    m.move(-y,x,False)
 
    if(lClk == 1):
        m.click()
        lClk=0
        
    if(rClk == 1):
        m.right_click()
        rClk=0
 
    sleep(0.001)

Result

Left-Click Check:

Right-Click Check:

Summary and help

In this tutorial we have shown how to remotely control Computer Mouse pointer using XinaBox xChips SH01 and SI01. The project is fun in making. The project gives a unique way to use your Computer Mouse pointer wirelessly without using any dongle!

If you have any challenges with this project or have any questions please contact

help@xinabox.cc

Like what you read? Why not show your appreciation by giving some love.

From a quick tap to smashing that love button and show how much you enjoyed this project.