0
votes

i am trying to send the UISlide value to Arduino Uno (With wifi shield) to control the Servo attached.

The following is the Xcode i have made. i am using the AsyncUdpSocket class , also tried with the GCDAsyncUdpSocket. Ok i managed to get it to work by clearing the buffer after every value received. Now the problem is the Lag :( ..

-(void)viewDidLoad { [super viewDidLoad];
mySocket = [[AsyncUdpSocket alloc]initWithDelegate:self]; gcdSocket = [[GCDAsyncUdpSocket alloc]initWithDelegate:self delegateQueue:dispatch_get_main_queue()]; mySlide.minimumValue = 0; mySlide.maximumValue = 180; // Do any additional setup after loading the view, typically from a nib. }

-(IBAction)slideValueChange:(UISlider*)sender{
NSInteger value = lround(sender.value); myLabel.text = [NSString stringWithFormat:@"%d",value]; NSString * address = @"192.168.1.7"; UInt16 port = 8888; NSData *myData = [myLabel.text dataUsingEncoding:NSUTF8StringEncoding];

//[gcdSocket sendData:myData toHost:address port:port withTimeout:-1 tag:2];
  [mySocket  sendData:myData toHost:address port:port withTimeout:-1 tag:2];  
}

-(void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }

-(IBAction)sendMessage:(id)sender {

NSString *message = [[NSString alloc]init];
NSString * address = @"192.168.1.7";
message = @"test";

UInt16 port = 8888;

NSData * data = [message dataUsingEncoding: NSUTF8StringEncoding];


[mySocket  sendData:data toHost:address port:port withTimeout:-1 tag:1];

}

-(void)onUdpSocket:(AsyncUdpSocket *)sock didSendDataWithTag:(long)tag {

if (tag == 1){

    NSLog(@"something is happening !!");
}

else if (tag == 2){

NSLog(@"received message from slider");}}

Following is my arduino code

Servo servo ;


int status = WL_IDLE_STATUS;
char ssid[] = "Allcad"; //  your network SSID (name) 
char pass[] = "gauravsoni"; 

unsigned  int localPort =  8888 ;

char PacketBuffer [ UDP_TX_PACKET_MAX_SIZE ] ;

WiFiUDP Udp;

void setup ()  { 

  Serial.begin(9600); 

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present"); 
    // don't continue:
    while(true);
  } 

  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) { 
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:    
    status = WiFi.begin(ssid,pass);

    // wait 10 seconds for connection:
    delay(10000);
  } 
  Serial.println("Connected to wifi");
  printWifiStatus();

  Serial.println("\nStarting connection to server...");

  Udp.begin(localPort);  

  servo.attach ( 6 ) ; 
}

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

void loop ( )  {

  int packetSize = Udp.parsePacket() ; 
  if ( packetSize ) 
  { 
    Udp.read ( PacketBuffer , UDP_TX_PACKET_MAX_SIZE ) ;
    int num = 0; 

    num = atoi ( PacketBuffer ) ;

    Serial.print("New num value is: ");
    Serial.println(num);
    servo.write (num) ; 

     for(int i=0;i<3;i++)
    {
      Serial.println(i);
      PacketBuffer[i] = 0;
    }
  } 
  delay ( 5 ) ; 
}
1
please post your Arduino codeSr.Richie
@Sr.Richie I have posted the code, i got the arduino to receive the messages , but having a problem of Lag , around a second , not much though. Can you improvise the code ? ThanksGaurav_soni

1 Answers

0
votes

The UDP support on the official Arduino Wifi Shield is know to be quite buggy. Make sure you get this working consistently with a simple test before trying anything else. You can find more information on how I got it to work here: Arduino Wifi Shield UDP support