Eduardo Contreras floresAndres Sabas
Published © CERN-OHL

Telegram Bot with Intel Edison

An application that communicates with a bot from telegram with the edison to get access to inputs and outputs from the edison.

IntermediateProtip2 hours1,284
Telegram Bot with Intel Edison

Things used in this project

Hardware components

Intel Edison
Intel Edison
With the arduino expansion board
×1

Software apps and online services

Telegram

Story

Read more

Schematics

FF4UF0QIG2JIM0K.LARGE.jpg

Into the cellphone on telegram
Temp

Code

Edison with bots

JavaScript
Through the terminal we'll install first the MRAA (Low Level Skeleton Library for IO Communication on GNU/Linux platforms) in order to can controll our GPIO from javascript

echo "src mraa-upm <a href="http://iotdk.intel.com/repos/1.1/intelgalactic" <a href="http://iotdk.intel.com/repos/1.1/intelgalactic" rel="nofollow"> http://iotdk.intel.com/repos/1.1/intelgalactic" </a> > /etc/opkg/mraa-upm.conf

opkg update
opkg install libmraa0


Now we'll install in npm the necesary tools in javascript for the Telegram API that controll the bots and the conection with MRAA

npm install node-telegram-bot mraa

You need to change the values to your own bot, in this case on the token section you must copy the token generated by the BotFather.

We're using the MRAA library to do the digitals inputs and outputs and also the analogic inputs, to see other things that MRAA allows you, you can visit the repository on the examples section.
var i2c = require('./i2c');
var display = new i2c.LCD(0);
var Bot = require('node-telegram-bot');
var m = require('mraa'); //require mraa
console.log('MRAA Version: ' + m.getVersion()); //write the mraa version to the console
var analogPin0 = new m.Aio(0); //setup access analog inpuput pin 0
var analogPin1 = new m.Aio(1);
var led = new m.Gpio(2); 
led.dir(m.DIR_OUT); //set the gpio direction to output
var bot = new Bot({
  token: 'setyourtokenhere'
});


bot.on('message', function (message) {
  parseMessage(message);
});


bot.start();
console.log("BOT ready!");
useLcd();


// Functions that handles a new message
function parseMessage(message) {

  //if(!isAuthorized(message.from.id)) return;

  switch(true) {

    case message.text == "/gettemp":
      var a = analogPin0.read();
      var resistance = (1023 - a) * 10000 / a; 
      var celTemp = 1 / (Math.log(resistance / 10000) / 3975 + 1 / 298.15) - 273.15;
          bot.sendMessage({
        chat_id: message.chat.id,
        text: 'The temperature is ' + celTemp.toFixed(2) + '°C',
      });
      break;

    case message.text == "/getlight":
     var c = analogPin1.read();
       if(c <250)
          {
             led.write(1);
          bot.sendMessage({
        chat_id: message.chat.id,
        text: 'I turn on the light',
       });
          }
        else { 
         led.write(0);
            bot.sendMessage({
        chat_id: message.chat.id,
        text: 'I turn off the light',
            });      }
      break;

      case message.text == "/getlcd":
          display.setColor(0, 168 , 255);
        display.setCursor(0,0);
        display.write('Hello there');
        display.setCursor(1,0);
        display.write('A message from telegram');          
          bot.sendMessage({
        chat_id: message.chat.id,
        text: 'LCD prendida ',
      });
      break;     
       
  }
}

function isAuthorized(userid) {

  for(i = 0; i < authorized_users.length; i++)
    if(authorized_users[i ] == userid) return true;

  return false;
}

function useLcd() {
    

    display.setColor(0, 0, 255);
    display.setCursor(0, 0);
    display.write('Welcome');
    display.setCursor(1,0);  
    display.write('Home');
    display.waitForQuiescent()
    .then(function() {
    })
    .fail(function(err) {
        console.log(err);
        display.clearError();
        rotateColors(display);
    });
}

Credits

Eduardo Contreras flores

Eduardo Contreras flores

5 projects • 9 followers
Electronic enginner, maker mexican :D
Andres Sabas

Andres Sabas

9 projects • 21 followers
Co-Founder of The Inventor's House Hackerspace, DIY, Workaholic

Comments