UISlide 値を Arduino Uno (wifi シールド付き) に送信して、接続されているサーボを制御しようとしています。
以下は私が作成したXcodeです。私は AsyncUdpSocket クラスを使用していますが、GCDAsyncUdpSocket でも試しました。わかりました、すべての値を受信した後にバッファをクリアすることで、なんとか機能させることができました。今問題はラグです:( ..
-(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; // 通常はペン先からビューをロードした後、追加のセットアップを行います。}-(IBAction)slideValueChange:(UISlider*)sender{
NSInteger 値 = 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 { [super didReceiveMemoryWarning]; // 再作成できるすべてのリソースを破棄します。}
-(IBAction)sendMessage:(id)送信者 {
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 ) ;
}