Arduino wifi shield (wiFly shield) を用いたAdhoc通信

1.設定したadhoc network のssid(下記の例だと”WiFly-Ad-Hoc”)に接続する。

2.PCのIP設定をstatic IP (DHCPではなく)で設定しておく。

//udp

参考URL/ライブラリ:

https://github.com/Seeed-Shield/WiFi_Shield

#include <Arduino.h>
#include <SoftwareSerial.h>
#include "WiFly.h"

#define SSID "WiFly-Ad-Hoc"

#define UDP_HOST_IP "255.255.255.255" // broadcast
#define UDP_REMOTE_PORT 5000
#define UDP_LOCAL_PORT 5000

WiFly wifly(2, 3);

char cmd[32];
char ip[24];
char packet[64];
int id; // WiFly's ID, between 1 and 100
unsigned long time_point = 0;

void setup() {

Serial.begin(9600);
 Serial.println("--------- WiFly Ad-Hoc --------");

 wifly.reset();

 if (wifly.version() >= 2.45) {
 Serial.println("The version of this WiFly's firmware is >= 2.45.");
 Serial.println("Warnning! This WiFly's firmware does not support Ad-Hoc mode. Try Ad-Hoc anyway");
 }

 // set up Ad-Hoc mode
 wifly.sendCommand("set w j 4\r", "AOK");
 wifly.sendCommand("set w s " SSID "\r", "AOK");
 wifly.sendCommand("set w c 1\r", "AOK");

 // get random id
 randomSeed(analogRead(3));
 id = random(1, 100);

 snprintf(ip, sizeof(ip), "10.0.0.%d\r", id);
 snprintf(cmd, sizeof(cmd), "set i a %s\r", ip);
 wifly.sendCommand(cmd, "AOK");
// wifly.sendCommand("set i a 10.0.0.10\r", "AOK");

wifly.sendCommand("set i n 255.255.255.0\r", "AOK");
 wifly.sendCommand("set i d 0\r", "AOK");

 // set up UDP
 wifly.sendCommand("set i p 1\r", "AOK");
 snprintf(cmd, sizeof(cmd), "set i h %s\r", UDP_HOST_IP);
 wifly.sendCommand(cmd, "AOK");
 snprintf(cmd, sizeof(cmd), "set i r %u\r", UDP_REMOTE_PORT);
 wifly.sendCommand(cmd, "AOK");
 snprintf(cmd, sizeof(cmd), "set i l %u\r", UDP_LOCAL_PORT);
 wifly.sendCommand(cmd, "AOK");

 // set up time
 // wifly.sendCommand("set t a 202.155.212.78\r", "AOK"); // 2.pool.ntp.org
 wifly.sendCommand("set t e 0\r", "AOK");
 wifly.sendCommand("set t r 1\r", "AOK");

 wifly.sendCommand("save\r");
 wifly.sendCommand("reboot\r");

 delay(1000);
 wifly.clear();
 Serial.print("IP: ");
 Serial.println(ip);
 Serial.println("SSID: " SSID);
}

char line_buf[64];
int index = 0;

void loop() {
 while (wifly.available()) {
 Serial.print((char)wifly.read());
 }

#if 1
 while (Serial.available()) {
 wifly.write(Serial.read());
 }
#endif

 // send an UDP packet in every 1 second
 if ((millis() - time_point) > 1000) {
 time_point = millis();

 snprintf(packet, sizeof(packet), "\r\nI'm WiFly NO %d. My IP is %s. I'm living\r\n", id, ip);
 wifly.send(packet);
 }
}

PerlでArduino SerialPortを扱う

perlのモジュール,Device::SerialPort::Arduino

をインストール

https://github.com/syxanash/Device-SerialPort-Arduino

https://metacpan.org/pod/release/SYXANASH/Device-SerialPort-Arduino-0.07/lib/Device/SerialPort/Arduino.pm

インストール方法:

perl Makefile.PL
make
make test
make install
use Device::SerialPort::Arduino;

my $Arduino = Device::SerialPort::Arduino->new(
 port => '/dev/ttyACM0',
 baudrate => 9600,

databits => 8,
 parity => 'none',
 );

# Reading from Arduino via Serial

while (1) {
 print $Arduino->receive(), "\n";
}

# or with a delay

while (1) {
 print $Arduino->receive(1), "\n";
}

# Send something via Serial

$Arduino->communicate('oh hi!!11')
or die 'Warning, empty string: ', "$!\n";

arduino wifi shield “WLS06201P” の 使い方 -ハードの組み立てからUDP通信まで-

Arduino WiFi Shield “WLS06201P”

<ハードの組み立て>

1.ヘッダをハンダ付けし,組み立てる

2.ジャンパーを2箇所設定(rxとtxのピンをarduinoの何番目のデシタルポートにするかを決める)

<ライブラリ>
https://github.com/Seeed-Shield/WiFi_Shield

<UDP通信のサンプルソース>

#include <Arduino.h>
#include <SoftwareSerial.h>
#include "WiFly.h"

#define SSID "xxxxxxxxxx"
#define KEY "yyyyyyyyyyy"

// WIFLY_AUTH_OPEN / WIFLY_AUTH_WPA1 / WIFLY_AUTH_WPA1_2 / WIFLY_AUTH_WPA2_PSK
#define AUTH WIFLY_AUTH_WPA2_PSK
//#define AUTH WIFLY_AUTH_WEP
#define UDP_HOST_IP "255.255.255.255" // broadcast
#define UDP_REMOTE_PORT 5000
#define UDP_LOCAL_PORT 5000

// Pins' connection
// Arduino WiFly
// 2 <----> TX
// 3 <----> RX
SoftwareSerial uart(2, 3);
WiFly wifly(uart);

void setupUDP(const char *host_ip, uint16_t remote_port, uint16_t local_port)
{
 char cmd[32];

 wifly.sendCommand("set w j 1\r", "AOK"); // enable auto join

 wifly.sendCommand("set i p 1\r", "AOK");
 snprintf(cmd, sizeof(cmd), "set i h %s\r", host_ip);
 wifly.sendCommand(cmd, "AOK");
 snprintf(cmd, sizeof(cmd), "set i r %d\r", remote_port);
 wifly.sendCommand(cmd, "AOK");
 snprintf(cmd, sizeof(cmd), "set i l %d\r", local_port);
 wifly.sendCommand(cmd, "AOK");
 wifly.sendCommand("save\r");
 wifly.sendCommand("reboot\r");
}

void setup() {

Serial.begin(9600);
 Serial.println("--------- WIFLY UDP --------");

 uart.begin(9600); // WiFly UART Baud Rate: 9600
 wifly.reset();

 while (1) {
 Serial.println("Try to join " SSID );
 if (wifly.join(SSID, KEY, AUTH)) {
 Serial.println("Succeed to join " SSID);
 wifly.clear();
 break;
 } else {
 Serial.println("Failed to join " SSID);
 Serial.println("Wait 1 second and try again...");
 delay(1000);
 }
 }

 setupUDP(UDP_HOST_IP, UDP_REMOTE_PORT, UDP_REMOTE_PORT);

 delay(1000);
 wifly.clear();
}

unsigned int time_point = 0;

void loop() {
 int c;
 while (wifly.available()) {
 c = wifly.read();
 if (c > 0) {
 Serial.write((char)c);
 }
 }

 // send an UDP packet in every 10 second
 if ((millis() - time_point) > 10000) {
 time_point = millis();

 wifly.send("I'm wifly, I'm living\r\n");
 }
}

ios 傾き検出(加速度センサ)

<.m>


#import <CoreMotion/CoreMotion.h>

#import <math.h>

@interface testViewController ()

@property (strong, nonatomic) CMMotionManager *motionManager;

@property (weak, nonatomic) IBOutlet UILabel *pitchLabel;

@property (weak, nonatomic) IBOutlet UILabel *rollLabel;

@property (weak, nonatomic) IBOutlet UILabel *yawLabel;

@end

@implementation testViewController

- (void)viewDidLoad

{

[super viewDidLoad];

_motionManager = [[CMMotionManager alloc] init];

[self attiude];

}

- (void)attiude

{

if (self.motionManager.deviceMotionAvailable) {

// 向きの更新通知を開始する

[self.motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue]

withHandler:^(CMDeviceMotion *motion, NSError *error)

{

NSLog(@"%f ",motion.attitude.pitch * 180 / M_PI);

self.pitchLabel.text

= [NSString stringWithFormat:@"pitch:%f", motion.attitude.pitch * 180 / M_PI];

self.rollLabel.text

= [NSString stringWithFormat:@"roll:%f", motion.attitude.roll * 180 / M_PI];

self.yawLabel.text

= [NSString stringWithFormat:@"yaw:%f", motion.attitude.yaw * 180 / M_PI];

}];

}

}


@end

ios UILabelの書き方

.mファイルにのみ宣言して,あとはstory boardと線でつなげる。

<.m>


@interface testViewController ()

@property (weak, nonatomic) IBOutlet UILabel *pitchLabel;

@property (weak, nonatomic) IBOutlet UILabel *rollLabel;

@property (weak, nonatomic) IBOutlet UILabel *yawLabel;

@end