Chat freely about anything...

User avatar
By relliott911
#62065 Hello all,

Newbie here. I would like to be able to power up a device and have it be able to connect to different access points with out uploading different sketches. I am working on a large site where we have different geographic SSID's for our AP's . I would like to be able to set it up so that if the 1st SSID/PW is not reachable it will use different SSID/PW for next location and so on....

The ESP8266's, 5 in total are equipped with dallas 1 wire temp sensors and are used around the site to monitor various product temperatures through out the day/week they are powered by LiPo batteries and may stay in one access points range for a day up to almost a week. Any help would be great!
User avatar
By GengusKahn
#62175 There can be a list made of the available AP's and use some logic to decide if you wish to connect...

Look through this example of scanning for open networks, then verifying the internet connection by attempting to connect to google and finally set the time via NTP......

I have used a Neopixel ring with 24 pixels to display the time....

Code: Select all/*
 * Time_NTP.pde
 * Example showing time sync to NTP time source
 *
 * This sketch uses the ESP8266WiFi library
 */
 
#include <TimeLib.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <Adafruit_NeoPixel.h>

#define PIXELPIN 2

#define BRIGHTNESS 32

Adafruit_NeoPixel strip = Adafruit_NeoPixel(24, PIXELPIN, NEO_GRB + NEO_KHZ800);


byte hourval,minuteval,secondval;
int minuteval1,hourval1,secondval1,humInt,tempInt,looPC,fAil;
byte pixelColorRed, pixelColorGreen, pixelColorBlue;

// NTP Servers:
//IPAddress timeServer(132, 163, 4, 101); // time-a.timefreq.bldrdoc.gov
 IPAddress timeServer(132, 163, 4, 102); // time-b.timefreq.bldrdoc.gov
// IPAddress timeServer(132, 163, 4, 103); // time-c.timefreq.bldrdoc.gov


const int timeZone = 0;     // GMT/UTC
//const int timeZone = -5;  // Eastern Standard Time (USA)
//const int timeZone = -4;  // Eastern Daylight Time (USA)
//const int timeZone = -8;  // Pacific Standard Time (USA)
//const int timeZone = -7;  // Pacific Daylight Time (USA)


WiFiUDP Udp;
unsigned int localPort = 8888;  // local port to listen for UDP packets

void setup()
{
  Serial.begin(115200);
  Serial.println("\r\n\r\n\r\n");
  WiFi.persistent(false);
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
  delay(100);
  strip.begin(); //begin neo pixel strip
  strip.show(); //initialize strip
  Serial.println("\r\n\r\n\r\n");
  Serial.println("Setup done");
  Serial.println("scan start");
  Serial.println("\r\n\r\n\r\n");
  int n = WiFi.scanNetworks();
  Serial.println("scan done");
  if (n == 0)
    Serial.println("no networks found");
  else
  {
    for (int i = 0; i < n; ++i)
    {
     
      colorWipe(strip.Color(0, 255, 0), 10);
      colorWipe(strip.Color(0, 0, 0), 10);
      delay(10);
      int sec = WiFi.encryptionType(i);
      if((sec == ENC_TYPE_NONE ) && (WiFi.RSSI(i) > -95)) {
        fAil=0;
        WiFi.persistent(false);
        WiFi.mode(WIFI_STA);
        WiFi.disconnect();
        delay(100);
       
        Serial.println("\r\nFound Open Network......\r\n");
        Serial.print("Connecting to SSID : ");
        Serial.println(WiFi.SSID(i));
        WiFi.begin(WiFi.SSID(i).c_str());
 
        while (WiFi.status() != WL_CONNECTED) {
          delay(500);
          Serial.print(".");
        }
        Serial.print("\r\n\r\nIP address is.......  ");
        Serial.println(WiFi.localIP());
        WiFiClient client;
        if(!client.connect("google.com", 80)){
          Serial.println("\r\n...... Internet Connection test failed...Retrying....\r\n");
          fAil=1;
        }else{
        client.stop();
        Serial.println("Starting UDP");
        Udp.begin(localPort);
        Serial.print("Local port: ");
        Serial.println(Udp.localPort());
        Serial.println("waiting for sync");
        setSyncProvider(getNtpTime);
        delay(50);
        strip.begin();
        strip.setBrightness(BRIGHTNESS);
        delay(50);
        colorWipe(strip.Color(125, 0, 125), 10);
        colorWipe(strip.Color(0, 128, 0), 10);
        colorWipe(strip.Color(0, 0, 0), 10);
        delay(10000);
        return;
        }
        }
    }
            if(fAil==1){
            Serial.println("\r\n...... Internet Connection failed...Halted....\r\n");
            colorWipe(strip.Color(64, 0, 0), 10);
            delay(-1);
          }
  }
}

time_t prevDisplay = 0; // when the digital clock was displayed

void loop()

 
  if (timeStatus() != timeNotSet) {
    if (now() != prevDisplay) { //update the display only if time has changed
    prevDisplay = now();
    digitalClockDisplay();
    hourval=hour();
    minuteval=minute();
    secondval=second();
    if(hourval > 11) hourval -= 12;
    hourval1 = constrain(map(hourval, 0, 11, 0, 23), 0, 23);
    if(int(minuteval)>2){
      minuteval1 = constrain(map(minuteval, 0, 59, 0, 23), 0, 23);
    }else{
      minuteval1=0;
    }
    if(int(secondval)>2){
      secondval1 = constrain(map(secondval, 0, 59, 0, 23), 0, 23);
    }else{
      secondval1=0;
    }
 for(uint8_t i=0; i<strip.numPixels(); i++) {
  if (i == minuteval1) {
    pixelColorGreen = 255; 
  }
  else {
    pixelColorGreen = 0;
  } 
  if (i == hourval1) {
    pixelColorRed = 255; 
  }
  else {
    pixelColorRed = 0;
  }
    if (i == secondval1) {
    pixelColorBlue = 255; 
  }
  else {
    pixelColorBlue = 0;
  }
  strip.setPixelColor(i, strip.Color(pixelColorRed, pixelColorGreen, pixelColorBlue));
  strip.show();
 }
    }   
  }else{

    delay(10000);
    setSyncProvider(getNtpTime);
    }
   
}

void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(".");
  Serial.print(month());
  Serial.print(".");
  Serial.print(year());
  Serial.println();
}



void printDigits(int digits){
  // utility for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

/*-------- NTP code ----------*/

const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message
byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets

time_t getNtpTime()
{
  while (Udp.parsePacket() > 0) ; // discard any previously received packets
  Serial.println("Transmit NTP Request");
  sendNTPpacket(timeServer);
  uint32_t beginWait = millis();
  while (millis() - beginWait < 1500) {
    int size = Udp.parsePacket();
    if (size >= NTP_PACKET_SIZE) {
      Serial.println("Receive NTP Response");
      Udp.read(packetBuffer, NTP_PACKET_SIZE);  // read packet into the buffer
      unsigned long secsSince1900;
      // convert four bytes starting at location 40 to a long integer
      secsSince1900 =  (unsigned long)packetBuffer[40] << 24;
      secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
      secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
      secsSince1900 |= (unsigned long)packetBuffer[43];
      return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
    }
  }
  Serial.println("No NTP Response :-(");
  return 0; // return 0 if unable to get the time
}

// send an NTP request to the time server at the given address
void sendNTPpacket(IPAddress &address)
{
  // set all bytes in the buffer to 0
  memset(packetBuffer, 0, NTP_PACKET_SIZE);
  // Initialize values needed to form NTP request
  // (see URL above for details on the packets)
  packetBuffer[0] = 0b11100011;   // LI, Version, Mode
  packetBuffer[1] = 0;     // Stratum, or type of clock
  packetBuffer[2] = 6;     // Polling Interval
  packetBuffer[3] = 0xEC;  // Peer Clock Precision
  // 8 bytes of zero for Root Delay & Root Dispersion
  packetBuffer[12]  = 49;
  packetBuffer[13]  = 0x4E;
  packetBuffer[14]  = 49;
  packetBuffer[15]  = 52;
  // all NTP fields have been given values, now
  // you can send a packet requesting a timestamp:                 
  Udp.beginPacket(address, 123); //NTP requests are to port 123
  Udp.write(packetBuffer, NTP_PACKET_SIZE);
  Udp.endPacket();
}

void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
  strip.setPixelColor(i, c);
  strip.show();
  delay(wait);
  }
}