A place users can post their projects. If you have a small project and would like your own dedicated place to post and have others chat about it then this is your spot.

User avatar
By xmarkx
#63254 Hello there I am trying to grab ssid and password from my android (bluetooth) and pass them to the wifi to begin a connection. It never connects to my wifi.

I have read that it needs some delay() and i added some ms but it still not working. Any ideas?

Code: Select all/*
 * Arduino home button sketch
 * Tzivaras Vasilis
 * vtzivaras@gmail.com
 *
*/

#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <SoftwareSerial.h>
#include <string.h>
#include <stdio.h>

#define BUTTONPIN D3

SoftwareSerial mySerial(D1,D2);

String Data = "";
String password1 = "";
String ssid1 = "";

int button = 0;   // 0 isrealeashed, 1 is pressed

char inChar;

int READINGSTATUS = -1;

char str[80] = "This is - www.tutorialspoint.com - website";
const char s[2] = ":";
char *token;


const char* ssid = "";
const char* password = "";

const char* host = "cemup.com";
const int httpsPort = 9002;

// Use web browser to view and copy
// SHA1 fingerprint of the certificate
const char* fingerprint = "CF 05 98 89 CA FF 8E D8 5E 5C E0 C2 E4 F7 E6 C3 C7 50 DD 5C";

void parseInput() {
  int i=0;
  int pos = 0;
 
  // Check if full message is received.
 
  for(i=0; i<Data.length(); i++) {
      str[pos] = Data[i];
      pos++;
      delay(5);
  }
 
  token = strtok(str, s);
  password1 = token;
 
  while( token != NULL ) {
    Serial.println(token );
    delay(5);
    token = strtok(NULL, s);
    ssid1 = token;
    break;
  }
  //Serial.println("");
  //Serial.print("SSID: ");
  //Serial.println(ssid1);
  //Serial.print("PASSWORD: ");
  //Serial.println(password1);
  //Serial.println("Serial input was read sucesfully.");   
}

void setup() {
 
  // Setting up Serial communication
  Serial.begin(115200);
  mySerial.begin(9600);

   // Setting up pins
  pinMode(BUTTONPIN, INPUT);
 
  // Blinking red -waiting to connect to a bluetooth device.
  // ...
 
  // Waiting to reveice data from bluetooth.
  delay(35);
  Serial.println("Program started...");
  Serial.println(micros());
  Serial.println(" Android device message:");
  while(1) {
    delay(20);
   
    if (mySerial.available() > 0) {
      char myc = mySerial.read();
      Data.concat(myc);
      Serial.print(myc);
      Serial.print(" ");
      delay(20);
      if (myc == '>') {
        break;
      }
    }
  }

  //parseInput();
  //mySerial.write("Everything is ok");
 
  //Serial.print("Android said: ");
  //Serial.println(Data);
  //Serial.println("Connecting with: ");
  //Serial.println(password1);
  //Serial.println(ssid1);
 
  //char id[ssid1.length()+1];
  //ssid1.toCharArray(id, ssid1.length()+1);
  //password.toCharArray(password1, );
  // Connecting to WIFI hotspot
  //Serial.print("SSID: ");
  //Serial.println(id);
 
  ssid = "myssid";
  password = "*****";

 
  Serial.println(micros());
  // Connecting to WIFI hotspot
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
 
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  // Use WiFiClientSecure class to create TLS connection
  WiFiClientSecure client;
  Serial.print("connecting to ");
  Serial.println(host);
  if (!client.connect(host, httpsPort)) {
    Serial.println("connection failed");
    return;
  }
/*
  if (client.verify(fingerprint, host)) {
    Serial.println("certificate matches");
  } else {
    Serial.println("certificate doesn't match");
  }
*/
  String url = "/";
  Serial.print("requesting URL: ");
  Serial.println(url);

  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "User-Agent: BuildFailureDetectorESP8266\r\n" +
               "Connection: close\r\n\r\n");

  Serial.println("request sent");
  while (client.connected()) {
    String line = client.readStringUntil('\n');
    if (line == "\r") {
      Serial.println("headers received");
      break;
    }
  }
 
  String line = client.readStringUntil('\n');
  if (line.startsWith("{\"state\":\"success\"")) {
    Serial.println("esp8266/Arduino CI successfull!");
  } else {
    Serial.println("esp8266/Arduino CI has failed");
  }
 
  Serial.println("reply was:");
  Serial.println("==========");
  Serial.println(line);
  Serial.println("==========");
  Serial.println("closing connection");
}

void loop() {
    int buttonState = digitalRead(BUTTONPIN);
    if( buttonState == 1) {
      Serial.println("Button is releashed!");
    }else {
      Serial.println("Button is pressed!");
    }
   
    if (mySerial.available() > 0) {
      //Serial.println("---> Incoming byte!");
      char myc = mySerial.read();
      Serial.print(myc);
      Serial.print(" ");
    }
    /* 
       
    inChar = mySerial.read();
      if(inChar == '<') {
        READINGSTATUS = 1;
        inChar = mySerial.read();
      }else if (inChar == '>') {
        READINGSTATUS = 0;
      }

      if (READINGSTATUS == 1) {
        Data.concat(inChar);
      }
    }
   
    if( READINGSTATUS == 0) {
      parseInput();
      READINGSTATUS = -1;
    }
    */
    delay(50);
}