Japanese Site
inquiry

ESP8266 UDP for SG90 youtube

Please use it to operate the SG90 series servo motor connected to the ESP8266 from your iPhone.

PrivacyPolicy English
This application is for controlling the SG90 servo connected to the Arduino ESP8266 family. Therefore, an Arduiono ESP8266 and SG90 servo are required in advance, and a program must be written to the ESP8266. Please refer to the source code for program samples. Make the ESP8266 compatible with UDP reception
A servo is attached to IO 5.
Starts as a wifi access point with SSID ESP8266 password 12345678
After connecting, use the iPhone app to access normally 192.168.4.1 port 8888

Currently applying for UDP access app for iPhone
Click here for the old LED app
From here


Source code





#include 
#include 


WiFiUDP UDP;
unsigned int localPort = 8888;

const char* ssid = "ssid";
const char* password = "password";


const char *ap_ssid = "ESP8266";
const char *ap_password = "12345678";

char packetBuffer[255];
static const char *udpReturnAddr = "1.1.1.1";
static const int udpReturnPort = 8889;
void motor_servo(int port,int motor_speed, int seconds) {
Serial.println("motor_servo");
const int SERVO_PIN = port;

  int pulse_width = 1500-motor_speed*8;
  int starttime = millis();
  while(true) {
    if (millis()-starttime > seconds) break;
      digitalWrite(SERVO_PIN, HIGH);
      delayMicroseconds(pulse_width);
      digitalWrite(SERVO_PIN, LOW);
      delay(20);
  }
}


void setup() {
    
    Serial.begin(115200);
    Serial.println(""); // to separate line

// WiFi.mode(WIFI_AP);  // モード設定
//   WiFi.softAP(ap_ssid, ap_password);  // APのSSID・パスワード設定
//  IPAddress myIP = WiFi.softAPIP();   // APとしてのIPアドレスを取得。デフォルトは  192.168.4.1 ?
//  Serial.println("AP IP address: ");
//  Serial.println(myIP);

 // AP+STAモードの設定
  WiFi.mode(WIFI_AP_STA);

  // APとして振る舞うためのSSIDとPW情報
  WiFi.softAP(ap_ssid, ap_password);

  // STAとして振る舞うための接続先APのSSIDとPW情報
  WiFi.begin(ssid, password);

  // APへの接続完了待ち
  while (WiFi.status() != WL_CONNECTED) {
    delay(100);
    Serial.print(". ");
  }
  Serial.println("connected!");

  IPAddress myIP = WiFi.softAPIP();   // APとしてのIPアドレスを取得。デフォルトは  192.168.4.1 ?
  Serial.println("AP IP address: ");
  Serial.println(myIP);
 Serial.println(WiFi.localIP());



UDP.begin(localPort);

}

void loop()
{

    int packetSize = UDP.parsePacket();
    
    if (packetSize) {
        
        int len = UDP.read(packetBuffer, packetSize);
        //終端文字設定
        if (len > 0) packetBuffer[len] = '\0';
        
        Serial.print(UDP.remoteIP());
        Serial.print(" / ");
        Serial.println(packetBuffer);
  
    int data[5];
    stringToIntValues( packetBuffer, data, ',' );

  Serial.print(data[0]);
  Serial.print(" :  ");
  Serial.print(data[1]);
  Serial.print(" :  ");
  Serial.print(data[2]);
  Serial.println("");

  String s;

  s = data[0]; //packetBuffer;
 if(data[0] > 0){
   motor_servo(data[0],data[1],data[2]);
  }
        UDP.beginPacket(udpReturnAddr, udpReturnPort);
        UDP.write("ok");
        UDP.endPacket();
        
    }
    
    
    delay(3000);
}
// String型のカンマ区切り文字列をint型配列に分解する関数
void stringToIntValues(String str, int value[], char delim) {
  int k = 0;
  int j = 0;
  char text[8];

  for (int i = 0; i <= str.length(); i++) {
    char c = str.charAt(i);
    if ( c == delim || i == str.length() ) {
      text[k] = '\0';
      value[j] = atoi(text);
      j++;
      k = 0;
    } else {
      text[k] = c;
      k++;
    }
  }
}