วันอาทิตย์ที่ 30 กันยายน พ.ศ. 2561

Mini project


Ethernet + Blynk ปิด เปิด ไฟ LED ผ่าน อินเตอร์เน็ต





จัดทำโดย
1.นายภัทระ      อุ่นเจ้าบ้าน  รหัส 6131271132
2.นายปวเรศ    จาตุกรณีย์   รหัส 6131271136

อุปกรณ์

1.Arduino R3
2.แผ่นไม้อัด
3.LED
4.Ethernet Shield W5100
5.บอร์ด PCB
6.Ethernet Cables


Book diagram



Wiring diagram


                                                       แอปพลิเคชัน

Blynk Application เป็นโปรแกรมบนมือถือที่ทำให้เราสร้างหน้าต่างควบคุมหรือแสดงผลเชื่อมต่อกับพวกไมโครคอนโทรเลอร์(Ardunio, ESP8266, Raspberry Pi) ได้ง่ายๆ และยังสามารถควบคุมผ่าน ทุกที่ ที่สามารถเชื่อมต่ออินเตอร์เน็ต  ได้อีกด้วย

ไปที่ Play Store ค้นหา Blynk

เปิด Blynk Application ขึ้นมา

-> Create New Account



กรอก อีเมล และ พาสเวิด -> Sign Up


-> New Project

เลือก DEVICE

ตั้งค่า ดังนี้

your hardware เป็น Arduino UNO

connection type เป็น Ethernet
ตั้งชื่อ Project ในตัวอย่างชื่อ W5100 แล้วจึงคลิก Create


โปรกรม จะส่ง Auth Toke ไปที่ อีเมล ที่เรากรอกไว้ -> OK


โดยทุกๆครั้งที่เริ่มสร้างโปรเจคใหม่ AUTH TOKEN จะถูกเปลี่ยนใหม่เสมอ ซึ่ง KEY นี้เองที่เป็นเสมือนกุญแจสำหรับเชื่อมต่อ คลิก เครื่องหมาย +


คลิก เพิ่ม Button

จะมี ปุ่ม BUTTON เพิ่มเข้ามาที่หน้าจอ แล้วจึง คลิกที่ BUTTON เพื่อตั้งค่า


เลือก เป็นแบบ SWITCH แล้ว คลิก ที่ PIN


Select pin -> Digital -> D7 -> CONTINUE


BUTTON จะอยู่ริมซ้ายมือบน


ไปที่

http://www.blynk.cc/getting-started/

ดาวน์โหลด ไลบรารี่ Blynk


เปิด โปรแกรม Arduino IDE จากนั้นให้ทำการเพิ่ม ไลบรารี่ Blynk  ให้กับ Arduino (IDE) ของเรา
การเพิ่ม ไลบรารี่ Blynk ให้กับ Arduino (IDE) ของเรา ตามรูป


เลือกไฟล์ ไลบรารี่ blynk-library-0.4.8.ZIP ที่เรา ดาวน์โหลดมาอยู่ในคอมพิวเตอร์ของเรา จากนั้นคลิก Open โปรแกรม จะเพิ่ม ไลบรารี่ Blynk เข้าสู่ Arduino (IDE) ของเรา


เมื่อเพิ่มเสร็จแล้วไปที่ File -> Eamples -> Blynk -> Boards_Ethernet -> Arduino_Ethernet


แก้ไข YourAuthToken ที่เราได้รับจาก อีเมล


ที่เราได้รับจาก อีเมล





เมื่อแก้ไขแล้ว อัพโหลดโค้ด ไปยัง  Arduino UNO



CODE


#define BLYNK_PRINT Serial #include <SPI.h> #include <Ethernet.h> #include <BlynkSimpleEthernet.h> // You should get Auth Token in the Blynk App. // Go to the Project Settings (nut icon). char auth[] = "18f89ec26be248018f0d629d17204350"; // ต้องแก้ไข
#อธิบายโค้ด ใช้เพื่อตั้งชื่อค่าคงที่ของw5100และsdcard ก่อนที่โปรแกรมจะถูกคอมไพล์ ค่าคงที่ที่ถูก define ใน Arduino จะไม่ใช้พื้นที่หน่วยความจำใดๆบนชิป
#define W5100_CS 10 #define W5100_CS 10คอมไพล์เลอร์จะแทนที่ W5100_CS ทุกที่ด้วย 10 เมื่อทำการคอมไพล์

#define SDCARD_CS 4 #define SDCARD_CS 4คอมไพล์เลอร์จะแทนที่ SDCARD_CS ทุกที่ด้วย 4 เมื่อทำการคอมไพล์ void setup() { // Debug console Serial.begin(9600); pinMode(SDCARD_CS, OUTPUT); digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card Blynk.begin(auth); // You can also specify server: //Blynk.begin(auth, "blynk-cloud.com", 8442); //Blynk.begin(auth, IPAddress(192,168,1,100), 8442); // For more options, see Boards_Ethernet/Arduino_Ethernet_Manual example } void loop() { Blynk.run(); }

ลิ้งทดลอง
https://www.youtube.com/watch?v=iZcU5wrAr8Q


แนวทางและที่มา

วันศุกร์ที่ 28 กันยายน พ.ศ. 2561

แก้สอบไม่ผ่าน


Hardware


ใช้ Arduino UNO ขนาด 8 บิต ATMega 328P ในโครงการเพื่อควบคุมส่วนประกอบต่างๆเช่นโมดูลบลูทู ธ และเครือข่ายรีเลย์
อุปกรณ์

  • Arduino UNO
  • HC – 05 Bluetooth Module
  • ตัวต้านทาน10 KΩ 
  • 20 KΩ Resistor
  • ตัวต้านทาน1 KΩ  X 4
  • ทรานซิสเตอร์ 2N2222 NPN  X 4
  • ไดโอด 1N4007 X 4
  • รีเรย์ 12 V  X 4
  • Prototyping board (Bread board)
  • สายต่อ
  • แหล่งจ่าย 12 V 
  • Smartphone or tablet (Bluetooth enabled)
INPUT

HC05 Bluetooth Module
Smartphone or tablet (Bluetooth enabled)

MCU
 
 Arduino Uno

OUTPUT

รีเรย์ 12V ,โหลด
SOFTWARE

#include <SoftwareSerial.h> //กำหนดเรียกใช้ใน ไลบรารี่ บลูทูธ
const int rxPin = 4; //กำหนดไปที่ rxPinขา4
const int txPin = 2; //กำหนดไปที่ txPinขา2         
SoftwareSerial mySerial(rxPin, txPin); //ไว้ใช้กำหนดค่าลงใน ไลบรารีบลูทูธ
const int Loads[] = {9, 10, 11, 12}; //ตัวแปรอาเรย์
int state = 0; //ตัวแปล state = 0
int flag = 0; //ตัวแปล flag = 0
void setup() //ทำหน้าที่ setup ตามจุดประสงค์ที่ต้องการ
for (int i=0;i<4;i++) //int=0<4และi+เรื่อยๆ
{
pinMode(Loads[i], OUTPUT); //สั่งกำหนดใช้ Load เก็บค่าตัวแปร อาเรย์ i เป็น OUTPUT
}

mySerial.begin(9600); //แสดงข้อมูลทางออกของ Serial Monitor
for (int i=0;i<4;i++) //int=0<4และi+เรื่อยๆ
{
digitalWrite(Loads[i], LOW); //Load[i]เป็นLOW
}
}
void loop()  //คำสั่งที่จะสั่งให้ทำงาน
{
if(mySerial.available() > 0) // if คำสั่งนี้สามารถต่อไปได้เรื่อยๆจนกว่าจะพอใจ
{
state = mySerial.read(); //stateการอ่านค่า
flag=0; //flag = 0
}
switch(state) //จะตรวจสอบว่าตัวแปรมีค่าเท่ากับ case ใด
{
case '0':digitalWrite(Loads[0], HIGH); //กำหนดcase 0 แสดงค่า Load อาเรย์ 0 ตั้งเป็น High
flag=1; // flag=1
break; //หยุดการทำงาน
case '1':digitalWrite(Loads[0], LOW); //กำหนดcase 1 แสดงค่า Load อาเรย์ 0 ตั้งเป็น LOW
flag=1; //flag=1
break; //หยุดการทำงาน
case '2':digitalWrite(Loads[1], HIGH); //กำหนดcase 2 แสดงค่า Load อาเรย์ 1 ตั้งเป็น HIGH
flag=1; //flag=1
break; //หยุดการทำงาน
case '3':digitalWrite(Loads[1], LOW); //กำหนดcase 3 แสดงค่า Load อาเรย์ 1 ตั้งเป็น LOW
flag=1; //flag=1
break; //หยุดการทำงาน
case '4':digitalWrite(Loads[2], HIGH); //กำหนดcase 4 แสดงค่า Load อาเรย์ 2 ตั้งเป็น HIGH
flag=1; //flag=1
break; //หยุดการทำงาน
case '5':digitalWrite(Loads[2], LOW); //กำหนดcase 5 แสดงค่า Load อาเรย์ 2 ตั้งเป็น LOW
flag=1; //flag=1
break; //หยุดการทำงาน
case '6':digitalWrite(Loads[3], HIGH); //กำหนดcase 6 แสดงค่า Load อาเรย์ 3 ตั้งเป็น HIGH
flag=1; //flag=1
break; //หยุดการทำงาน
case '7':digitalWrite(Loads[3], LOW); //กำหนดcase 7 แสดงค่า Load อาเรย์ 3 ตั้งเป็น LOW
flag=1; //flag=1
break; //หยุดทำงาน
case '8':digitalWrite(Loads[0], LOW); //กำหนดcase 8 แสดงค่า Load อาเรย์ 0 ตั้งเป็น LOW
digitalWrite(Loads[1], LOW); //กำหนดค่า Load อาเรย์ 1 เป็น LOW
digitalWrite(Loads[2], LOW); //กำหนดค่า Load อาเรย์ 2 เป็น LOW
digitalWrite(Loads[3], LOW); //กำหนดค่า Load อาเรย์ 3 เป็น LOW
flag=1; //flag=1
break; //หยุดการทำงาน
}


}

FLOWCHART





-ขอบคุณที่ให้โอกาสครับ ^_^


ตัวอย่างที่ 20


เครื่องจ่ายน้ำอัตโนมัติโดยใช้ Arduino

HARDWARE



SOFTWARE

#define trigger 9
#define echo 8
#define LED 13
#define MOSFET 12
 
 
float time=0,distance=0;
 
void setup()
{
Serial.begin(9600);
 pinMode(trigger,OUTPUT);
 pinMode(echo,INPUT);
 pinMode(LED,OUTPUT);
 pinMode(MOSFET,OUTPUT);
 delay(2000);
}
 
void loop()
{
 measure_distance();
 if(distance<10)
 {
   digitalWrite(LED,HIGH);digitalWrite(MOSFET,HIGH);
 }
 else
 {
   digitalWrite(LED,LOW);digitalWrite(MOSFET,LOW);
 }
 delay(500);
}
void measure_distance()
{
 digitalWrite(trigger,LOW);
 delayMicroseconds(2);
 digitalWrite(trigger,HIGH);
 delayMicroseconds(10);
 digitalWrite(trigger,LOW);
 delayMicroseconds(2);
 time=pulseIn(echo,HIGH);
 
 distance=time*340/20000;
}

ตัวอย่างที่ 19


Arduino Wattmeter: วัดแรงดันไฟฟ้ากระแสตรงและกำลังไฟ

HARDWARE



SOFTWARE

#include <LiquidCrystal.h>  //Default Arduino LCD Librarey is included 
int Read_Voltage  = A3;
int Read_Current  = A4;
const int rs = 3, en = 4, d4 = 8, d5 = 9, d6 = 10, d7 = 11; //Mention the pin number for LCD connection
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() {
  lcd.begin(16, 2); //Initialise 16*2 LCD
  lcd.print(" Arduino Wattmeter"); //Intro Message line 1
  lcd.setCursor(0, 1);
  lcd.print("  With Arduino  "); //Intro Message line 2
  delay(2000);
  lcd.clear();
}
void loop() {
 
 float Voltage_Value = analogRead(Read_Voltage);
 float Current_Value = analogRead(Read_Current);
 Voltage_Value = Voltage_Value * (5.0/1023.0) * 6.46;
 Current_Value = Current_Value * (5.0/1023.0) * 0.239;
 lcd.setCursor(0, 0);
 lcd.print("V="); lcd.print(Voltage_Value); 
 lcd.print("  "); 
 lcd.print("I=");lcd.print(Current_Value); 
 float Power_Value = Voltage_Value * Current_Value;
 lcd.setCursor(0, 1);
 lcd.print("Power="); lcd.print(Power_Value); 
 
delay(200);
}

ตัวอย่างที่ 18

การเชื่อมต่อ nRF24L01 กับ Arduino: การควบคุมเซอร์โวมอเตอร์

HARDWARE



SOFTWARE

Code for Transmitter Part:
/*Transmit POT value through NRF24L01 using Arduino
 *
 * Pin Conections
 *  CE - 7
    MISO - 12
    MOSI - 11
    SCK - 13
    CS - 8
    POT-A7
*/
#include <SPI.h>
#include "RF24.h"
RF24 myRadio (7, 8);
struct package
{
  int msg = 0;
};
byte addresses[][6] = {"0"};
typedef struct package Package;
Package data;
void setup()
{
  Serial.begin(9600);
  myRadio.begin();
  myRadio.setChannel(115);  //115 band above WIFI signals
  myRadio.setPALevel(RF24_PA_MIN); //MIN power low rage
  myRadio.setDataRate( RF24_250KBPS ) ;  //Minimum speed
  delay(500);
  Serial.print("Setup Initialized");
}
void loop()
{
  int Read_ADC = analogRead(A7);
  char servo_value = map (Read_ADC, 0, 1024, 0,180);
    if (servo_value>1)
    data.msg = servo_value;
    WriteData();
    delay(50);
// ReadData();
 //delay(200);
}
void WriteData()
{
  myRadio.stopListening();  //Stop Receiving and start transminitng
  myRadio.openWritingPipe( 0xF0F0F0F0AA); //Sends data on this 40-bit address
  myRadio.write(&data, sizeof(data));
Serial.print("\nSent:");
  Serial.println(data.msg);
  delay(300);
}
void ReadData()
{
myRadio.openReadingPipe(1, 0xF0F0F0F066); // Which pipe to read, 40 bit Address
  myRadio.startListening(); //Stop Transminting and start Reveicing
  if ( myRadio.available())
  {
    while (myRadio.available())
    {
      myRadio.read( &data, sizeof(data) );
    }
    Serial.print("\nReceived:");
    Serial.println(data.msg);
  }
}

Code for Receiver Part:

/*CE - 7
MISO - 12
MOSI - 11
SCK - 13
CS - 8
Recently tested with nano
*/
#include <SPI.h>
#include "RF24.h"
#include <Servo.h>
Servo myservo;
RF24 myRadio (7, 8); 
struct package
{
  int msg;
};
typedef struct package Package;
Package data;
byte addresses[][6] = {"0"}; 
void setup()
{
  Serial.begin(9600);
  myRadio.begin();
  myRadio.setChannel(115);  //115 band above WIFI signals
  myRadio.setPALevel(RF24_PA_MIN); //MIN power low rage
  myRadio.setDataRate( RF24_250KBPS ) ;  //Minimum speed
  myservo.attach(6);

  Serial.print("Setup Initialized");
  delay(500);
}
int Servo_value;
int Pev_servo_value;
void loop()
{
  ReadData();
  delay(50);

  Pev_servo_value = Servo_value;
  Servo_value = data.msg; 
  while (Pev_servo_value< Servo_value)
  {
  myservo.write(Pev_servo_value);
  Pev_servo_value++;
  delay(2);
  }
  while (Pev_servo_value> Servo_value)
  {
  myservo.write(Pev_servo_value);
  Pev_servo_value--;
  delay(2);
  }
   //data.msg = "nothing to send";
   //WriteData();
  // delay(50);
}
void ReadData()
{
  myRadio.openReadingPipe(1, 0xF0F0F0F0AA); //Which pipe to read, 40 bit Address
  myRadio.startListening(); //Stop Transminting and start Reveicing 
  if ( myRadio.available())
  {
    while (myRadio.available())
    {
      myRadio.read( &data, sizeof(data) );
    }
    Serial.print("\nReceived:");
    Serial.println(data.msg);
  }
}
void WriteData()
{
  myRadio.stopListening(); //Stop Receiving and start transminitng
  myRadio.openWritingPipe(0xF0F0F0F066);//Sends data on this 40-bit address
  myRadio.write(&data, sizeof(data));
  Serial.print("\nSent:");
  Serial.println(data.msg);
  delay(300);
}



ตัวอย่างที่ 17

หุ่นยนต์ติดตามวัตถุ Ultrasonic


HARDWARE

SOFTWARE

#include <Servo.h>

#include <NewPing.h>

// Motor A pins (enableA = enable motor, pinA1 = forward, pinA2 = backward)
int enableA = 3;
int pinA1 = 6;
int pinA2 = 7;

//Motor B pins (enabledB = enable motor, pinB2 = forward, pinB2 = backward)
int enableB = 5;
int pinB1 = 8;
int pinB2 = 9;

#define TRIGGER_PIN  A0 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     A1  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 25 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
Servo myservo;  // create servo object to control a servo
// a maximum of eight servo objects can be created

int pos = 23;
int distance = 0;
int stepsize = 5;

int detect = 0;


void setup() {
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.

  pinMode(enableA, OUTPUT);
  pinMode(pinA1, OUTPUT);
  pinMode(pinA2, OUTPUT);

  pinMode(enableB, OUTPUT);
  pinMode(pinB1, OUTPUT);
  pinMode(pinB2, OUTPUT);

  myservo.attach(10);  // attaches the servo on pin 10 to the servo object
  myservo.write(pos);

}
void loop() {

  enableMotors();
  analogWrite(enableA, 225);  // ปรับความเร็วหุ่นยนต์
  analogWrite(enableB, 225); // ปรับความเร็วหุ่นยนต์

  delay(30);                      // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  unsigned int uS = sonar.ping();
  // Send ping, get ping time in microseconds (uS).
  distance = uS / US_ROUNDTRIP_CM;

  if (distance <= 6) {
    // nothing detected
    detect = 0;
    // move left

   turnLeft(100);
    forward(1);


    pos = pos + stepsize;
    myservo.write(pos);
    if (pos > 200) {
      pos = 200;
    }
  }
  else {
    // something is detected
    detect = 1;
    // move right

    turnRight(100);
    forward(1);

    pos = pos - stepsize;
    myservo.write(pos);
    if (pos < 20) {
      pos = 20;
    }




  }

  Serial.print(detect);
  Serial.print(", ");
  Serial.print(pos);
  Serial.print(", ");
  Serial.println(distance);
}

//Define high-level H-bridge commands

void enableMotors()
{
  motorAOn();
  motorBOn();
}

void disableMotors()
{
  motorAOff();
  motorBOff();
}

void forward(int time)
{
  motorAForward();
  motorBForward();
  delay(time);
}

void backward(int time)
{
  motorABackward();
  motorBBackward();
  delay(time);
}

void turnLeft(int time)
{
  motorABackward();
  motorBForward();
  delay(time);
}

void turnRight(int time)
{
  motorAForward();
  motorBBackward();
  delay(time);
}

void coast(int time)
{
  motorACoast();
  motorBCoast();
  delay(time);
}

void brake(int time)
{
  motorABrake();
  motorBBrake();
  delay(time);
}
//Define low-level H-bridge commands

//enable motors
void motorAOn()
{
  digitalWrite(enableA, HIGH);
}

void motorBOn()
{
  digitalWrite(enableB, HIGH);
}

//disable motors
void motorAOff()
{
  digitalWrite(enableB, LOW);
}

void motorBOff()
{
  digitalWrite(enableA, LOW);
}

//motor A controls
void motorAForward()
{
  digitalWrite(pinA1, HIGH);
  digitalWrite(pinA2, LOW);
}

void motorABackward()
{
  digitalWrite(pinA1, LOW);
  digitalWrite(pinA2, HIGH);
}

//motor B controls
void motorBForward()
{
  digitalWrite(pinB1, HIGH);
  digitalWrite(pinB2, LOW);
}

void motorBBackward()
{
  digitalWrite(pinB1, LOW);
  digitalWrite(pinB2, HIGH);
}

//coasting and braking
void motorACoast()
{
  digitalWrite(pinA1, LOW);
  digitalWrite(pinA2, LOW);
}

void motorABrake()
{
  digitalWrite(pinA1, HIGH);
  digitalWrite(pinA2, HIGH);
}

void motorBCoast()
{
  digitalWrite(pinB1, LOW);
  digitalWrite(pinB2, LOW);
}

void motorBBrake()
{
  digitalWrite(pinB1, HIGH);
  digitalWrite(pinB2, HIGH);
}

งานชิ้นที่ 1

องค์ประกอบหน้าที่ต่างๆของโรงงาน โดยแบ่งหน้าที่ตามความเหมาะสมและความถนัดบริหารโดยมีหัวหน้าใหญ่เป็นผู้จัดตำแหน่งหน้าที่ ...