venkatsai.laurel
Posts: 1
Joined: Sat Jun 23, 2018 10:42 am

convert aurdino code to python for raspberry pi

Sat Jun 23, 2018 10:52 am

I want this code of VOICE CONTROLLER in aurdino to python which can be executed in raspberry pi

Code: Select all

#include <SPI.h>
#include <Ethernet.h>
#define tv 7
#define red 6
#define green 5
#define blue 4
#define fadespeed 10
#define buzzer A2
#define buzzlight 3

// Set the MAC address
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};

// Set the IP address
IPAddress ip(172, 16, 77, 50);
//IPAddress gateway=(192, 168, 0, 1);
//IPAddress subnet=(255, 255, 255, 0);

// Start a server at port 80 (http)
EthernetServer server(80);

void setup() {
  // Open serial communications
  Serial.begin(9600);

  // start the Ethernet connection and the server
  Ethernet.begin(mac, ip);
  server.begin();
  pinMode(tv, OUTPUT);
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(blue, OUTPUT);
  pinMode(buzzer, OUTPUT);
  pinMode(buzzlight, OUTPUT);
 /* analogWrite(red,0);
  analogWrite(green,0);
  analogWrite(blue,255);*/
}


void loop() {
int r,g,b;
  
  // Check if client connected
  EthernetClient client = server.available();
  
  if (client) { // If there is a client...
    
    boolean currentLineIsBlank = true;
    String readString= ""; // A buffer for the GET request
    
    while (client.connected()) {
      //Serial.print("connected");

      if (client.available()) {
        char c = client.read();
        if(readString.length() < 100){
        readString +=c; // Store the data in a buffer
        Serial.print(c);
        }
        if(c=='\n')
        {
          
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();// Blank line ==> end response
            client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          client.println("<head>");
          client.println("<title>voice controller</title>");
          client.println("</head>");
          client.println("<body><center>");
          client.println("<h1>VOICE CONTROLLER</h1><BR><BR><BR>");
          client.println("<form action=\"$1\" method=\"get\">");
          client.println("<button type=submit style=\"width:200px\">TV ON</button>");
          client.println("</form><br>");
          client.println("<form action=\"$2\" method=\"get\">");
          client.println("<button type=submit style=\"width:200px\">TV OFF</button>");
          client.println("</form><br>");
          client.println("<form action=\"$3\" method=\"get\">");
          client.println("<button type=submit style=\"width:200px\">AC ON</button>");
          client.println("</form><br>");
           client.println("<form action=\"$4\" method=\"get\">");
          client.println("<button type=submit style=\"width:200px\">AC OFF</button>");
          client.println("</form><br>");
           client.println("<form action=\"$5\" method=\"get\">");
          client.println("<button type=submit style=\"width:200px\">START ALARM</button>");
          client.println("</form><br>");
           client.println("<form action=\"$6\" method=\"get\">");
          client.println("<button type=submit style=\"width:200px\">STOP ALARM</button>");
          client.println("</form><br>");
          client.println("<form action=\"$7\" method=\"get\">");
          client.println("<button type=submit style=\"width:200px\">STOP</button>");
          client.println("</form>");
          client.print(readString);
          client.println("</body></center>");
          client.print("</html>");
         delay(1);
         client.stop();
          if(readString.indexOf("$1") >0) {
              digitalWrite(tv,HIGH);
              Serial.println("TV ON");
            }
            
            if(readString.indexOf("$2") >0) {
           Serial.println("TV OFF");
             digitalWrite(tv,LOW);
            }
         if(readString.indexOf("$3") >0) {
           Serial.println("AC ON");
           //digitalWrite(red,11);
            for(r=0;r<256;r++)
            {
              analogWrite(red,r);
              delay(fadespeed);
            }
            for(b=255;b>0;b--)
            {
              analogWrite(blue,b);
              delay(fadespeed);
            }
            for(g=0;r<256;g++)
            {
              analogWrite(green,g);
             delay(fadespeed);
            }
            for(r=255;r>0;r--)
            {
              analogWrite(red,r);
              delay(fadespeed);
            }
            for(b=0;b<256;b++)
            {
              analogWrite(blue,b);
              delay(fadespeed);
            }
            for(g=255;g>0;g--)
            {
              analogWrite(green,g);
              delay(fadespeed);
            } 
            }
            if(readString.indexOf("$4") >0) {
           Serial.println("AC OFF");
        
             analogWrite(red,0);
              analogWrite(green,0);
              analogWrite(blue,0);
            }
            if(readString.indexOf("$5") >0) {
              int a=analogRead(buzzer);
              Serial.println("ALARM ON");
              for(int i=1;i<300;i++)
              {
              analogWrite(buzzer,i);  
              digitalWrite(buzzlight,1);
            }
            }
            if(readString.indexOf("$6") >0) {
              analogWrite(buzzer,0);  
              digitalWrite(buzzlight,0);
              Serial.println("ALARM OFF");
            }
            if(readString.indexOf("$7") >0) {
              digitalWrite(tv,0);
             analogWrite(red,0);
              analogWrite(green,0);
              analogWrite(blue,0);
              analogWrite(buzzer,0);  
              digitalWrite(buzzlight,0);
              Serial.println("ALL APPLIANCES ARE IN OFF STATE");
            }
            readString="";
        }
       
         
        }
    }
            
          }
        }

sora03
Posts: 193
Joined: Mon Dec 29, 2014 4:11 pm
Contact: Website

Re: convert aurdino code to python for raspberry pi

Sat Jun 23, 2018 12:37 pm

venkatsai.laurel wrote:
Sat Jun 23, 2018 10:52 am
I want this code of VOICE CONTROLLER in aurdino to python which can be executed in raspberry pi

Code: Select all

#include <SPI.h>
#include <Ethernet.h>
#define tv 7
#define red 6
#define green 5
#define blue 4
#define fadespeed 10
#define buzzer A2
#define buzzlight 3

// Set the MAC address
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};

// Set the IP address
IPAddress ip(172, 16, 77, 50);
//IPAddress gateway=(192, 168, 0, 1);
//IPAddress subnet=(255, 255, 255, 0);

// Start a server at port 80 (http)
EthernetServer server(80);

void setup() {
  // Open serial communications
  Serial.begin(9600);

  // start the Ethernet connection and the server
  Ethernet.begin(mac, ip);
  server.begin();
  pinMode(tv, OUTPUT);
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(blue, OUTPUT);
  pinMode(buzzer, OUTPUT);
  pinMode(buzzlight, OUTPUT);
 /* analogWrite(red,0);
  analogWrite(green,0);
  analogWrite(blue,255);*/
}


void loop() {
int r,g,b;
  
  // Check if client connected
  EthernetClient client = server.available();
  
  if (client) { // If there is a client...
    
    boolean currentLineIsBlank = true;
    String readString= ""; // A buffer for the GET request
    
    while (client.connected()) {
      //Serial.print("connected");

      if (client.available()) {
        char c = client.read();
        if(readString.length() < 100){
        readString +=c; // Store the data in a buffer
        Serial.print(c);
        }
        if(c=='\n')
        {
          
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();// Blank line ==> end response
            client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          client.println("<head>");
          client.println("<title>voice controller</title>");
          client.println("</head>");
          client.println("<body><center>");
          client.println("<h1>VOICE CONTROLLER</h1><BR><BR><BR>");
          client.println("<form action=\"$1\" method=\"get\">");
          client.println("<button type=submit style=\"width:200px\">TV ON</button>");
          client.println("</form><br>");
          client.println("<form action=\"$2\" method=\"get\">");
          client.println("<button type=submit style=\"width:200px\">TV OFF</button>");
          client.println("</form><br>");
          client.println("<form action=\"$3\" method=\"get\">");
          client.println("<button type=submit style=\"width:200px\">AC ON</button>");
          client.println("</form><br>");
           client.println("<form action=\"$4\" method=\"get\">");
          client.println("<button type=submit style=\"width:200px\">AC OFF</button>");
          client.println("</form><br>");
           client.println("<form action=\"$5\" method=\"get\">");
          client.println("<button type=submit style=\"width:200px\">START ALARM</button>");
          client.println("</form><br>");
           client.println("<form action=\"$6\" method=\"get\">");
          client.println("<button type=submit style=\"width:200px\">STOP ALARM</button>");
          client.println("</form><br>");
          client.println("<form action=\"$7\" method=\"get\">");
          client.println("<button type=submit style=\"width:200px\">STOP</button>");
          client.println("</form>");
          client.print(readString);
          client.println("</body></center>");
          client.print("</html>");
         delay(1);
         client.stop();
          if(readString.indexOf("$1") >0) {
              digitalWrite(tv,HIGH);
              Serial.println("TV ON");
            }
            
            if(readString.indexOf("$2") >0) {
           Serial.println("TV OFF");
             digitalWrite(tv,LOW);
            }
         if(readString.indexOf("$3") >0) {
           Serial.println("AC ON");
           //digitalWrite(red,11);
            for(r=0;r<256;r++)
            {
              analogWrite(red,r);
              delay(fadespeed);
            }
            for(b=255;b>0;b--)
            {
              analogWrite(blue,b);
              delay(fadespeed);
            }
            for(g=0;r<256;g++)
            {
              analogWrite(green,g);
             delay(fadespeed);
            }
            for(r=255;r>0;r--)
            {
              analogWrite(red,r);
              delay(fadespeed);
            }
            for(b=0;b<256;b++)
            {
              analogWrite(blue,b);
              delay(fadespeed);
            }
            for(g=255;g>0;g--)
            {
              analogWrite(green,g);
              delay(fadespeed);
            } 
            }
            if(readString.indexOf("$4") >0) {
           Serial.println("AC OFF");
        
             analogWrite(red,0);
              analogWrite(green,0);
              analogWrite(blue,0);
            }
            if(readString.indexOf("$5") >0) {
              int a=analogRead(buzzer);
              Serial.println("ALARM ON");
              for(int i=1;i<300;i++)
              {
              analogWrite(buzzer,i);  
              digitalWrite(buzzlight,1);
            }
            }
            if(readString.indexOf("$6") >0) {
              analogWrite(buzzer,0);  
              digitalWrite(buzzlight,0);
              Serial.println("ALARM OFF");
            }
            if(readString.indexOf("$7") >0) {
              digitalWrite(tv,0);
             analogWrite(red,0);
              analogWrite(green,0);
              analogWrite(blue,0);
              analogWrite(buzzer,0);  
              digitalWrite(buzzlight,0);
              Serial.println("ALL APPLIANCES ARE IN OFF STATE");
            }
            readString="";
        }
       
         
        }
    }
            
          }
        }
You may want to learn Python coding, it is much easier than C though. If you need a voice assistant try Jasper. Note that the Pi does not have analog in/out so you will need a MCP3008/MCP3002 to conver analog to digital signal
FLOSS enthusiast, Debian user (linux-libre)

User avatar
Arjan
Posts: 262
Joined: Sat Sep 08, 2012 1:59 pm

Re: convert aurdino code to python for raspberry pi

Sat Jun 23, 2018 8:48 pm

venkatsai.laurel wrote:
Sat Jun 23, 2018 10:52 am
I want this code of VOICE CONTROLLER in aurdino to python which can be executed in raspberry pi
See append Running Arduino sketches on the Pi -> viewtopic.php?t=134928
http://www.raspberrypi-dmx.org/
Open Source DMX/RDM/MIDI/OSC/Art-Net/sACN solutions

Return to “Beginners”