0

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

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

-(void)viewDidLoad { [超级 viewDidLoad];
mySocket = [[AsyncUdpSocket alloc]initWithDelegate:self]; gcdSocket = [[GCDAsyncUdpSocket alloc]initWithDelegate:self delegateQueue:dispatch_get_main_queue()]; mySlide.minimumValue = 0; mySlide.maximumValue = 180; // 在加载视图后做任何额外的设置,通常是从一个 nib。}

-(IBAction)slideValueChange:(UISlider*)sender{
NSInteger value = lround(sender.value); myLabel.text = [NSString stringWithFormat:@"%d",value]; NSString * 地址 = @"192.168.1.7"; UInt16 端口 = 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 { [超级 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 ) ; 
}
4

1 回答 1

0

众所周知,官方 Arduino Wifi Shield 上的 UDP 支持非常有问题。在尝试其他任何事情之前,请确保您通过简单的测试始终如一地工作。你可以在这里找到更多关于我如何让它工作的信息:Arduino Wifi Shield UDP support

于 2013-06-11T16:40:20.033 回答