Notes
Notes - notes.io |
#define ECHO_PIN_1 2
#define TRIG_PIN_2 11
#define ECHO_PIN_2 3
// Interrupt pins: 2, 3, 18, 19, 20, 21 for Arduino Mega
// Wifi configuration
#define WIFI_NAME "iPhone"
#define WIFI_PASSWORD "melih123"
#define WIFI_BAUDRATE 115200 // Wifi modulünün baudrate hizi
// Thingspeak configuration
String HOST = "api.thingspeak.com";
String PORT = "80";
String CHANNEL_ID = "924026";
String API_WRITE = "BG4D216982DHR85U"; // to write data
String API_READ = "3AXCARGDYMVV4V60"; // to read data
String FIELD_PEOPLEIN = "field1";
String FIELD_PEOPLEOUT = "field2";
bool sentCommand = false;
int peopleIn = 0;
int peopleOut = 0;
int sensorEngelMesafesi = 20; // Sensör tetiklenmesi için cm degeri
int sensor1 = 0, sensor2 = 0;
int sensor1Last = 0, sensor2Last = 0;
bool flagIn = false;
bool flagOut = false;
// ----------------WIFI-ESP--------------------
void initEsp(); // Wifi baglantisi sagliyor
void connectToServer(); // Servere baglanti saglaniyor
void disconnectFromServer(); // Serverden baglanti kesiliyor
void sendData(); // Thingspeak'e datayi yolluyor
bool sendCommandEsp(String, char*, int); // Esp modulüne komut yollar
// ----------------WIFI-ESP--------------------
// ------------------ Sensor ---------------------
void readSensors(); // Sensörlerden veri okuyor
int readSensorValue(int); // return= 0: Engel yok , 1: Engel var
void trigSensors(int);
void trigIn();
void trigOut();
// ------------------ Sensor ---------------------
void setup() {
pinMode(TRIG_PIN_1, OUTPUT); // Sets the trigPin as an Output
pinMode(ECHO_PIN_1, INPUT); // Sets the echoPin as an Input
pinMode(TRIG_PIN_2, OUTPUT); // Sets the trigPin as an Output
pinMode(ECHO_PIN_2, INPUT); // Sets the echoPin as an Input
attachInterrupt(digitalPinToInterrupt(ECHO_PIN_1), trigIn, RISING);
attachInterrupt(digitalPinToInterrupt(ECHO_PIN_2), trigOut, RISING);
Serial.begin(9600); // Serial monitoru başlatiyoruz
Serial3.begin(WIFI_BAUDRATE); // Esp modulü ile haberleşmeyi başlatiyor
//initEsp(); // Esp modulünü kuruyoruz
}
void loop() {
trigSensors(0);
trigSensors(1);
Serial.println("In: "+String(peopleIn));
Serial.println("Out: "+String(peopleOut));
Serial.println("nn");
delay(500);
}
void trigIn(){
peopleIn++;
}
void trigOut(){
peopleOut++;
}
void trigSensors(int which){
digitalWrite(which == 0 ? TRIG_PIN_1 : TRIG_PIN_2, LOW);
delayMicroseconds(2);
digitalWrite(which == 0 ? TRIG_PIN_1 : TRIG_PIN_2, HIGH);
delayMicroseconds(10);
digitalWrite(which == 0 ? TRIG_PIN_1 : TRIG_PIN_2, LOW);
delay(50);
}
void readSensors() {
sensor1Last = sensor1;
sensor2Last = sensor2;
sensor1 = readSensorValue(0);
sensor2 = readSensorValue(1);
}
int readSensorValue(int which) {
double duration = 0;
double distance = 0;
digitalWrite(which == 0 ? TRIG_PIN_1 : TRIG_PIN_2, LOW);
delayMicroseconds(2);
digitalWrite(which == 0 ? TRIG_PIN_1 : TRIG_PIN_2, HIGH);
delayMicroseconds(10);
digitalWrite(which == 0 ? TRIG_PIN_1 : TRIG_PIN_2, LOW);
duration = pulseIn(which == 0 ? ECHO_PIN_1 : ECHO_PIN_2, HIGH);
distance = duration / 29.2 / 2;
delay(50);
return (distance <= sensorEngelMesafesi) ? 1 : 0;
}
void initEsp() {
Serial.println("AT Yollandi.Esp modulünden cevap bekleniyor...");
sentCommand = sendCommandEsp("AT", (char*)"OK", 10);
if (sentCommand)
Serial.println("Wifi modulune baglanildi.");
else
{
Serial.println("Wifi modulune baglanilamadi!!");
return;
}
Serial.println("Esp modülünü client olarak ayarlamak için istek yollandi");
sentCommand = sendCommandEsp("AT+CWMODE=1", (char*)"OK", 10);
if (sentCommand)
Serial.println("Wifi modülü client olarak ayarlandi.");
else
{
Serial.println("Wifi modülü client olarak ayarlanamadi!!");
return;
}
String connectionCommand = "AT+CWJAP=";
connectionCommand += """;
connectionCommand += WIFI_NAME;
connectionCommand += """;
connectionCommand += ",";
connectionCommand += """;
connectionCommand += WIFI_PASSWORD;
connectionCommand += """;
Serial.println("Aga baglaniliyor...");
sentCommand = sendCommandEsp(connectionCommand, (char*)"OK", 10);
if (sentCommand)
Serial.println("Aga baglanildi: " + String(WIFI_NAME));
else
{
Serial.println("Aga baglanilamadi: " + String(WIFI_NAME));
return;
}
connectToServer();
}
void connectToServer() {
String connect_ = ""TCP"";
connect_ += ",";
connect_ += """ + HOST + """;
connect_ += ",";
connect_ += PORT;
Serial.println("Thingspeak serverine baglaniliyor..." + String("(" + connect_ + ")"));
sentCommand = sendCommandEsp("AT+CIPSTART=" + connect_, (char*)"OK", 10);
if (sentCommand)
Serial.println("Thingspeak serverine baglanti kuruldu.");
else
Serial.println("Thingspeak serverine baglanti kurulamadi !!");
}
void disconnectFromServer() {
Serial.println("Serverden baglanti kopariliyor...");
sentCommand = sendCommandEsp("AT+CIPCLOSE", (char*)"OK", 10);
if (sentCommand)
Serial.println("Serverden baglanti koptu.");
else
Serial.println("Serverden baglanti kopamadi !");
}
void sendData() {
connectToServer();
String veri = "GET ";
veri += "/update?";
veri += "api_key=" + API_WRITE;
veri += "&";
veri += FIELD_PEOPLEIN;
veri += "=";
veri += String(peopleIn);
veri += "&";
veri += FIELD_PEOPLEOUT;
veri += "=";
veri += String(peopleOut);
veri += "rn";
Serial.println("Gönderilecek veri boyutu ayarlandi:" + String(veri.length()));
Serial.println("Cevap bekleniyor...");
sentCommand = sendCommandEsp("AT+CIPSEND=" + String(veri.length()), (char*)">", 10);
if (sentCommand) {
Serial.println("Veri gönderiliyor...");
Serial3.print(veri);
Serial.println("Veri Gönderildi");
Serial.println(veri);
} else
Serial.println("Veri gonderilemedi !!");
disconnectFromServer();
}
bool sendCommandEsp(String command, char* response, int timeout) {
bool count = 0;
bool ok = true;
Serial3.println(command);
while (!Serial3.find(response)) {
Serial3.println(command);
if ( (++count) > timeout)
{
ok = false;
break;
}
}
return ok;
}
|
Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 12 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team