Go Down

Topic: Arduino via UDP (Read 23372 times) previous topic - next topic

esbjerg

Finally managed to get my yun to save and run python scripts. So, thanks!

Now trying to get my arduino sketch to be sent through the python script. I can't get it to send over.
Arduino code.

Code: [Select]

// Setup pin locations
int flexPin0 = A0; //analog pin 0
int flexPin1 = A1; //analog pin 1
int flexPin2 = A2; //analog pin 2
int flexPin3 = A3; //analog pin 3

int inByte = 0;

void setup(){
  Serial1.begin(9600);
  while (!Serial) {
    ;
  }
 
  establishContact();
}
 

void loop(){
  // Read values
    // if we get a valid byte, read analog ins:
  if (Serial.available() > 0) {
    // get incoming byte:
    inByte = Serial.read();
   
  int flexSensorReading0 = analogRead(flexPin0);
  delay(20);
 
  int flexSensorReading1 = analogRead(flexPin1);
  int flexSensorReading2 = analogRead(flexPin2);
  int flexSensorReading3 = analogRead(flexPin3);


  // Scaling section
  int scale0 = map(flexSensorReading0, 128, 256, 0, 100);
  int scale1 = map(flexSensorReading1, 128, 256, 0, 100);
  int scale2 = map(flexSensorReading2, 128, 256, 0, 100);
  int scale3 = map(flexSensorReading3, 128, 256, 0, 100);
 
  // Output results to serialon one line and spaces
    Serial.write(flexSensorReading0);
    Serial.write(flexSensorReading1);
    Serial.write(flexSensorReading2);
    Serial.write(flexSensorReading3);

  delay(500); //just here to slow down the output for easier reading
}
}

void establishContact() {
  while (Serial.available() <= 0) {
    Serial.print('A');   // send a capital A
    delay(300);
  }
}


Python script
Code: [Select]
#!/usr/bin/python
import socket
import serial

ser = serial.Serial('/dev/ttyATH0', 9600)

while 1:
    data_raw = ser.readline()

UDP_IP = "192.168.1.242"   #Uno IP address
UDP_PORT = 8888
MESSAGE = data_raw
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
sock.sendto(bytes(MESSAGE), (UDP_IP, UDP_PORT))
recvmsg=sock.recv(1024)
print recvmsg
sock.close()


Was hoping I could just use that, but I think I'm not sure how to get the python script to read the serial correctly, and send that out.
Thanks!

esbjerg

#1
Mar 26, 2014, 02:05 pm Last Edit: Mar 26, 2014, 02:17 pm by esbjerg Reason: 1
Just realised that some weren't serial1, so I've changed that now

Revised Arduino
Code: [Select]
//
// Setup pin locations
int flexPin0 = A0; //analog pin 0
int flexPin1 = A1; //analog pin 1
int flexPin2 = A2; //analog pin 2
int flexPin3 = A3; //analog pin 3

int inByte = 0;

void setup(){
 Serial1.begin(9600);
 while (!Serial1) {
   ;
 }
 
 establishContact();
}
 

void loop(){
 // Read values
   // if we get a valid byte, read analog ins:
 if (Serial1.available() > 0) {
   // get incoming byte:
   inByte = Serial.read();
   
 int flexSensorReading0 = analogRead(flexPin0);
 delay(20);
 
 int flexSensorReading1 = analogRead(flexPin1);
 int flexSensorReading2 = analogRead(flexPin2);
 int flexSensorReading3 = analogRead(flexPin3);


 // Scaling section
 int scale0 = map(flexSensorReading0, 128, 256, 0, 100);
 int scale1 = map(flexSensorReading1, 128, 256, 0, 100);
 int scale2 = map(flexSensorReading2, 128, 256, 0, 100);
 int scale3 = map(flexSensorReading3, 128, 256, 0, 100);
 
 // Output results to serial
   Serial1.write(flexSensorReading0);
   Serial1.write(flexSensorReading1);
   Serial1.write(flexSensorReading2);
   Serial1.write(flexSensorReading3);

 delay(500); //just here to slow down the output for easier reading
}
}

void establishContact() {
 while (Serial1.available() <= 0) {
   Serial1.print('A');   // send a capital A
   delay(300);
 }
}

Potikinawah

#2
Apr 15, 2014, 12:46 pm Last Edit: Apr 15, 2014, 12:51 pm by Potikinawah Reason: 1
Hi,

I noticed you tried to do so in order to send sensors informations to max/msp.

In another forum you ended up with these codes

Arduino
Code: [Select]
   /* Sketch for reading 4 analogue inputs for glove
*/

// Setup pin locations
int flexPin0 = A0; //analog pin 0
int flexPin1 = A1; //analog pin 1
int flexPin2 = A2; //analog pin 2
int flexPin3 = A3; //analog pin 3

int inByte = 0;

void setup(){
 Serial.begin(9600);
 while (!Serial) {
   ;
 }  
 establishContact();
}

void loop(){
 // Read values
   // if we get a valid byte, read analog ins:
 if (Serial.available() > 0) {
   // get incoming byte:
   inByte = Serial.read();

 int flexSensorReading0 = analogRead(flexPin0);
 delay(10);
 int flexSensorReading1 = analogRead(flexPin1);
 delay(10);
 int flexSensorReading2 = analogRead(flexPin2);
 delay(10);
 int flexSensorReading3 = analogRead(flexPin3);
 delay(10);

 // Output results to serial
   Serial.write(flexSensorReading0);
   Serial.write(flexSensorReading1);
   Serial.write(flexSensorReading2);
   Serial.write(flexSensorReading3);

 delay(50); //just here to slow down the output for easier reading
 }
}

void establishContact() {
 while (Serial.available() <= 0) {
   Serial.println('A');   // send a capital A
   delay(500);
 }
}


Python Script
Code: [Select]
#!/usr/bin/python
import socket
import serial

ser = serial.Serial('/dev/ttyATH0', 9600)


while ser.read() != 'A':
#   do nothing
   pass


UDP_IP = "192.168.1.242"   #Max IP address
UDP_PORT = 8888
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP

while True: # ser.readline() == 'A':
   data_raw = read(8) #read 8 bytes
   sock.sendto(bytes(data_raw), (UDP_IP, UDP_PORT)) #sends the byte
   ser.write('A')

#recvmsg=sock.recv(1024) #read response
#print recvmsg
sock.close()


Did it work for you (thru wifi ?)
I can't get how you have the Yun running the Python script (I thought it would need to use the "Process" class within Bridge Library)
I guess you specified a static IP adress to your wifi on the laptop running max/msp

I'm actually investigating in the same way you did, in order to send sensors raw values to max/msp thru wifi

Thanks

esbjerg

Hey! I never got it to work, you could be right, I had no experience when trying this so was pretty much stabbing wildly with help from people on here!
Have you managed to get anything working?

Potikinawah

Yes i have somethin that works perfectly actually.

A bit slow using the Bridge class with Bridge.put() function.

Look here for the arduino and python code to receive an analog input from a Pot, you can adapt it quite easily to receive a bundle of informations (i'll come back juste a tiny bit later to post the codes to send a 3-axis compass and to do it with python OSC.OSCBundle() http://forum.arduino.cc/index.php?PHPSESSID=ru1melcgtjodsj6ov9amqb49h7&topic=233723.0

I'm lauching the python script through SSH command into the arduino. I tried to lauch it within th arduino sketch but it slows the process (4 times slower the way i did it)
In Max/MSP you only need a [udpreceive] then [OSC-route /Pot] to get the OSC message then  [fromsymbol] to transform the string into int.

I'm finishing the test tomorrow using serial communication within Python in order to make it faster, today i'll have too much work with the choreography and Max patch to adapt to the performance.

Hope it helps (using Bridge.put() i can have a 5/6Hz communication, so one information every 166/200ms. It's quite slow but might be enough for a lot of use anyway)

Go Up