Logic Machine Forum
Nabaztag control - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Nabaztag control (/showthread.php?tid=1247)



Nabaztag control - gilles38 - 22.02.2018

Hi all as a contribution here after you will find my code done to pilot a Nabaztag

as a reminder Nabaztag is a communicating rabbit, that can speak(TTS), play music, move ears, have an embded RFID sensor. I personnaly use it as vocal feedback of my homeautomation system (homelynk based)

Code:
--[[
Author: Gilles38
Name: Class_Nabaztag
Version: 0.0.1
Design for HomeLYnk and spaceLYnk

Available methodes:
        Is_Reachable() return boolean
                Is_Connected() return boolean
                Is_Sleeping() return boolean
                Reboot() return true or error message
                SetEars($Pos_Gauche,$Pos_Droite) return true or error message
                Speak($Texte,$Voix)    return true or error message
                Play_Stream($Url) return true or error message
         Get_Name()    return string
                Get_Voices()    return array
                Wakeup() return true or error message
                Goto_Sleep() return true or error message

Required for constructor:
                Adresse_main:define standard server adresse
                Adresse_Unified: define alternativ server
                Name: is the name of the Rabbit
                Token: define the token of the rabbit
                Mac: Mac adress of the rabbit
                Voice: define the list of voices available
]]--


Nabaztag={Name="",Token="",Mac="", Adresse_Main="", Adresse_Unified="", Voice={}}
Nabaztag.__index = Nabaztag

--Nabaztag class constructor
function Nabaztag:new( o,Name, Token, Mac)
 o=o or {}
 setmetatable(o, self)
 self.Name = Name
 self.Token = Token
 self.Mac = Mac
 self.Adresse_Main="http://openjabnab.fr/ojn/FR/api.jsp"
 self.Adresse_Unified="http://api.wizz.cc/?server=pixel"
 self.Voices= {"Alice","Antoine","Bruno","Claire","Julie","Margaux","Lorise","Deepa","Graham","Lucy","Peter","Rachel"}
 log("Creation classe name:"..self.Name.." Token:"..self.Token)
 return o
end

--Send standard command  to Nabaztag return true or false
function Nabaztag:Send_Command(action)
  Query=self.Adresse_Main.."?sn="..self.Mac.."&token="..self.Token.."&action="..action
 return(self:Launch_Query(Query))
end

function Nabaztag:Launch_Query(Query)
 log("Launch-Query")
 require ("socket")
    require ("socket.http")
 respResult, respcode, respHeaders, respStatus = socket.http.request(Query)
 return(respResult)
end

--************Get Rabbit name**********
function Nabaztag:Get_name()
 return(self.Name)
end

--**********Get voices available*******
function Nabaztag:Get_Voices()
 return(self.Voices)
end

--************Goto slip****************
function Nabaztag:Goto_Sleep()
 return(self:Send_Command(14))
end
 
--**********Make the rabbit wakeup**********
function Nabaztag:Wakeup()
   return(self:Send_Command(13))
end

--*********Play sounds from URL***********
function Nabaztag:Play_Stream(Url)
 Query=self.Adresse_Unified.."&sn="..self.Mac.."&token="..self.Token.."&urlList="..Url
 result=self:Launch_Query(Query)
end

--********Make the rabbit speak**********
function Nabaztag:Speak(Text,Voice)
     --*******Url encode**********
  if (Text) then
     Text = string.gsub (Text, "\n", "\r\n")
     --Text = string.gsub (Text, "([^%w ])",
       -- function (c) return string.format ("%%%02X", string.byte(c)) end)
   Text = string.gsub (Text, " ", "+")
   Text=string.gsub(Text,"é","%%%e9")
   Text=string.gsub(Text,"è","%%%e8")
   Text=string.gsub(Text,"ç","%%%e7")
   Text=string.gsub(Text,"ê","%%%ea")
   Text=string.gsub(Text,"ô","%%%f4")
   Text=string.gsub(Text,"� ","%%%e0")
   Text=string.gsub(Text,"â","%%%e2")
   Text=string.gsub(Text,"?","%%%3f")
   Text=string.gsub(Text,":","%%%3a")
  end
 log(Text)
 Query=self.Adresse_Unified.."&sn="..self.Mac.."&token="..self.Token.."&tts="..Text.."&ws_acapela="..Voice
 result=self:Launch_Query(Query)
end

--*******Ears position changing****************
--*******Parameters: Left ears, right ears value from 0 to 16
--*******return tru if OK else an error message
function Nabaztag:SetEars(Pos_Gauche,Pos_Droite)
 if(Pos_Gauche>16) then
   Pos_Gauche=16
 end
 
 if(Pos_Droite>16) then
   Pos_Droite=16
 end
 
 Query=self.Adresse_Main.."&sn="..self.Mac.."&token="..self.Token.."&posleft="..Pos_Gauche.."&posright="..Pos_Droite
 result=self:Launch_Query(Query)
end


--*****Reboot rabbit*********
function Nabaztag:Reboot()
 return(self:Send_Command(17))
end

--*********Check if rabbit is connected********
function Nabaztag:Is_Connected()
 if(self:Send_Command(15)~=nil) then
     if (string.find(self:Send_Command(15),"YES") ~= nil) then
       return(true)
        else
       return(false)
        end
 else
   return(false)
 end
end

--*********Check if rabbit is sleeping********
function Nabaztag:Is_Sleeping()
 if (string.find(self:Send_Command(7),"YES") ~= nil) then
   return(true)
    else
   return(false)
    end
end

--*********Check if rabbit can launch an action********
function Nabaztag:Is_Reachable()
    return(self:Is_Sleeping() and self:Is_Connected())
end


function  Nabaztag:Status()
 
 if(self:Is_Connected()) then
   if(self:Is_Sleeping()) then
     return(2)
   else
     return(1)
   end
 else
   return (0)
 end
 
end



RE: Nabaztag control - admin - 22.02.2018

Nice, one small suggestion: have you tried using socket.url.escape instead of multiple string.gsub? It should handle other language-specific characters as well.


RE: Nabaztag control - gilles38 - 22.02.2018

(22.02.2018, 11:29)admin Wrote: Nice, one small suggestion: have you tried using socket.url.escape instead of multiple string.gsub? It should handle other language-specific characters as well.

thanks I will try Wink  and update the code accordingly.