5007Views18Replies

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

Discussions

The forums are retiring in 2021 and are now closed for new topics and comments.
0
hemanth kumar reddy
hemanth kumar reddy

Reply 3 years ago

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

0
msuzuki777
msuzuki777

Reply 3 years ago

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

LOG

0
hemanth kumar reddy
hemanth kumar reddy

Reply 3 years ago

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

0
hemanth kumar reddy
hemanth kumar reddy

Reply 3 years ago

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

0
msuzuki777
msuzuki777

Reply 3 years ago

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

0
hemanth kumar reddy
hemanth kumar reddy

Reply 3 years ago

I AM USING URDINO UNO

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

0
msuzuki777
msuzuki777

Reply 3 years ago

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

0
hemanth kumar reddy
hemanth kumar reddy

Reply 3 years ago

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

Thanks

0
hemanth kumar reddy
hemanth kumar reddy

Reply 3 years ago

)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

0
msuzuki777
msuzuki777

Reply 3 years ago

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

0
hemanth kumar reddy
hemanth kumar reddy

3 years ago

#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

0
Bay Yolal
Bay Yolal

3 years ago

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.

0
hemanth kumar reddy
hemanth kumar reddy

Reply 3 years ago

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

0
hemanth kumar reddy
hemanth kumar reddy

Reply 3 years ago

its 115200 only i changed it by using At command

0
msuzuki777
msuzuki777

3 years ago

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.

0
hemanth kumar reddy
hemanth kumar reddy

Reply 3 years ago

#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