boolean WiFiManager::autoConnect(char const *apName) {
    begin(apName);

//  DEBUG_PRINT("");
//    DEBUG_PRINT("AutoConnect");
    // read eeprom for ssid and pass
    String ssid = getSSID();
    String pass = getPassword();
    
    if ( ssid.length() > 1 ) {
        display.print("Waiting for Wifi to connect");
        DEBUG_PRINT("Waiting for Wifi to connect");

        WiFi.mode(WIFI_STA);
        WiFi.begin(ssid.c_str(), pass.c_str());
        if ( hasConnected() ) {
            return true;
        }
    }
    //setup AP
    beginConfigMode();
    //start portal and loop
    startWebConfig();
    return false;
}
Esempio n. 2
0
//读取fash的数据 看看是否有数据
uint8_t readFlashDate()
{
  uint8_t i=0;
  
   
  readyByte(ReadBuffer);
  if(ReadBuffer[0] != '\0')
  {
    getSSID(ReadBuffer);
    i = 1; 
  }
  return i;
}
Esempio n. 3
0
//处理获取到的数据格式  格式标准: ssid+个数(一位)+路由器名+pass+个数(暂时个位)+密码
//例如:ssid2TDpass812345678
void saveFlaceDate()
{
  /**
  int stn=0;
  int i=0;
  char *substr = "ssid";
  char *subpass= "******";
  char nowssid[50]={0};  //ssid值
  char nowpass[50]={0};  //pass值
  char *ssidstr = strstr(RxBuffer1, substr);//获取 ssid
  char *passtr=strstr(ssidstr, subpass); //获取 pass后面的字符串
  stn=(*(ssidstr+4))-'0';  //获取ssid个数
  for(i=0;i <stn ;i++)   //获取ssid值
  {
    nowssid[i] = *(ssidstr+5+i);
    if((stn-1)==i){
      nowssid[i+1]='\0';
    }
  }
  stn=(*(passtr+4))-'0';  //获取pass个数
  for(i=0;i <stn ;i++)    //获取pass值
  {
    nowpass[i] = *(passtr+5+i);
    if((stn-1)==i){
      nowpass[i+1]='\0';
    }
  }
  char *ch1 = "AT+CWJAP=\"";
  char *ch3 = "\",\"";
  char *ch5 = "\"";
  char *writeBuffer = NULL;
  writeBuffer = str_contact(ch1,nowssid);
  writeBuffer = str_contact(writeBuffer,ch3);
  writeBuffer = str_contact(writeBuffer,nowpass);
  writeBuffer = str_contact(writeBuffer,ch5);
  wraitByte(writeBuffer);
  */
  getSSID(RxBuffer1);
  wraitByte(RxBuffer1);
  Delayms(10);
}
// Scan APs, use configured if found, otherwise try each open AP to get through, finally open soft AP with config page.
boolean WiFiManager::findOpenAP(const char *szUrl)
{
    int nOpen = 0;
    begin("ESP8266");
    int nScan = WiFi.scanNetworks();
    bool bFound = false;
    Serial.println("scan done");
    String sSSID = getSSID();
    int ind = 0;

    display.setFontScale2x2(false);
    if (nScan == 0) {
        Serial.println( "No APs found" );
        display.print("No APs Found");
    }
    else {
        for (int i = 0; i < nScan; ++i) // try each public AP
        {
            Serial.print(WiFi.SSID(i));
            Serial.print(" ");
            Serial.println(WiFi.encryptionType(i));
            display.print(WiFi.SSID(i));
            if(WiFi.encryptionType(i) == 7 /*&& strncmp(WiFi.SSID(i),"Chromecast",6) != 0*/)
            {
              display.drawString(128-8, 56, "O");
              nOpen++;
            }
            else if( sSSID == WiFi.SSID(i) ){ // The saved WiFi was found
              bFound  = true;
              display.drawString(128-8, 56, "<");
              Serial.println(" Cfg AP found");
            }
        }
  }

  if(nOpen == 0 && !bFound)
  {
    display.print("No open AP found");
    if(sSSID == "")
    {
       display.print("Switch to SoftAP");
       display.print("Hotspot: ESP8266");
       display.print("Goto 192.168.4.1");
    }else{
       display.print("Switching to");
       display.print(sSSID);
    }
  }

  delay(2000); // delay for reading

  if(nOpen && !bFound)
  {
    WiFi.mode(WIFI_STA);
    int counter = 0;
    for (int i = 0; i < nScan; ++i)
    {
      if(WiFi.encryptionType(i) == 7)   // run through open APs and try to connect
      {
        Serial.print("Attempting ");
        Serial.print(WiFi.SSID(i));
        display.print(String(WiFi.SSID(i)) + "...");
        WiFi.begin(WiFi.SSID(i));
        for(int n = 0; n < 50 && WiFi.status() != WL_CONNECTED; n++)
        {
          delay(200);
          Serial.print(".");
          display.clear();
          display.drawXbm(34,10, 60, 36, WiFi_Logo_bits);
          display.setColor(INVERSE);
          display.fillRect(10, 10, 108, 44);
          display.setColor(WHITE);
          drawSpinner(4, n % 4);
          display.display();
        }
        if(WiFi.status() == WL_CONNECTED)
        {
          Serial.println("Connected");
          display.print("Connected");
          if(attemptClient(szUrl))    // attemp port 80 and 8080
          {
            display.drawString(64-8, 56, "!");
            break;
          }else{
            display.drawString(64-8, 56, "X");
            WiFi.disconnect();
          }
        }
        counter++;
      }
    }
    if (WiFi.status() != WL_CONNECTED)
    {
      Serial.println("Open WiFi failed");
      Serial.println("Switch to SoftAP");
      display.print("Open WiFi failed");
      display.print("Switch to SoftAP");
      autoConnect("ESP8266");
    }
  }
  else
  {
    autoConnect("ESP8266");
  }
  return true;
}
void SmartMote::work(){
    while(1){
        /*  se l'ora corrente è di scansione                                    */
        if(!(RTCC.hours() % 6) && (RTCC.minutes() >= 0x00 && RTCC.minutes() <= 0x05)){
            AString data;
            AString message;
            AString answer;
            /*  abilito il core timer (la prima volta è già abilitato)          */
            System::wakeCoreTimer();
            /*  accendo il led verde                                            */
            turnOnGreen();
            /*  accendo la seriale                                              */
            openUART();
            /*  accendo l'i2c                                                   */
            openI2C();
            /*  prendo le misure                                                */
            data += mac();
            data += luminosity();
            data += ambientTempAndHum();
            data += groundTemp();
            data += groundHum();
            data += battey();
            /*  compongo la stringa                                             */
            message += _PHP_CHUNK1;
            /*  inserisco l'hostname                                            */
            message += getHost();
            /*  inserisco la seconda parte di richiesta http                    */
            message += _PHP_CHUNK2;
            /*  inserisco la lunghezza dei dati                                 */
            message += AString(static_cast<sint32>(data.size()));
            /*  inserisco la terza parte di richiesta http                      */
            message += _PHP_CHUNK3;
            /*  inserisco i dati                                                */
            message += data;

            /*  se fallisce l'inizializzazione dell'esp                         */
            if(!m_net.initialize()){
                /*  notifico l'errore                                           */
                error();
            }
            /*  se fallisce l'avvio del dhcp                                    */
            if(!m_net.setDhcp(true)){
                /*  notifico l'errore                                           */
                error();
            }
            /*  se fallisce la connessione alla rete                            */
            if(!m_net.joinAP(getSSID(), getKey())){
                /*  notifico l'errore                                           */
                error();
            }
            /*  se fallisce la connessione al server                            */
            if(!m_net.connectToHost(getHost(), 80)){
                /*  notifico l'errore                                           */
                error();
            }
            /*  invio i dati                                                    */
            m_net.send(message);
            /*  aspetto l'ok                                                    */
            m_net.waitForData(answer);
            /*  notifico l'ok o lerrore dell'invio                              */
            wasSuccess(answer);
            /*  lascio l'ap                                                     */
            m_net.leaveAP();
            /*  libero la memoria occupata dalle stringhe                       */
            message.clear();
            data.clear();
            answer.clear();
        }
        /*  calcolo del tempo di sleep per il wifi                              */
        m_net.sleep(getSleepTime());
        /*  punto la prossima sveglia                                           */
        setNextAlarm();
        /*  spengo il led verde                                                 */
        turnOffGreen();
        /*  spengo l'uart                                                       */
        closeUART();
        /*  chiudo l'i2c                                                        */
        closeI2C();
        /*  spengo il core timer                                                */
        System::stopCoreTimer();
        /*  vado a dormire                                                      */
        System::sleep();
            
    }
}
Esempio n. 6
0
/** join a wireless network */
bool JPWiFly::join(uint16_t timeout) {
	String ssid;
	getSSID(ssid);
	return join(ssid.c_str(), timeout);
}