3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

トーマスをESP-WROOM-02で

Last updated at Posted at 2017-03-14

esp8266で子供のおもちゃをつくりました。

#部品リスト

  1. ESP-WROOM-02 https://www.switch-science.com/catalog/2347/

  2. DRV8830(モータードライバ)http://akizukidenshi.com/catalog/g/gK-06273/

  3. 抵抗、圧電スピーカー、電池ボックス、LED

  4. 3端子レギュレーター TA48033S

  5. トーマスのおもちゃ http://store.shopping.yahoo.co.jp/toysrus-babierus/482307900.html?sc_e=slga_pla

#スケッチ
どこでも使えるように、ESP-WROOM-02はAPモードで動かします

gotohmas.ino
#include <ESP8266WiFi.h>
#include <WiFiClient.h> 
#include <ESP8266WebServer.h>
#include <Wire.h>

const char *ssid = "****";//任意のSSIDを
const char *password = "****";//任意のパスワードを
const int led=13;
const int bz=12;
const int drv8830=0x60;
int l=0; 

ESP8266WebServer server(80);

String form="<html><head>"
"<meta name=viewport content=width=100>"
"<style type=text/css>"
"input.stop{width:100px;font-size:24px;font-weight:bold;tet-decoration:none;display:block;text-align:center;padding:8px 0 20px;color:#fff;background-color:#ff7f50;border-radius:5px;box-shadow:2px 2px #1a6ea0;text-shadow:0 -1px #1a6ea0;}"
"input.move{width:100px;font-size:24px;font-weight:bold;tet-decoration:none;display:block;text-align:center;padding:8px 0 20px;color:#fff;background-color:#49a9d4;border-radius:5px;box-shadow:2px 2px #1a6ea0;text-shadow:0 -1px #1a6ea0;}"
"input.buzzer{width:100px;font-size:24px;font-weight:bold;tet-decoration:none;display:block;text-align:center;padding:8px 0 20px;color:#fff;background-color:#ffb700;border-radius:5px;box-shadow:2px 2px #1a6ea0;text-shadow:0 -1px #1a6ea0;}"
"input.led{width:100px;font-size:24px;font-weight:bold;tet-decoration:none;display:block;text-align:center;padding:8px 0 20px;color:#fff;background-color:#b22222;border-radius:5px;box-shadow:2px 2px #1a6ea0;text-shadow:0 -1px #1a6ea0;}"
"</style>"
"</head>"
"<body>"
"<table border=0>"
"<tr><td></td><td><form action=FD><input type=submit value=forward class=move></form></td><td></td></tr>"
"<tr><td><form action=TL><input type=submit value=left class=move></form></td>"
"<td><form action=ST><input type=submit value=stop class=stop></form></td>"
"<td><form action=TR><input type=submit value=right class=move></form></td></tr>"
"<tr><td></td><td><form action=BK><input type=submit value=back class=move></form></td><td></td></tr>"
"</table><table>"
"<tr><td><form action=BZ><input type=submit value=buzzer class=buzzer></form></td><td></td><td>";

String ledon="<form action=LEDON><input type=submit value=LED-ON class=led></form></td></tr></table>"
"</body></html>";

String ledoff="<form action=LEDOFF><input type=submit value=LED-OFF class=led></form></td></tr></table>"
"</body></html>";

String leds="";
/* Just a little test message.  Go to http://192.168.4.1 in a web browser
 * connected to this access point to see it.
 */

void writeRegister(byte reg,byte vset,byte data){
  int vdata=vset << 2 | data;
  Wire.beginTransmission(drv8830);
  Wire.write(reg);
  Wire.write(vdata);
  Wire.endTransmission(true); 
}


void setup() {
  delay(2000);
  Wire.begin();
 //13pin is led
  pinMode(led,OUTPUT);
 //12pin in buzzer
  pinMode(bz,OUTPUT);
  Serial.begin(115200);
  Serial.println("start");
  start_melody();
  Serial.println();
  Serial.print("Configuring access point...");
  WiFi.softAP(ssid, password);

  IPAddress myIP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(myIP);
  server.on("/", handleRoot);
  server.on("/ST", handle_stop);
  server.on("/FD", handle_forward);
  server.on("/TR", handle_turn_right);
  server.on("/TL", handle_turn_left);
  server.on("/BK", handle_back);
  server.on("/BZ",handle_buzzer);
  server.on("/LEDON",handle_ledon);
  server.on("/LEDOFF",handle_ledoff);
  server.begin();
  Serial.println("HTTP server started");
}

void loop() {
	server.handleClient();
}

void handleRoot(){
  if (l==0){
    leds=ledon;
  }
  else{
    leds=ledoff;
  }
  server.send(200,"text/html",form+leds);
}

void handle_stop(){
  if (l==0){
    leds=ledon;
  }
  else{
    leds=ledoff;
  }
  Serial.println("STOP");
  ST_ACT();
  server.send(200,"text/html",form+leds);
}

void handle_forward(){
  if (l==0){
    leds=ledon;
  }
  else{
    leds=ledoff;
  }
  Serial.println("FORWARD");
  FD_ACT();
  server.send(200,"text/html",form+leds);
}


void handle_turn_right(){
  if (l==0){
    leds=ledon;
  }
  else{
    leds=ledoff;
  }
  Serial.println("RIGHT");
  TR_ACT();
  server.send(200,"text/html",form+leds);
}

void handle_turn_left(){
  if (l==0){
    leds=ledon;
  }
  else{
    leds=ledoff;
  }
  Serial.println("LEFT");
  TL_ACT();
  server.send(200,"text/html",form+leds);
}

void handle_back(){
  if (l==0){
    leds=ledon;
  }
  else{
    leds=ledoff;
  }
  Serial.println("BACK");
  BK_ACT();
  server.send(200,"text/html",form+leds);
}

void handle_buzzer(){
  if (l==0){
    leds=ledon;
  }
  else{
    leds=ledoff;
  }
  Serial.println("BUZZER ON");
  BZ_ACT();
  server.send(200,"text/html",form+leds); 
}

void handle_ledon(){
  Serial.println("LED ON");
  LEDON_ACT();
  l=1;
  server.send(200,"text/html",form+ledoff); 
}

void handle_ledoff(){
  Serial.println("LED OFF");
  LEDOFF_ACT();
  l=0;
  server.send(200,"text/html",form+ledon); 
}

void ST_ACT(){
  Serial.println("motor STBY");
  writeRegister(0x00,0x00,0x00);
  delay(200);
}

void FD_ACT(){
  Serial.println("motor FORWARD");
  writeRegister(0x00,0x20,0x01);
}

void TR_ACT(){
  Serial.println("motor RIGHIT");
  ST_ACT();
  RE_ACT();
  delay(900);
  FD_ACT();  
}

void BK_ACT(){
  Serial.println("motor back");
  ST_ACT();
  RE_ACT();
  delay(1300);
  FD_ACT();  
}

void TL_ACT(){
  Serial.println("motor left");
  ST_ACT();
  RE_ACT();
  delay(1700);
  FD_ACT();   
}

void RE_ACT(){
  Serial.println("motor REVERCE");
  writeRegister(0x00,0x1F,0x02);
  delay(20);  
}

void BZ_ACT(){
  tone(bz,175,500);
  delay(500);
  digitalWrite(bz,LOW);
}
void LEDON_ACT(){
  digitalWrite(led,HIGH);
}

void LEDOFF_ACT(){
  digitalWrite(led,LOW);
}

void start_melody(){
  Serial.println("start Melody");
  tone(bz,175,200);
  delay(200);
  tone(bz,147,200);
  delay(200);
  tone(bz,175,200);
  delay(200);
  tone(bz,147,200);
  delay(200);
  tone(bz,196,200);
  delay(200);
  tone(bz,147,200);
  delay(200);
  digitalWrite(bz,LOW);
}

このトーマスは、前進とバックしかできません。
バックする時に方向が変わるという仕組みですので左折・右折に関しては
バックしている時間によってそちらの方向を向くようにしています。
(実測で数値入れましたが、走らせる場所によって著しく方向が著しくかわります。。
ジャイロセンサーとか入れればもう少しうまくできるかも。。)

メロディーに関しては、電車の発車メロディーを参考にしたのですが
音楽的素養がないので不穏な音が鳴ります。。

#回路図
手書きですいません。
回路図.png

プルアップ抵抗とか極力入れてません。(ものぐさ)

#実機
17202981_1245499995534047_7041526787815068222_n.jpg

17155482_1245500012200712_1551528544428234529_n.jpg

#参考にさせていただきました。
https://tech-blog.cerevo.com/archives/1022/
http://makers-with-myson.blog.so-net.ne.jp/2014-05-15

3
3
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?