-
Notifications
You must be signed in to change notification settings - Fork 0
/
CarIoT.ino
146 lines (136 loc) · 2.83 KB
/
CarIoT.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//Import libary
#include <SoftwareSerial.h>
#include <ESP8266WiFi.h>
#include <FirebaseArduino.h>
#include <ArduinoJson.h>
#include <ESP8266HTTPClient.h>
SoftwareSerial bluetooth(D6, D5);
//Define Firebase
#define FIREBASE_HOST "project-demo-e0f9a.firebaseio.com"
#define FIREBASE_AUTH "SYE0NrsPDGNsCYKeLAvpCsxhiaQ8Vic6kgkAOYkD"
#define WIFI_SSID "Minh Quan"
#define WIFI_PASSWORD "13102020"
//Define motor
#define IN1 D1
#define IN2 D2
#define IN3 D3
#define IN4 D4
String myString;
int MQ = A0;
int dataMQ = 0;
int speeds = 255;
char key;
void setup(){
Serial.begin(9600);
bluetooth.begin(9600);
//pinMode MQ
pinMode(MQ, INPUT);
//Connect to Wifi
pinMode(D0, OUTPUT);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting");
while(WiFi.status() != WL_CONNECTED){
Serial.print(".");
delay(500);
}
Serial.println();
Serial.print("Connected");
Serial.println(WiFi.localIP());
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
//pinMode for Motor
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}
void loop(){
//Control car
if(bluetooth.available()){
key = bluetooth.read();
Serial.println(key);
}
if(key == 'R'){
goTop();
} else if(key == 'G'){
goBack();
} else if(key == 'Y'){
goLeft();
} else if(key == 'B'){
goRight();
} else if(key == 'L'){
goTopgg();
} else if(key == 'X'){
goBackgg();
} else if(key == 'T'){
goLeftgg();
} else if(key == 'P'){
goRightgg();
} else {
goStop();
}
//Send data MQ to Firebase
sendDataMQ();
}
// Run when command is Top
void goTop(){
digitalWrite(IN1, HIGH);
digitalWrite(IN3, HIGH);
}
// Run when command is Back
void goBack(){
digitalWrite(IN2, HIGH);
digitalWrite(IN4, HIGH);
}
// Run when command is Left
void goLeft(){
digitalWrite(IN3, HIGH);
}
// Run when command is Right
void goRight(){
digitalWrite(IN1, HIGH);
}
// Run when key is not equals with command
void goStop(){
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
key = 's';
}
// Run when Google A -> call Top
void goTopgg() {
digitalWrite(IN1, HIGH);
digitalWrite(IN3, HIGH);
delay(1000);
goStop();
}
// Run when Google A -> call Back
void goBackgg() {
digitalWrite(IN2, HIGH);
digitalWrite(IN4, HIGH);
delay(1000);
goStop();
}
// Run when Google A -> call Left
void goLeftgg() {
digitalWrite(IN3, HIGH);
delay(500);
goStop();
}
// Run when Google A -> call Right
void goRightgg() {
digitalWrite(IN1, HIGH);
delay(500);
goStop();
}
// Send data read in MQ to Firebase
void sendDataMQ(){
dataMQ = analogRead(MQ);
myString = String(dataMQ);
Serial.print(myString);
Firebase.setString("MQ Value", myString);
}