Entering listening mode after replug power source

Hi

My photon has a strange behaviour:

When the photon is unplugged from the power source for a long time (12 hours) and then started again, it’s blinking blue (listening mode). I’m using an external antenna because internal antenna doesnt work. Any advice?

My code:

#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>

// OLED display TWI address
#define OLED_ADDR   0x3C

Adafruit_SSD1306 display(-1);

#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif

 
float tempf;
float tempc;
float humidity;
float inches;

int altitude = 79;
double pot3;
float pot2;
float pot1;
float pressurehpa;

char id[] = "xxxxx";
char pwd[] = "xxxxxx";

char data[180];

#include "Adafruit_Sensor.h"
#include "Adafruit_BME280.h"

#define BME_SCK D0
#define BME_MISO D3
#define BME_MOSI D2
#define BME_CS D1

char* SSID = "xxxxxx";
char* PASSWORD = "xxxxxx";


#define SEALEVELPRESSURE_HPA (1013.25)

Adafruit_BME280 bme; // I2C
//Adafruit_BME280 bme(BME_CS); // hardware SPI
//Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO,  BME_SCK);


SYSTEM_THREAD(ENABLED);
SYSTEM_MODE(SEMI_AUTOMATIC);
STARTUP(WiFi.selectAntenna(ANT_EXTERNAL));

int VaneValue;// raw analog value from wind vane
int Direction;// translated 0 - 360 direction
int CalDirection;// converted value with offset applied
int LastValue;

#define Offset 0; 

#include <math.h>

#define WindSensorPin (D6) // The pin location of the anemometer sensor

volatile unsigned long Rotations; // cup rotation counter used in interrupt routine
volatile unsigned long ContactBounceTime; // Timer to avoid contact bounce in interrupt routine

float WindSpeed; // speed miles per hour
float WindSpeedkt;
int displays = 0;

void setup() {
  // initialize and clear display
  LastValue = 1; 
    Serial.begin(9600);
  Serial.println(F("BME280 test"));

pinMode(WindSensorPin, INPUT);

WiFi.on();
WiFi.disconnect();
WiFi.clearCredentials();
WiFi.setCredentials(SSID, PASSWORD);
WiFi.connect();
waitUntil(WiFi.ready);
Particle.connect();



  display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
  display.clearDisplay();
  display.display();
  

  if (!bme.begin(0x76)) {
    Serial.println("Could not find a valid BME280 sensor, check wiring!");
    display.setTextSize(5);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.print("N/A");
  display.display();
    
  }




}

void loop() {
  // put your main code here, to run repeatedly:
  // display a line of text
  
  
  Rotations = 0; // Set Rotations count to 0 ready for calculations

	attachInterrupt(WindSensorPin, isr_rotation, FALLING);
	delay(3000);
	detachInterrupt(WindSensorPin);
	// convert to mp/h using the formula V=P(2.25/T)
	// V = P(2.25/3) = P * 0.75

	WindSpeed = Rotations * 0.75;
	WindSpeedkt = WindSpeed /  1.151;

  
 VaneValue = analogRead(A4);
Direction = map(VaneValue, 0, 4095, 0, 360);
CalDirection = Direction + Offset;

if(CalDirection > 360)
CalDirection = CalDirection - 360;

if(CalDirection < 0)
CalDirection = CalDirection + 360; 
   
    
   tempc = bme.readTemperature();
    tempf = tempc * 1.8 + 32;
    humidity = bme.readHumidity();
    pressurehpa = (bme.readPressure() / 100.0F);
    inches =  0.02952998751 * pressurehpa;
    
  float a = 17.271;
  float b = 237.7;
  float taupunktTemp = (a *  bme.readTemperature()) / (b +  bme.readTemperature()) + log(humidity/100);
  float pPre= (b * taupunktTemp) / (a - taupunktTemp);
  float pf = pPre * 1.8 + 32;
  
  pot1 = 1 - 0.0065 * altitude / (bme.readTemperature() + 0.0065 * altitude + 273.15);
  pot2 = (0.03416 / 0.0065);
  pot3 = pow(pot1,pot2);
  float relhpa = pressurehpa / pot3;
  float relinches = 0.02952998751 * relhpa;
  
  displays = displays + 1;
  
  if (displays > 3) {
      displays = 0;
  }
  
  if ((displays == 0) or (displays == 1)) {
      

 display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.print(tempc,1);
  display.print(" Grad");
  
 display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0,20);
  display.print(WindSpeedkt,1);
  display.print(" kt");

  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0,40);
  display.print(CalDirection);
  display.print(" deg");
  
 
  // update display with all of the above graphics
  display.display();
  
  
  }
  
   if ((displays == 2) or (displays == 3)) {
  
 display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.print(tempc,1);
  display.print(" Grad");

  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0,20);
  display.print(pressurehpa,2);
  display.print(" hP");
  
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0,40);
  display.print(humidity,1);
  display.print(" %");
  // update display with all of the above graphics
  display.display();
  
  
  }
  
  
  
   snprintf(data, sizeof(data)
        ,"{\"id\":\"%s\""
         ",\"pw\":\"%s\""
         ",\"dt\":\"%s\""
         ",\"tf\":\"%.1f\""
         ",\"hum\":\"%.1f\""
         ",\"baro\":\"%.2f\""
         ",\"tau\":\"%.1f\""
         ",\"wsm\":\"%.1f\""
         ",\"wd\":\"%.d\""
         "}"
        ,id
        ,pwd
        ,"now"
        ,tempf
        ,humidity
        ,relinches
        ,pf
        ,WindSpeed
        ,CalDirection
        );

 Particle.publish("spanienwunderground", data, PRIVATE);




}


void isr_rotation () {

	if ((millis() - ContactBounceTime) > 15 ) { // debounce the switch contact.
		Rotations++;
		ContactBounceTime = millis();
	}

} 

When i then try again it’s very often the same but sometimes it’s working fine…
Can I disable listening mode during startup or is there an other solution?

The unconditional WiFi.clearCredentials() call may well contribute to the fact that over time the device develops some amnesia.
We’ve always been warning about clearing the flash for no good reason.

Hi :slight_smile:

The problem was before adding this command. But I will remove it again :slight_smile:

Any other ideas?

When a Photon enters listening mode, it does this as it has no stored wi-fi credentials. That seems to suggest to me that credentials are being cleared somehow. Wifi.clearCredentials(); will absolutely do this.

If you’re stating this is an issue even when clearCredentails() is not called–it’s likely something else.

I’m wondering if the duration it’s offline is a red herring here–and rather how it’s going into an offline state. If it’s an abrupt turn-off, I’m wondering if the wifi stored credentials are somehow being corrupted in this state.

What is the device OS version of the Photon you’re running? You can find this by performing particle identify from the CLI while it is in listening mode.