-->
Page 1 of 2

What's Wrong With This Code?

PostPosted: Tue Jan 12, 2016 1:34 pm
by derek bernsen
It is not printing to the serial monitor, nor toggling pin 2 on and off!
Why is that?

The code:
Code: Select all#include <Arduino.h>
#include <ESP8266WiFi.h>

int OptoPin = 2;

 const char* SSID = "AI-THINKER_F53A7F";
// Return RSSI or 0 if target SSID not found
int32_t getRSSI(const char* target_ssid) {
  byte available_networks = WiFi.scanNetworks();

  for (int network = 0; network < available_networks; network++) {
    if (strcmp(WiFi.SSID(network), target_ssid) == 0) {
      return WiFi.RSSI(network);
    }
  }
  return 0;
}

void setup() {
  Serial.begin(115200);
  pinMode(OptoPin, OUTPUT);
}

void loop() {
  int32_t rssi = getRSSI(SSID);
  //int result = (rssi) * (rssi);
  int result = rssi * rssi;
   //int output = rssi;
   Serial.print(result);
  if (result > 80) {
   digitalWrite(2, HIGH);
   delay(10);
   }
  else if (result < 80) {
   digitalWrite(2, LOW);
   
  }
}
 

Re: What's Wrong With This Code?

PostPosted: Tue Jan 12, 2016 2:02 pm
by martinayotte
First, is the SSID "AI-THINKER_F53A7F" is the one from the current ESP or one from another ESP ?
Because, the current ESP won't be in the list of scanNetworks(), it doesn't scan itself.
Also, scanNetwork() probably returns nothing if your ESP is in AP-only mode, you need to do either WiFi.mode(WIFI_AP_STA); or WiFi.mode(WIFI_STA); to be able to scan APs.

Re: What's Wrong With This Code?

PostPosted: Wed Jan 13, 2016 1:19 am
by reaper7
...and finally if you have arduino esp8266 github version with sdk 1.5.x then:
https://github.com/esp8266/Arduino/issues/1355

Re: What's Wrong With This Code?

PostPosted: Wed Jan 13, 2016 7:28 pm
by derek bernsen
I fixed the scan networks. I made it faster by connecting to a network instead of scanning for it first.
Serial printing is working fine...
However, the LED (optoisolator) on GPIO2 is still not responding.
Why is GPIO2 not working? it goes to the LED, then to 3.3v positive.
There is a current limiting resistor between GPIO2 and LED - color coded brown-black-yellow-gold.
I am using a ESP-01 module that connects to a WiFi hotspot.
Here's my current code:
Code: Select all#include <ESP8266WiFi.h>
#include <SPI.h> //Not sure if needed for serial printing.
const char* ssid     = "dd-wrt_vap"; //AP
const char* password = ""; //Left blank, open hotspot.

void setup(){
     Serial.begin(115200);
     WiFi.mode(WIFI_STA);
     WiFi.begin(ssid, password);
}

 void loop() {
   
    if (WiFi.status() != WL_CONNECTED) {
    Serial.println("Couldn't get a wifi connection");
    digitalWrite(2, HIGH);
    Serial.println("OPTO OFF");
   
    }
  else {

  long rssi = WiFi.RSSI();
  Serial.print(rssi);
   
  if (rssi > -80 && rssi < 0) {
   digitalWrite(2, LOW);
   Serial.println("OPTO ON");
   }
   
  if (rssi < -80) {
   digitalWrite(2, HIGH);
   Serial.println("OPTO OFF");
   
  }
  }
 }