element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Send Python data wirelessly to router connected to Ethernet shield on Arduino and read it?
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Not Answered
  • Replies 0 replies
  • Subscribers 400 subscribers
  • Views 255 views
  • Users 0 members are here
  • python
  • help
  • controller
  • robotic
  • ethernet
  • robotics
  • drive
  • control
  • send
  • robot
  • usb
  • wirelessly
  • serial
  • tank
  • wireless
  • shield
  • data
  • motor
  • arduino
  • router
Related

Send Python data wirelessly to router connected to Ethernet shield on Arduino and read it?

Former Member
Former Member over 12 years ago

I am trying to take my Python script that reads two joysticks on a USB PlayStation 3 controller and then send it wirelessly to a router connected to my arduino via a ethernet shield.

 

Currently, I can read my controller and send its data over serial to my Arduino. From there, the Arduino can process the information in order to drive two motor controllers (Jaguar speed controllers). I am trying to get the data sent wirelessly. I would like to be able to hook my TP-LINK router to my Ethernet shield and send data to it. I am not sure if this will change my code completely. I have attached what I currently programmed in both Python and the Arduino language.

 

My goal is to create a robot that I can controller with just my PlayStation controller plugged into my computer. It will read the data and send it to the router attached to the Arduino wirelessly. From there, the Arduino will be able to read the incoming data and process it into motor controls (for a tank drive robot, each joystick for both sides of the robot). I have it working properly when all connected. I just need to make things wireless. Hopefully this is not too difficult of a task. Help would be very appreciated. Thanks a lot!


Python code: (Reading Controller Attempt)


#!/usr/bin/env python

 

 

import pygame, serial, os, sys, time   #, msvcrt

 

 

print "Opening Serial port..."

from pygame import locals

 

 

arduino = serial.Serial(2, 9600, timeout = 1)

 

 

time.sleep(2)

 

 

print "Initialize Complete"

 

 

pygame.init()

 

 

pygame.joystick.init() # main joystick device system

 

 

try:

   j = pygame.joystick.Joystick(0) # create a joystick instance

   j.init() # init instance

   print 'Enabled joystick: ' + j.get_name()

   print "55,55;"

   arduino.write("55,55;")

except pygame.error:

   print 'No joystick found.'

 

 

while 1:

   for e in pygame.event.get(): # iterate over event stack

        if e.type == pygame.locals.JOYAXISMOTION: # Read Analog Joystick Axis

            y1 = j.get_axis(1) # Left Stick

            y2 = j.get_axis(3) # Right Stick

 

 

            #y1 = (y1 * 255) + 255

            #y2 = (y2 * 255) + 255

 

 

            y1 = (y1 * 45) + 55

            y2 = (y2 * 45) + 55

           

            leftStick = int (y1)

            rightStick = int (y2)

 

 

            leftStick = str (leftStick)

            rightStick = str (rightStick)

 

 

            #leftCommand = 'L%s' % leftStick

            #rightCommand = 'R%s' % rightStick

 

 

            arduinoCommand = leftStick

            arduinoCommand += ','

            arduinoCommand += rightStick

            arduinoCommand += ';'

 

 

            print arduinoCommand

            arduino.write(arduinoCommand)

            #time.sleep(.1)

 

Arduino code: (TankDriveRobot)

 

//

#include <avr/interrupt.h>

const int leftjagsigpin = 9; //pwm connection on pin 9

const int leftjagpwrpin = 7;//using pin 7 as the pwm power reference

const int leftjaggndpin = 8;//using pin 8 as the pwm ground reference

//const int ledpin = 13;//using the standard LED as a status indicator

//const int buffsize = 9;//determining the digits buffer size

const int rightjagsigpin = 10; //pwm connection on pin 9

const int rightjagpwrpin = 2;//using pin 7 as the pwm power reference

const int rightjaggndpin = 4;

 

 

//char mybuffer[buffsize]; //declare an array to take input from the serial

int zeroValue = 47;// decimal 47 corresponds with the pwm midpoint

//int minvalue = 20;// decimal 20 corresponds with the pwm full reverse

//int maxvalue = 74; //decimal 74 corresponds with the pwm full forward

//char inChar;

//byte index = 0;

//int leftRead;

//int rightRead;,

//int leftMotor;

//int rightMotor;

 

 

//boolean ledstatus = LOW; //initialize the LED status

 

 

const int NUMBER_OF_FIELDS = 2; // how many comma separated fields we expect

int fieldIndex = 0;            // the current field being received

int values[NUMBER_OF_FIELDS];   // array holding values for all the fields

 

 

int motorState;

int motorVal;

 

void setup()

{

  //set the PWM frequency to be 122Hz

  TCCR1B = TCCR1B & 0b11111000 | 0x04;

 

 

//setup the pins to be used with the PWM

  pinMode(leftjagsigpin, OUTPUT);

  pinMode(leftjagpwrpin, OUTPUT);

  pinMode(leftjaggndpin, OUTPUT);

 

  pinMode(rightjagsigpin, OUTPUT);

  pinMode(rightjagpwrpin, OUTPUT);

  pinMode(rightjaggndpin, OUTPUT);

 

 

//setup the LED status pin

  //pinMode(ledpin, OUTPUT);

 

 

//define the pwm reference voltages

  digitalWrite(leftjagpwrpin,HIGH);

  digitalWrite(leftjaggndpin,LOW);

  digitalWrite(rightjagpwrpin,HIGH);

  digitalWrite(rightjaggndpin,LOW);

 

 

//start the serial interface... 9600 is arbitrary

  Serial.begin(9600);

 

  Serial.println("We have connection");

 

 

//set the pwm to midscale so the jaguar starts in a friendly state

  analogWrite(leftjagsigpin, zeroValue);

  //delay(1);

  analogWrite(rightjagsigpin, zeroValue);

 

 

 

}

 

 

// the loop routine runs over and over again forever:

void loop()

{

  if( Serial.available())

  {

    char ch = Serial.read();

    if(ch >= '0' && ch <= '9') // is this an ascii digit between 0 and 9?

    {

      // yes, accumulate the value

      values[fieldIndex] = (values[fieldIndex] * 10) + (ch - '0');

    }

    else if (ch == ',')  // comma is our separator, so move on to the next field

    {

      if(fieldIndex < NUMBER_OF_FIELDS-1)

        fieldIndex++;   // increment field index

    }

    else

    {

      // any character not a digit or comma ends the acquisition of fields

      // in this example it's the newline character sent by the Serial Monitor

      //Serial.print( fieldIndex +1);

      //Serial.println(" fields received:");

      for(int i=0; i <= fieldIndex; i++)

      {

       

       

        int motorState = values[i];

        if (motorState == values[0])

        {

          int motorVal = map(motorState, 99, 10, 35, 59);

          //Serial.print("Motor 1...");

          //Serial.println(motorVal);

          analogWrite(leftjagsigpin, motorVal);

        }

        else if (motorState == values[1])

        {

          int motorVal = map(motorState, 99, 10, 35, 59);

          //Serial.print("Motor 2...");

          //Serial.println(motorVal);

          analogWrite(rightjagsigpin, motorVal);

        }

       

       

        values[i] = 0; // set the values to zero, ready for the next message

      }

      Serial.flush();

      fieldIndex = 0;  // ready to start over

    }

  }

}

 

 

 

 

 

 

 

 

 

 



Attachments:
ReadingControllerAttempt.zip
TankDriveRobot.ino.zip
  • Sign in to reply
  • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube