使用AsyncUdpSocket通过UDP向Arduino发送UISlider值

时间:2013-06-10 09:42:36

标签: objective-c udp arduino

我正在尝试将UISlide值发送到Arduino Uno(带wifi屏蔽)来控制附加的伺服。

以下是我制作的Xcode。我正在使用AsyncUdpSocket类,也尝试使用GCDAsyncUdpSocket。好吧,我设法通过在收到每个值后清除缓冲区来使其工作。现在的问题是滞后:( ..

  

- (无效)viewDidLoad中   {       [super viewDidLoad];
      mySocket = [[AsyncUdpSocket alloc] initWithDelegate:self];       gcdSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];       mySlide.minimumValue = 0;       mySlide.maximumValue = 180;       //在加载视图后进行任何其他设置,通常是从笔尖。   }

     

- (IBAction为)slideValueChange:(UISlider *)发件人{
      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];  
}
  

- (无效)didReceiveMemoryWarning   {       [super didReceiveMemoryWarning];       //处理可以重新创建的任何资源。   }

     

- (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");}}
  

以下是我的arduino代码

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 个答案:

答案 0 :(得分:0)

官方Arduino Wifi Shield上的UDP支持已知非常错误。在尝试其他任何事情之前,请确保您通过简单的测试始终如一地工作。您可以在此处找到有关我如何使用它的更多信息:Arduino Wifi Shield UDP support