Advertisement
sonicvinyes

Untitled

Dec 1st, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. boolean datos = false;
  2.  
  3. #include <WiFi.h>
  4. #include <HTTPClient.h>
  5.  
  6. const char* ssid = "ssid";
  7. const char* password = "pass";
  8.  
  9. //removed the password and ssid for security, it was there before.
  10. void setup() {
  11.   pinMode(2, OUTPUT);
  12.   Serial.begin(115200);
  13.  
  14.   while (! Serial);
  15.   Serial.println("===================================================");
  16.   Serial.println("Welcome to the menu, choose what do you want to do:");
  17.   Serial.println("1º Connect to the Wifi | W");
  18.   Serial.println("2º Scan Wifi | S");
  19.   Serial.println("3º Send data to Website | D");
  20.   Serial.println("===================================================");
  21.   Serial.println("Made by Francine and Javi.");
  22.   Serial.println("===================================================");
  23. }
  24.  
  25. void scanwifi() {
  26.   int n = WiFi.scanNetworks();
  27.   Serial.println("===================================================");
  28.   Serial.println("Scan have been finished.");
  29.   Serial.println("===================================================");
  30.   if (n == 0) {
  31.     Serial.println("===================================================");
  32.     Serial.println("No networks have been found.");
  33.     Serial.println("===================================================");
  34.   }  
  35.   else
  36.   {
  37.     Serial.print(n);// imprimir en el puerto serial el # de redes encontradas
  38.     Serial.println(" networks have been found.");
  39.     for (int i = 0; i < n; ++i)  {
  40.       // SSID: Nombre de la red; RSSI: Potencia de la red en dBm
  41.       Serial.print(i + 1);//iniciamos en la red # 1...
  42.       Serial.print(": ");
  43.       Serial.print(WiFi.SSID(i));//Imprime el nombre de la red
  44.       Serial.print(" (");
  45.       Serial.print(WiFi.RSSI(i));//Imprime la potencia de la señal en dBm
  46.       Serial.print(")");
  47.       byte encryption = WiFi.encryptionType(i);// en la variable encryption, guardamos el tipo de encriptación de la red
  48.       Serial.print(" Encryption Type: ");
  49.       Serial.println(encryption,HEX);// imprimimos el tipo de encriptación: 2: WPA, 5: WEP, etc.
  50.       delay(10);    
  51.     }
  52.   }
  53. }
  54.  
  55. void conectarsealwifi() {
  56.   Serial.begin(115200);
  57.  
  58.   WiFi.begin(ssid, password);
  59.  
  60.   while (WiFi.status() != WL_CONNECTED) {
  61.     delay(500);
  62.    
  63.     Serial.println("Connecting to WiFi..");
  64.   }
  65.  
  66.   Serial.println("Connected to the WiFi network");
  67.   delay(1000);
  68.   setup();
  69. }
  70.  
  71.  
  72. void loop() {
  73.       if (Serial.available()){
  74.     char ch = Serial.read();
  75.         if (ch == 'w' || ch == 'W'){
  76.             conectarsealwifi();
  77.         }
  78.     if (ch == 'S' || ch == 's'){
  79.       scanwifi();
  80.     }
  81.     if (ch == 'd' || ch == 'D'){
  82.       datos = true;
  83.       Serial.println(datos);
  84.     }
  85.     if (datos==true){ // si esta habilitado el sending de datos haz
  86.       //randomN = random(200);
  87.       if(WiFi.status()== WL_CONNECTED){
  88.        digitalWrite(2, HIGH);
  89.         HTTPClient http;
  90.  
  91.         http.begin("removedtheurl"); //Specify the URL
  92.         int httpCode = http.GET();                                        //Make the request
  93.  
  94.         if (httpCode > 0) { //Check for the returning code
  95.  
  96.         String payload = http.getString();
  97.         Serial.println(httpCode);
  98.         Serial.println(payload);
  99.         Serial.println("===================================================");
  100.        
  101.       }
  102.     else {
  103.       Serial.println("Error on HTTP request, reminder maybe its cloudflare shit.");
  104.     }
  105.     http.end(); //Free the resources
  106.     digitalWrite(2, LOW);
  107.   }
  108.   delay(2000);
  109.  
  110. }
  111.     else
  112.     {
  113.      
  114.     }
  115.   }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement