L298 Motor drive bord.
Code for control motor.
#include<SoftwareSerial.h>
char receivedChar;
int ENA=2;
int IN1=3;
int IN2=4;
int IN3=5;
int IN4=6;
int ENB=7;
int ENA1=A0;
int IN11=A1;
int IN12=A2;
int IN13=A3;
int IN14=A4;
int ENB1=A5;
SoftwareSerial mySerial(10, 11); // RX, TX
//RX on Bluetooth to TCX on Arduino through a voltage divider 50ohm from arduino and 100ohm to ground. This is so we can drop the the voltage down to 3.3v (roughly)
// Use blueterm on android to get messages
void setup()
{
pinMode(ENA,OUTPUT);//output
pinMode(ENB,OUTPUT);
pinMode(IN1,OUTPUT);
pinMode(IN2,OUTPUT);
pinMode(IN3,OUTPUT);
pinMode(IN4,OUTPUT);
pinMode(ENA1,OUTPUT);//output
pinMode(ENB1,OUTPUT);
pinMode(IN11,OUTPUT);
pinMode(IN12,OUTPUT);
pinMode(IN13,OUTPUT);
pinMode(IN14,OUTPUT);
mySerial.begin(9600);
}
void loop() // run over and over
{
while (!mySerial.available()); // stay here so long as COM port is empty
receivedChar = mySerial.read();
if (receivedChar == 'A') {
Serial.print(receivedChar);
//digitalWrite(ENA,LOW);
// digitalWrite(ENB,LOW);// High is start driving car
digitalWrite(ENA,HIGH);
digitalWrite(ENB,HIGH);// High is start driving car
digitalWrite(IN1,HIGH);
digitalWrite(IN2,LOW);//setting motorA's directon
digitalWrite(IN3,LOW);
digitalWrite(IN4,HIGH);//setting motorB's directon
}
if (receivedChar == 'B') {
// digitalWrite(ENA,LOW); Backward
// digitalWrite(ENB,LOW);// High is start driving car
digitalWrite(ENA,HIGH);
digitalWrite(ENB,HIGH);// High is start driving car
digitalWrite(IN1,LOW);
digitalWrite(IN2,HIGH);//setting motorA's directon
digitalWrite(IN3,HIGH);
digitalWrite(IN4,LOW);//setting motorB's directon
}
if (receivedChar == 'C')
{
// digitalWrite(ENA,LOW);Left
// digitalWrite(ENB,LOW);// High is start driving car
digitalWrite(ENA,HIGH);
digitalWrite(ENB,HIGH);// High is start driving car
digitalWrite(IN1,HIGH);
digitalWrite(IN2,LOW);//setting motorA's directon
digitalWrite(IN3,HIGH);
digitalWrite(IN4,LOW);//setting motorB's directon
} // if it is a 3 flash the LED
if (receivedChar == 'D') {
//digitalWrite(ENA,LOW);
//digitalWrite(ENB,LOW);// High is start driving car
digitalWrite(ENA,HIGH);
digitalWrite(ENB,HIGH);// High is start driving car
digitalWrite(IN1,LOW);
digitalWrite(IN2,HIGH);//setting motorA's directon
digitalWrite(IN3,LOW);
digitalWrite(IN4,HIGH);//setting motorB's directon
} // if it is a 4 print out lots of sensor data
if (receivedChar == 'E') {
digitalWrite(ENA,HIGH);
digitalWrite(ENB,HIGH);// High is start driving car
digitalWrite(IN1,LOW);
digitalWrite(IN2,LOW);//setting motorA's directon
digitalWrite(IN3,LOW);
digitalWrite(IN4,LOW);//setting motorB's directon
}
}
How to make mobile app control
ESP 8266 To Think speak
Code
//ตัวนี้ใช้ได้ดีมาก
#include <SoftwareSerial.h>
#include <stdlib.h>
#include "DHT.h"
DHT dht;
int sensor1State=0;
int sensor2State=0;
//int sensor3State=0;
//int sensor4State=0;
// replace with your channel's thingspeak API key
String apiKey = "Your API from web ";
// connect 10 to TX of ESP8266
// connect 11 to RX of ESP8266
SoftwareSerial ser(10, 11); // RX, TX
int i=1;
void setup() {
// DHT
dht.setup(2); // data pin 2
Serial.begin(9600);
ser.begin(9600);
// reset ESP8266
ser.println("AT+RST");
}
// the loop
void loop() {
delay(dht.getMinimumSamplingPeriod());
float humidity = dht.getHumidity(); // ดึงค่าความชื้น
float temperature = dht.getTemperature(); // ดึงค่าอุณหภูมิ
Serial.print(dht.getStatusString());
Serial.print("\t");
Serial.print(humidity, 1);
Serial.print("\t\t");
Serial.print(temperature, 1);
Serial.print("\t\t");
Serial.println(dht.toFahrenheit(temperature), 1);
sensor1State = humidity ;//digitalRead(sensor1);
sensor2State = temperature; //digitalRead(sensor2);
String state1=String(sensor1State);
String state2=String(sensor2State);
// server connect
String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += "184.106.153.149"; // api.thingspeak.com
cmd += "\",80";
ser.println(cmd);
Serial.println(cmd);
if(ser.find("Error")){
Serial.println("AT+CIPSTART error");
return;
}
// prepare GET string
String getStr = "GET /update?api_key=";
getStr += apiKey;
getStr +="&field1=";
getStr += String(state1);
getStr +="&field2=";
getStr += String(state2);
getStr += "\r\n\r\n";
// send data length
cmd = "AT+CIPSEND=";
cmd += String(getStr.length());
ser.println(cmd);
Serial.println(cmd);
if(ser.find(">")){
ser.print(getStr);
Serial.print(getStr);
}
else{
ser.println("AT+CIPCLOSE");
// alert user
Serial.println("AT+CIPCLOSE");
}
// thingspeak needs 15 sec delay between updates
delay(16000);
}