Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By derek bernsen
#63185 Hi all,
I am trying to get this code working. It works initially, but after the first time through the loop, the servo just makes momentary buzzing noises.
The purpose is to move the servo from 20 to 160 degrees once when the rssi is inside the correct range, and then repeat the same movement again only once when the rssi is outside the specified range. The default on startup is 160 degrees.

Here's my code:

Code: Select all


#include <ESP8266WiFi.h>
#include <SPI.h> //May be needed for serial printing.
#include <Servo.h> //Servo library
#define D0 16 //Defines pins to make assigning pins easier.
#define D1 5 // I2C Bus SCL (clock)
#define D2 4 // I2C Bus SDA (data)
#define D3 0
#define D4 2 // Same as "LED_BUILTIN", but inverted logic
#define D5 14 // SPI Bus SCK (clock)
#define D6 12 // SPI Bus MISO
#define D7 13 // SPI Bus MOSI
#define D8 15 // SPI Bus SS (CS)
#define D9 3 // RX0 (Serial console)
#define D10 1 // TX0 (Serial console)
Servo myservo; //Create a servo object named myservo
//Phone or additional ESP8266 module set to hotspot AP mode:
const char* ssid = "DroneBeacon1"; //Put your hotspot name inside the quotes
const char* password = "12345678910"; //Put your hotspot password inside the quotes
int state = 0;

void setup(){
     Serial.begin(115200); //sets serial baud rate so the microcontroller can talk to the serial print interface in the Arduino IDE - You may need to change it to 9600 instead!
     myservo.attach(D0);  // attaches the servo on pin D0 aka GPIO16 to the servo object
     myservo.write(160); //moves servo arm to 10 degrees rotation
     Serial.println("Locked");  //output the the serial monitor the word "Locked"
     WiFi.mode(WIFI_STA); //Sets wifi to Station mode
     WiFi.begin(ssid, password); //Connects to hotspot beacon
}

 void loop() { //The loop runs over and over again rapidly
   
    if (WiFi.status() != WL_CONNECTED) { //If wifi is NOT connected, do the following...
    Serial.println("Couldn't get a wifi connection");
    myservo.write(160); //Moves servo arm to 10 degrees
    Serial.println("Locked");
   
    }
  else { //If WiFi IS connected, then do the following...

  long rssi = WiFi.RSSI(); //Creates a variable named rssi and assign it to the function that returns the signal strength reading of the hotspot beacon
  Serial.print(rssi); //outputs the rssi reading to the serial monitor
   
  if (rssi > -50 && rssi < -5 && state == 0) { //If signal strength is stronger than -30, and weaker than -5. then do the following...
   myservo.write(20); //Rotate servo arm to 20 degrees
   delay(15); // wait for servo to move
   myservo.write(160); //Rotate servo arm to 160 degrees
   state = 1;
   Serial.println("Unlocked");
   }
   
  else { //If the above conditions aren't met then do the following...
    if (state = 1) {
   myservo.write(20); //Rotates servo arm back to 10 degrees.
   delay(15); //Wait for servo arm to move.
   myservo.write(160);
   state = 0;
   Serial.println("Locked");
   }
  }
  }
 }

Last edited by derek bernsen on Mon Mar 20, 2017 7:57 pm, edited 1 time in total.