3905Views18Replies

Author Options:

security system (not responding after combining ultrasonic sensor code and esp8266 code ) Answered

The base of this project is https://www.instructables.com/id/SIMPLE-ARDUINO-ULTRASONIC-SENSOR-DOOR-ALARM-/
&http://randomnerdtutorials.com/door-status-monitor-using-the-esp8266/


I changed according to my uses from above links

I tryed by dividing into two parts one for ultrasonic sensor and other for esp8266 they worked fine while separating

ultrasonic and buzzer are working in (Tools <board<Arduino/Generic uno)
and wifi module is working in onlt(Tools <board<(any esp8266 in down column)
when i mix ultrasonic and buzzer nothing happening)
any idea


here is the code


#include <Arduino.h>

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>

#include <ESP8266HTTPClient.h>

#define USE_SERIAL Serial


#define trigPin 9
#define echoPin 10
#define piezoPin 8
ESP8266WiFiMulti WiFiMulti;
int normalDistance;
boolean triggered = false;

long duration, distance;

void setup() {
  USE_SERIAL.begin(115200);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(piezoPin, OUTPUT);

  long duration, distance;

while (millis() < 5000) {
  digitalWrite(piezoPin, HIGH);
      digitalWrite(trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance= duration*0.034/2;
        normalDistance = distance;
     
USE_SERIAL.print("Distance: ");
USE_SERIAL.println(distance);
    
   digitalWrite(piezoPin, LOW);
   for(uint8_t t = 4; t > 0; t--) {
        USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
        USE_SERIAL.flush();
        delay(1000);
}
WiFiMulti.addAP("username", "pass");
}
}
void loop() {

digitalWrite(trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance= duration*0.034/2;

USE_SERIAL.print("Distance: ");
USE_SERIAL.println(distance);
if (distance < normalDistance-5) {
      triggered = true;
    }
    else {
        triggered = false;
    }
    if (triggered) {
    tone(piezoPin, 635);
    delay(500);
    tone(piezoPin, 912);
    delay(250);
    if((WiFiMulti.run() == WL_CONNECTED)) {
        HTTPClient http;

        USE_SERIAL.print("[HTTP] begin...\n");
       
        http.begin("iftt link.........................."); //HTTP

        USE_SERIAL.print("[HTTP] GET...\n");
        // start connection and send HTTP header
        int httpCode = http.GET();

        // httpCode will be negative on error
        if(httpCode > 0) {
            // HTTP header has been send and Server response header has been handled
            USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);

            // file found at server
            if(httpCode == HTTP_CODE_OK) {
                String payload = http.getString();
                USE_SERIAL.println(payload);
            }
        } else {
            USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
        }

        http.end();
    }
    }
}

any idea about whats going on

Thanks

Hemanth

18 Replies

user
msuzuki777 (author)2017-03-14

Which version of ESP8266 are you using? I don't believe any of them support 9, 10 and 8 as digital pins. I think they're usually connected to flash memory in the ESP8266 board.

https://github.com/esp8266/Arduino/blob/master/doc...

LOG

Select as Best AnswerUndo Best Answer

user

Can we use ultrasonic header files so that i can use directly
http://www.ardublog.com/library-for-arduino-ultrasonic-ranging-hc-sr04/
Like in that link

Select as Best AnswerUndo Best Answer

user

I could access the link but if you are using a UNO, it should work okay.

LOG

Select as Best AnswerUndo Best Answer

user

OK IF U FIND HOW TO ACCESS HTTP KINK WITH AT COMMANDS PLZ

LET ME KNOW I WILL TRY ANOTHER METHOD IN CODING BY HEADER FILES

THANKS

Select as Best AnswerUndo Best Answer

user

esp8266 (01)

ya esp is not connect to digital pin

my connection are

GND----GND

GPI0----GND

VCC------3.3V

RX-----RX

TX------TX

CH_PD------3.3V

Select as Best AnswerUndo Best Answer

user

When you are using these:

#include <ESP8266WiFi.h>

#include <ESP8266WiFiMulti.h>

#include <ESP8266HTTPClient.h>

then the Arduino environment will look for an ESP8266 as the only microcontroller. The ESP8266 does have digital pins called GPIO pins. So when your code includes these lines:

#define trigPin 9

#define echoPin 10

#define piezoPin 8

it is talking about digital pins on the ESP8266. I think the only permitted digital pins are 0,2,12,13,14,15,18.

I think what you are trying to do is connect a regular Arduino with an ESP8266. This code will not do that. Look at the following for a way to do that:

https://www.instructables.com/id/Wireless-Temperat...

The other alternative would be to connect the ultrasonic sensor directly to the ESP8266 and not use a regular Arduino. If you have an ESP-01, this would be hard to do.

Hope this helps.

LOG

Select as Best AnswerUndo Best Answer

user

I AM USING URDINO UNO

what can i do now is there no way to make it work with this setup

Select as Best AnswerUndo Best Answer

user

With an Arduino UNO you can hook up the ultrasonic and buzzer as in the Instructable you found. The you would have to hook up the ESP8266 serially like that example:

https://www.instructables.com/id/Wireless-Temperat...

then you would have to write code for the UNO to send 'AT' commands to the ESP8266 to do the WiFi stuff. I don't know how to do that. You would have to figure that out for yourself or see if you can find some examples on the Internet.

LOG

Select as Best AnswerUndo Best Answer

user

Thanks for your help and i will try your idea if find anything feel free to let me know

Thanks

Select as Best AnswerUndo Best Answer

user

)The other alternative would be to connect the ultrasonic sensor directly to the ESP8266 and not use a regular Arduino. If you have an ESP-01, this would be hard to do.)

whats that means

Thanks for ur help

Select as Best AnswerUndo Best Answer

user

The problem with the ESP-01 is that it has only two digital pins, GPIO0 and 2 on the connector. You need two GPIO pins for the ultrasonic plus one for the buzzer. The other GPIOs are available but you have to find them on the PCB. If you have an ESP-03 or higher, there are more GPIO pins available on the connectors.

LOG

Select as Best AnswerUndo Best Answer

user
hemanth kumar reddy (author)2017-03-14

#define trigPin 9

#define echoPin 10

#define piezoPin 8

const char* ssid = "hema";

const char* password = "hema9966";

const char* host = "maker.ifttt.com";

int normalDistance;

boolean triggered = false;

long duration, distance;

void setup() {

Serial.begin(115200);

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

pinMode(piezoPin, OUTPUT);

long duration, distance;

while (millis() < 5000) {

digitalWrite(piezoPin, HIGH);

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance= duration*0.034/2;

normalDistance = distance;

Serial.print("Distance: ");

Serial.println(distance);

digitalWrite(piezoPin, LOW);

}

}

void loop() {

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance= duration*0.034/2;

Serial.print("Distance: ");

Serial.println(distance);

if (distance < normalDistance-5) {

triggered = true;

} else {

triggered = false;

}

if (triggered) {

tone(piezoPin, 635);

delay(500);

tone(piezoPin, 912);

delay(250);

WiFi.begin(ssid, password); // connecting to wifi

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

delay(500);

WiFiClient client;

const int httpPort = 80;

if (!client.connect(host, httpPort)) {

return;

String url = "/trigger/security_system/with/key/gVviiDDe7JMh1WUL3Aw9glnWNkPsH6k-PohmYn8sovA";

client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");

// Sleep for a second to prevent flooding the serial

delay(1000);

}

}

}

}

i did one more but showing error

WiFi.begin(ssid, password);was not declared in this scope

any idea

Select as Best AnswerUndo Best Answer

user
Bay Yolal (author)2017-03-14

What is your ESP8266's Baud Rate? Is it 115200, or 9600? Your code shows me you are working on 115200. Maybe your ESP8266 was in 9600.

Select as Best AnswerUndo Best Answer

user

normally when i enter the code into arduino the buzzer high and ultra sonic starts measuring distance but while i enter the code by adding esp8266 code its doing nothing there is no responce

thanks

Select as Best AnswerUndo Best Answer

user

its 115200 only i changed it by using At command

Select as Best AnswerUndo Best Answer

user
msuzuki777 (author)2017-03-14

I am only guessing but in your code, I do not see that you are including an ultrasonic library for the pulseIn() function.

And I'm not sure that the ultrasonic library is compatilble with the ESP8266 code.

Good luck.

Select as Best AnswerUndo Best Answer

user

#define trigPin 9

#define echoPin 10

#define piezoPin 8

int normalDistance;

boolean triggered = false;

long duration, distance;

void setup() {

Serial.begin (9600);

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

pinMode(piezoPin, OUTPUT);

long duration, distance;

while (millis() < 5000) {

digitalWrite(piezoPin, HIGH);

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance= duration*0.034/2;

normalDistance = distance;

Serial.print("Distance: ");

Serial.println(distance);

digitalWrite(piezoPin, LOW);

}

}

void loop() {

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance= duration*0.034/2;

Serial.print("Distance: ");

Serial.println(distance);

if (distance < normalDistance-5) {

triggered = true;

} else {

triggered = false;

}

if (triggered) {

tone(piezoPin, 635);

delay(500);

tone(piezoPin, 912);

delay(250);

}

}

it is the code for only working of ultrasonic and buzzer it worked for me

Select as Best AnswerUndo Best Answer