2022年7月19日火曜日

EPS32 WiFi Car


 






//esp32_wrover_ftmc004_wifi

//Thanks to mukujii.sakura.ne.jp/esp1.html

//Thanks to www.freenove.com

//Thanks to https://akizukidenshi.com/download/ds/akizuki/ft-mc-002_004_appendix.pdf

#include <WiFi.h>

#include <ESP32Servo.h>

Servo RightServo;

Servo LeftServo;

Servo WiperServo;


int pos=0;

int RightPin=12;

int LeftPin=14;

int intv=150;

int WiperPin=18;

int HazardPin=13;


const char* ssid     = "";      

const char* password = "";   

const char html[] =

"<!DOCTYPE html><html lang='ja'><head><meta charset='UTF-8'>\

<style>input {font-size:16pt;margin:16px;width:160px;}\

div {font-size:32pt;color:blue;text-align:center;width:600px;border:medium solid black;}</style>\

<title>EPS32 WiFi_Car</title></head>\

<body><div><p>4WD Car Controller</p>\

<form method='get'>\

<input type='submit' name='le' value='Left' />\

<input type='submit' name='fo' value='Forward' />\

<input type='submit' name='ri' value='Right' /><br>\

<input type='submit' name='hz' value='Hazard' />\

<input type='submit' name='st' value='Stop' /> \

<input type='submit' name='wp' value='Wiper' /><br>\

<input type='submit' name='lb' value='LeftBack' />\

<input type='submit' name='ba' value='Back' />\

<input type='submit' name='rb' value='RightBack' /><br><br>\

</form></div></body></html>";


WiFiServer server(80);


void ServoSet()

{

  RightServo.attach(RightPin,1000,2000);  

  LeftServo.attach(LeftPin,1000,2000);   

}


void Forward()

{

  ServoSet();

  for (pos = 10; pos <= 170; pos += 1) 

  { 

    RightServo.write(pos); 

    LeftServo.write(pos);                

  }

}


void MotorStop()

{

  RightServo.detach();

  LeftServo.detach(); 

}


void Back()

{

  ServoSet();

  for (pos = 170; pos >= 10; pos -= 1) 

  { 

    RightServo.write(pos); 

    LeftServo.write(pos);                

  }  

}


void Right()

{

  ServoSet();

  RightServo.detach();

  for (pos = 10; pos <= 170; pos += 1) 

  { 

    LeftServo.write(pos);                

  }

  delay(intv);

  MotorStop();                 

}


void Left()

{

  ServoSet();

  LeftServo.detach();

  for (pos = 10; pos <= 170; pos += 1) 

  { 

    RightServo.write(pos);                

  }

  delay(intv);

  MotorStop();                 

}


void RightBack()

{

  ServoSet();

  RightServo.detach();

  for (pos = 170; pos >= 10; pos -= 1) 

  { 

    LeftServo.write(pos);                

  }

  delay(intv);

  MotorStop();                 

}


void LeftBack()

{

  ServoSet();

  LeftServo.detach();

  for (pos = 170; pos >= 10; pos -= 1) 

  { 

    RightServo.write(pos);                

  }

  delay(intv);

  MotorStop();                 

}


void Hazard()

{

    for (int i=1;i<=5;i +=1)

  {

    digitalWrite(HazardPin,HIGH);

    delay(300);

    digitalWrite(HazardPin,LOW);

    delay(300);

  }                      

}


void Wiper()

{

    for (int i=1;i<=5;i +=1)

  {

    for (pos = 20; pos <= 160; pos += 10) 

    { 

      WiperServo.write(pos); 

      delay(15);               

    } 

   for (pos = 160; pos >= 20; pos -= 10) 

    { 

      WiperServo.write(pos); 

      delay(15);               

    }

  }                      

}



void setup()

{

  pinMode(HazardPin,OUTPUT);

  Serial.begin(115200);

  RightServo.attach(RightPin,1000,2000);  

  LeftServo.attach(LeftPin,1000,2000);

  WiperServo.attach(WiperPin,1000,2000);  

  delay(10);


  Serial.println();

  Serial.println();

  Serial.print("Connecting to ");

  Serial.println(ssid);


   WiFi.begin(ssid, password);


   while (WiFi.status() != WL_CONNECTED) {

      delay(500);

      Serial.print(".");

   }


   Serial.println("");

   Serial.println("WiFi connected.");

   Serial.println("IP address: ");

   Serial.println(WiFi.localIP());


   server.begin();

}


void loop(){

   WiFiClient client = server.available();


   if (client) {

      Serial.println("New Client.");

      String currentLine = "";

      while (client.connected()) {

         if (client.available()) {

            char c = client.read();

            Serial.write(c);

               if (c == '\n') {

                  if (currentLine.length() == 0) {

                     client.println("HTTP/1.1 200 OK");

                     client.println("Content-type:text/html");

                     client.println();


                     client.print(html);

                     client.println();

                     break;

                  } else {

                     currentLine = "";

                  }

               } else if (c != '\r') {

                  currentLine += c;

               }


            if (currentLine.endsWith("GET /?fo")) {

              Forward();              

            }

            if (currentLine.endsWith("GET /?le")) {

              Left();

            }

            if (currentLine.endsWith("GET /?ri")) {

              Right();

            }

            if (currentLine.endsWith("GET /?ba")) {

              Back();

            }

            if (currentLine.endsWith("GET /?lb")) {

              LeftBack();

            }

            if (currentLine.endsWith("GET /?rb")) {

              RightBack();

            }

            if (currentLine.endsWith("GET /?st")) {

               MotorStop();

            }

            if (currentLine.endsWith("GET /?hz")) {

               Hazard();

            }

            if (currentLine.endsWith("GET /?wp")) {

               Wiper();

            }           

         }

      }

      client.stop();

      Serial.println("Client Disconnected.");

   }

}

2022年7月11日月曜日

LearningKit 2022 for all ESP32,ESP8266


 

//all_esp32wroverdev_wiper_lcd

/**********************************************************************

  Filename    : Servo Sweep

  Description : Control the servo motor for sweeping

  Auther      : www.freenove.com

  Modification: 2020/07/11

**********************************************************************/

#include <ESP32Servo.h>

#include <LiquidCrystal_I2C.h>

#include <Wire.h>


#define SDA 13

#define SCL 14


Servo myservo;  


int posVal = 0;    

int servoPin = 25; 


LiquidCrystal_I2C lcd(0x27,16,2);


void setup() {

  myservo.setPeriodHertz(50);           

  myservo.attach(servoPin, 500, 2500);   


  Wire.begin(SDA,SCL);

  lcd.init();

  lcd.backlight();

  lcd.setCursor(0,0);

  lcd.print("Hello,world");

  lcd.setCursor(0,1);

  lcd.print("I am a ESP32");

  delay(100);

}

void loop() {


  for (posVal = 20; posVal <= 160; posVal += 5) { 

    myservo.write(posVal);      

    delay(15);  

  }    

  for (posVal = 160; posVal >= 20; posVal -= 5) { 

    myservo.write(posVal);       

    delay(15);                   

  }

}



 //all_esp8266_wiper_lcd

 #include <Servo.h> // servo library  

 #include<LiquidCrystal_I2C.h>

 int interval = 10;

 Servo s1;


 int lcdColumns = 16;

 int lcdRows = 2;


 LiquidCrystal_I2C lcd(0x27,lcdColumns,lcdRows);

 

 void setup()  

 {   

  s1.attach(0);  // servo attach D3 pin of arduino  

  lcd.init();

  lcd.backlight();

  lcd.setCursor(0,0);

  lcd.print("Hello,world!");

  lcd.setCursor(0,1);

  lcd.print("I am a ESP8266");

  delay(100);

  

 }  

 void loop()   

 {  

   s1.write(20);  

   delay(interval);  

   s1.write(30);  

   delay(interval);  

   s1.write(40);  

   delay(interval);

   s1.write(50);  

   delay(interval);

   s1.write(60);  

   delay(interval);

   s1.write(70);  

   delay(interval);

   s1.write(80);  

   delay(interval);

   s1.write(90);  

   delay(interval); 

   s1.write(100);  

   delay(interval);

   s1.write(110);  

   delay(interval);  

   s1.write(120);  

   delay(interval);  

   s1.write(110);  

   delay(interval);

   s1.write(100);  

   delay(interval);

   s1.write(90);  

   delay(interval);

   s1.write(80);  

   delay(interval);

   s1.write(70);  

   delay(interval);

   s1.write(60);  

   delay(interval); 

   s1.write(50);  

   delay(interval);

   s1.write(40);  

   delay(interval);

   s1.write(30);  

   delay(interval); 

 }  

2022年7月10日日曜日

LearningKit 2022 for all Pico


 

# all_pico_wiper_lcd.py

# red-pin40 VBUS brown-GND orange-pin0

from machine import Pin, PWM

import utime

from esp8266_i2c_lcd import I2cLcd


pwm = PWM(Pin(2))

pwm.freq(50)


I2C_ADDR = 0x27

sda=Pin(0)

scl=Pin(1)


def servo_value(degree):

    return int((degree * 9.5 / 180 + 2.5) * 65535 / 100)


              

while True:

    i2c=machine.I2C(0,sda=sda,scl=scl,freq=400000)

    lcd=I2cLcd(i2c,I2C_ADDR,2,16)

    lcd.putstr("Hello,world!\n I am a pico")

    utime.sleep(0.1)  

    

    pwm.duty_u16(servo_value(20))

    utime.sleep(1)

    pwm.duty_u16(servo_value(160))

    utime.sleep(1)


LearningKit 2022 for all Arduino


 //all_uno_wiper_lcd

#include <Servo.h>

Servo myservo;

int interval=50;


#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);


void setup() {

  myservo.attach(5);


  lcd.init();

  lcd.backlight();

  lcd.clear();

  lcd.setCursor(0,0);

  lcd.print("Hello,world!");

  lcd.setCursor(0,1);

  lcd.print("I am a UNO"); 

}


void loop() {

  

  for(int i=20;i<165;i=i+10)

  {

    myservo.write(i);

    delay(interval); 

  }

 

  for(int i=160;i<25;i=i-10)

  {

   myservo.write(i);

    delay(interval); 

  }

}



//all_nano_wiper_lcd

#include <Servo.h>

Servo myservo;

int interval=50;


#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);


void setup() {

  myservo.attach(5);


  lcd.init();

  lcd.backlight();

  lcd.clear();

  lcd.setCursor(0,0);

  lcd.print("Hello,world!");

  lcd.setCursor(0,1);

  lcd.print("I am a nano"); 

}


void loop() {

  

  for(int i=20;i<165;i=i+10)

  {

    myservo.write(i);

    delay(interval); 

  }

 

  for(int i=160;i<25;i=i-10)

  {

   myservo.write(i);

    delay(interval); 

  }

}




//all_mega_wiper_lcd
#include <Servo.h>
Servo myservo;
int interval=50;

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);

void setup() {
  myservo.attach(5);

  lcd.init();
  lcd.backlight();
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Hello,world!");
  lcd.setCursor(0,1);
  lcd.print("I am a MEGA"); 
}

void loop() {
  
  for(int i=20;i<165;i=i+10)
  {
    myservo.write(i);
    delay(interval); 
  }
 
  for(int i=160;i<25;i=i-10)
  {
   myservo.write(i);
    delay(interval); 
  }

}


2022年7月5日火曜日

LearningKit 2022 for esp32 wrover dev

/* WiFi-Control-Car */  

//esp32_makerdrive

//MakerDrive M1A-pin0 M1B-pin2 M2A-pin4 M2B-pin5

//Thanks to http://mukujii.sakura.ne.jp/esp1.html


#include <WiFi.h>


const char* ssid     = "";      

const char* password = "";    

const char html[] =

"<!DOCTYPE html><html lang='ja'><head><meta charset='UTF-8'>\

<style>input {margin:8px;width:80px;}\

div {font-size:16pt;color:red;text-align:center;width:400px;border:groove 40px orange;}</style>\

<title>WiFi_Car Controller</title></head>\

<body><div><p>Tank Controller</p>\

<form method='get'>\

<input type='submit' name='le' value='左' />\

<input type='submit' name='fo' value='前' />\

<input type='submit' name='ri' value='右' /><br>\

<input type='submit' name='st' value='停止' /><br>\

<input type='submit' name='bl' value='後左' />\

<input type='submit' name='ba' value='後ろ' />\

<input type='submit' name='br' value='後右' /><br><br>\

</form></div></body></html>";


void stop(){

   digitalWrite(0, LOW);

   digitalWrite(2, LOW);

   digitalWrite(4, LOW);

   digitalWrite(5, LOW);

}


WiFiServer server(80);


void setup()

{

   Serial.begin(115200);

   pinMode(0, OUTPUT);

   pinMode(2, OUTPUT);

   pinMode(4, OUTPUT);

   pinMode(5, OUTPUT);


   delay(10);


   Serial.println();

   Serial.println();

   Serial.print("Connecting to ");

   Serial.println(ssid);


   WiFi.begin(ssid, password);


   while (WiFi.status() != WL_CONNECTED) {

      delay(500);

      Serial.print(".");

   }


   Serial.println("");

   Serial.println("WiFi connected.");

   Serial.println("IP address: ");

   Serial.println(WiFi.localIP());


   server.begin();


}


void loop(){

   WiFiClient client = server.available();


   if (client) {

      Serial.println("New Client.");

      String currentLine = "";

      while (client.connected()) {

         if (client.available()) {

            char c = client.read();

            Serial.write(c);

               if (c == '\n') {

                  if (currentLine.length() == 0) {

                     client.println("HTTP/1.1 200 OK");

                     client.println("Content-type:text/html");

                     client.println();


                     client.print(html);

                     client.println();

                     break;

                  } else {

                     currentLine = "";

                  }

               } else if (c != '\r') {

                  currentLine += c;

               }


            if (currentLine.endsWith("GET /?fo")) {

               stop();

               digitalWrite(0,HIGH);

               digitalWrite(2,LOW);

               digitalWrite(4,HIGH);

               digitalWrite(5,LOW);

            }

            if (currentLine.endsWith("GET /?le")) {

               stop();

               digitalWrite(0,HIGH);

               digitalWrite(2,LOW);

               digitalWrite(4,LOW);

               digitalWrite(5,LOW);

            }

            if (currentLine.endsWith("GET /?ri")) {

               stop();

               digitalWrite(0,LOW);

               digitalWrite(2,LOW);

               digitalWrite(4,HIGH);

               digitalWrite(5,LOW);

            }

            if (currentLine.endsWith("GET /?ba")) {

               stop();

               digitalWrite(0,LOW);

               digitalWrite(2,HIGH);

               digitalWrite(4,LOW);

               digitalWrite(5,HIGH);

            }

            if (currentLine.endsWith("GET /?bl")) {

               stop();

               digitalWrite(0,LOW);

               digitalWrite(2,HIGH);

               digitalWrite(4,LOW);

               digitalWrite(5,LOW);

            }

            if (currentLine.endsWith("GET /?br")) {

               stop();

               digitalWrite(0,LOW);

               digitalWrite(2,LOW);

               digitalWrite(4,LOW);

               digitalWrite(5,HIGH);

            }

            if (currentLine.endsWith("GET /?st")) {

               stop();

            }

         }

      }

      client.stop();

      Serial.println("Client Disconnected.");

   }

}