SHARE
TWEET

Untitled

a guest Dec 5th, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ESP8266HTTPClient.h>
  2. #include <ESP8266WiFi.h>
  3.  
  4. String ssid = "Az";
  5. String pass = "gonoreq5";
  6.  
  7. char** data;
  8.  
  9. int size = 0;
  10. int cap = 2;
  11.  
  12. WiFiServer server(80);
  13.  
  14. HTTPClient http;
  15.  
  16. void setup(){
  17.   Serial.begin(9600);
  18.  
  19.   WiFi.mode(WIFI_STA);
  20.   WiFi.begin(ssid, pass);
  21.   server.begin();
  22.   while (WiFi.status() != WL_CONNECTED) {
  23.     delay(500);
  24.     Serial.println(".");
  25.   }
  26.  
  27.   Serial.println(WiFi.localIP());
  28.  
  29.   data = (char **)malloc(sizeof(char[12]) * cap);
  30. }
  31.  
  32. void add(String cardGiven){
  33.   char card[12];
  34.   cardGiven.toCharArray(card, 12);
  35.  
  36.   for(int i=0; i<size; i++){
  37.     if(strcmp(data[i], card) == 0) return;
  38.   }
  39.  
  40.   data[size] = (char *) malloc(sizeof(char[12]));
  41.   cardGiven.toCharArray(data[size], 12);
  42.  
  43.   size++;
  44.  
  45.   if(size == cap-1){
  46.     cap += 3;
  47.     data = (char **) realloc(data, sizeof(char[12]) * cap);
  48.  
  49.     if(data == NULL)Serial.println("QJ PISHKI GEI SMOTAN NAUCHI SE DA KODISH PEERAS NAEBAN");
  50.   }
  51.  
  52.  
  53.   Serial.println("-------------");
  54.   for(int i=0; i<size; i++){
  55.     Serial.println(String(data[i]));
  56.   }
  57.   Serial.println("-------------");
  58. }
  59.  
  60. String headerS;
  61.  
  62. void loop(){
  63.   WiFiClient client = server.available();   // Listen for incoming clients
  64.  
  65.   if (client) {                             // If a new client connects,
  66.     String currentLine = "";                // make a String to hold incoming data from the client
  67.     while (client.connected()) { // loop while the client's connected
  68.       //currentTime = millis();        
  69.       if (client.available()) {             // if there's bytes to read from the client,
  70.         char c = client.read();             // read a byte
  71.         headerS += c;
  72.         if (c == '\n') {                    // if the byte is a newline character
  73.           // if the current line is blank, you got two newline characters in a row.
  74.           // that's the end of the client HTTP request, so send a response:
  75.           if (currentLine.length() == 0) {
  76.             client.println("HTTP/1.1 200 OK");
  77.             client.println("Content-type: text/plain");
  78.            
  79.            
  80.             if (headerS.indexOf("GET /test") >= 0){
  81.               client.println("Content-Length: 3");
  82.               client.println("Connection: close");
  83.               client.println();
  84.               client.println("GET");
  85.             }else if(headerS.indexOf("POST /add") >= 0){
  86.               int content_length = 0, index = headerS.indexOf("Content-Length:"), i;
  87.              
  88.               for(i = index; headerS[i] != '\n'; i++);
  89.  
  90.               content_length = headerS.substring(index+16, i).toInt();
  91.  
  92.               delay(10);
  93.  
  94.               String data = "";
  95.  
  96.               for(int ii=0; ii<content_length; ii++){
  97.                 data += (char)client.read();
  98.               }
  99.  
  100.               for(int i=0; i<data.length(); i++){
  101.                 if((data[i] > 90 || data[i] < 48) && data[i] != 32){
  102.                   data = "";
  103.                   break;        
  104.                 }
  105.               }
  106.  
  107.  
  108.               Serial.println(">>" + data);
  109.  
  110.               if(data.length() == 11)add(data);
  111.              
  112.  
  113.               client.println("Content-Length: 2");
  114.               client.println("Connection: close");
  115.               client.println();
  116.               if(data == "")client.println("ne");
  117.               else client.println("da");
  118.             }
  119.            
  120.             // The HTTP response ends with another blank line
  121.             client.println();
  122.             // Break out of the while loop
  123.             break;
  124.           } else { // if you got a newline, then clear currentLine
  125.             currentLine = "";
  126.           }
  127.         } else if (c != '\r') {  // if you got anything else but a carriage return character,
  128.           currentLine += c;      // add it to the end of the currentLine
  129.         }
  130.       }
  131.     }
  132.     // Clear the header variable
  133.     headerS = "";
  134.     // Close the connection
  135.     client.stop();
  136.   }
  137. }
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145. /* GET
  146.   http.begin("http://jsonplaceholder.typicode.com/users/1");
  147.   int httpCode = http.GET();
  148.  
  149.   String response = http.getString();
  150.  
  151.   http.end();
  152.   */
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top