Image

Image

Search This Blog

Showing posts with label arduino. Show all posts
Showing posts with label arduino. Show all posts

Monday, July 15, 2024

ESP32-CAM serve video to multiple http clients

 // Multicam v.2.19

// ESP32 has two cores, APPlication and PROcess

#define APP_CPU 1
#define PRO_CPU 0

#include "src/OV2640.h"
#include <WiFi.h>
#include <WebServer.h>
#include <WiFiClient.h>
// we should disable bt
//#include <esp_bt.h>
#include <esp_wifi.h>
#include <esp_sleep.h>
#include <driver/rtc_io.h>
//disable brownout problems
#include "soc/soc.h"           
#include "soc/rtc_cntl_reg.h"

#define CAMERA_MODEL_AI_THINKER
//those are the GPIO pins for AI_THINKER - find yours if you have a different SOC
#define PWDN_GPIO_NUM 32
#define RESET_GPIO_NUM -1
#define XCLK_GPIO_NUM 0
#define SIOD_GPIO_NUM 26
#define SIOC_GPIO_NUM 27
#define Y9_GPIO_NUM 35
#define Y8_GPIO_NUM 34
#define Y7_GPIO_NUM 39
#define Y6_GPIO_NUM 36
#define Y5_GPIO_NUM 21
#define Y4_GPIO_NUM 19
#define Y3_GPIO_NUM 18
#define Y2_GPIO_NUM 5
#define VSYNC_GPIO_NUM 25
#define HREF_GPIO_NUM 23
#define PCLK_GPIO_NUM 22
// wifi, because I'm too lazy to put it in a different file
#define SSID1 "my_WiFi"
#define PWD1 "myp455w0rd"

//init camera
OV2640 cam;

//init webserver
WebServer server(80);


// ===== rtos task handles =====
// Streaming is implemented with 3 tasks:
// handle client connections to the webserver
TaskHandle_t tMjpeg;
// handle getting picture frames from the camera and storing them locally
TaskHandle_t tCam;
// actually streaming frames to all connected clients
TaskHandle_t tStream;

// frameSync semaphore is used to prevent streaming buffer while is replaced with the next frame
SemaphoreHandle_t frameSync = NULL;

// Queue stores currently connected clients to whom we are streaming
QueueHandle_t streamingClients;

// We will try to achieve 15 FPS frame rate - for surveilllance, it is ok-ish...
const int FPS = 15;

// We will handle web client requests every 100 ms (10 Hz) - web can wait a bit
const int WSINTERVAL = 100;


// ======== Server Connection Handler Task ==========
void mjpegCB(void* pvParameters) {
  TickType_t xLastWakeTime;
  const TickType_t xFrequency = pdMS_TO_TICKS(WSINTERVAL);

  // Creating frame synchronization semaphore and initializing it
  frameSync = xSemaphoreCreateBinary();
  xSemaphoreGive(frameSync);

  // Creating a queue to track all connected clients
  streamingClients = xQueueCreate(10, sizeof(WiFiClient*));

  //=== Setup section  ===

  //  Creating RTOS task for grabbing frames from the camera
  xTaskCreatePinnedToCore(
    camCB,     // callback
    "cam",     // name
    4096,      // stacj size
    NULL,      // parameters
    2,         // priority
    &tCam,     // RTOS task handle
    APP_CPU);  // core

  //  Creating task to push the stream to all connected clients
  xTaskCreatePinnedToCore(
    streamCB,
    "strmCB",
    4 * 1024,
    NULL,  //(void*) handler,
    2,
    &tStream,
    APP_CPU);

  //  Registering webserver handling routines
  server.on("/mjpeg", HTTP_GET, handleJPGSstream);
  server.on("/jpeg", HTTP_GET, handleJPG);
  server.onNotFound(handleNotFound);

  //  Starting webserver
  server.begin();

  //=== loop() section  ====
  xLastWakeTime = xTaskGetTickCount();
  for (;;) {
    server.handleClient();

   // After every server client handling request, we let other tasks run and then pause
    taskYIELD();
    vTaskDelayUntil(&xLastWakeTime, xFrequency);
  }
}


// Commonly used variables:
volatile size_t camSize;  // size of the current frame, byte
volatile char* camBuf;    // pointer to the current frame


// ==== RTOS task to grab frames from the camera ====
void camCB(void* pvParameters) {

  TickType_t xLastWakeTime;

  //  A running interval associated with currently desired frame rate
  const TickType_t xFrequency = pdMS_TO_TICKS(1000 / FPS);

  // Mutex for the critical section of swithing the active frames around
  portMUX_TYPE xSemaphore = portMUX_INITIALIZER_UNLOCKED;

  //  Pointers to the 2 frames, their respective sizes and index of the current frame
  char* fbs[2] = { NULL, NULL };
  size_t fSize[2] = { 0, 0 };
  int ifb = 0;

  //=== loop() section  ===
  xLastWakeTime = xTaskGetTickCount();

  for (;;) {

    //  Grab a frame from the camera and query its size
    cam.run();
    size_t s = cam.getSize();

    //  If frame size is more that we have previously allocated - request  125% of the current frame space
    if (s > fSize[ifb]) {
      fSize[ifb] = s * 4 / 3;
      fbs[ifb] = allocateMemory(fbs[ifb], fSize[ifb]);
    }

    //  Copy current frame into local buffer
    char* b = (char*)cam.getfb();
    memcpy(fbs[ifb], b, s);

    //  Let other tasks run and wait until the end of the current frame rate interval (if any time left)
    taskYIELD();
    vTaskDelayUntil(&xLastWakeTime, xFrequency);

    //  Only switch frames around if no frame is currently being streamed to a client
    //  Wait on a semaphore until client operation completes
    xSemaphoreTake(frameSync, portMAX_DELAY);

    //  Do not allow interrupts while switching the current frame
    portENTER_CRITICAL(&xSemaphore);
    camBuf = fbs[ifb];
    camSize = s;
    ifb++;
    ifb &= 1;  // this should produce a 1, 0, 1, 0, 1 ... sequence
    portEXIT_CRITICAL(&xSemaphore);

    //  Let anyone waiting for a frame know that the frame is ready
    xSemaphoreGive(frameSync);

    //  Technically only needed once: let the streaming task know that we have at least one frame
    //  and it could start sending frames to the clients, if any
    xTaskNotifyGive(tStream);

    //  Immediately let other (streaming) tasks run
    taskYIELD();

    //  If streaming task has suspended itself (no active clients to stream to) there is no need to grab frames from the camera. We can save some power by suspending the tasks
    if (eTaskGetState(tStream) == eSuspended) {
      vTaskSuspend(NULL);  //NULL means "suspend yourself"
    }
  }
}


// ==== Memory allocator uses of PSRAM if present ====
char* allocateMemory(char* aPtr, size_t aSize) {

  //  Since current buffer is too small, free it
  if (aPtr != NULL) free(aPtr);

  size_t freeHeap = ESP.getFreeHeap();
  char* ptr = NULL;

  // If memory requested is more than 2/3 of the currently free heap, try PSRAM immediately
  if (aSize > freeHeap * 2 / 3) {
    if (psramFound() && ESP.getFreePsram() > aSize) {
      ptr = (char*)ps_malloc(aSize);
    }
  } else {
    //  Enough free heap - let's try allocating fast RAM as a buffer
    ptr = (char*)malloc(aSize);

    //  If allocation on the heap failed, let's give PSRAM one more chance:
    if (ptr == NULL && psramFound() && ESP.getFreePsram() > aSize) {
      ptr = (char*)ps_malloc(aSize);
    }
  }

  // Well, if the memory pointer is NULL, we were not able to allocate any memory, and that is the end. RESTART.
  if (ptr == NULL) {
    ESP.restart();
  }
  return ptr;
}


// ==== STREAMING ======
const char HEADER[] = "HTTP/1.1 200 OK\r\n"
                      "Access-Control-Allow-Origin: *\r\n"
                      "Content-Type: multipart/x-mixed-replace; boundary=123456789000000000000987654321\r\n";
const char BOUNDARY[] = "\r\n--123456789000000000000987654321\r\n";
const char CTNTTYPE[] = "Content-Type: image/jpeg\r\nContent-Length: ";
const int hdrLen = strlen(HEADER);
const int bdrLen = strlen(BOUNDARY);
const int cntLen = strlen(CTNTTYPE);


// ==== Handle connection request from clients ======
void handleJPGSstream(void) {
  //  Can only acommodate 10 clients. The limit is a default for WiFi connections
  if (!uxQueueSpacesAvailable(streamingClients)) return;


  //  Create a new WiFi Client object to keep track of this one
  WiFiClient* client = new WiFiClient();
  *client = server.client();

  //  Immediately send this client a header
  client->write(HEADER, hdrLen);
  client->write(BOUNDARY, bdrLen);

  // Push the client to the streaming queue
  xQueueSend(streamingClients, (void*)&client, 0);

  // Wake up streaming tasks if they were previously suspended:
  if (eTaskGetState(tCam) == eSuspended) vTaskResume(tCam);
  if (eTaskGetState(tStream) == eSuspended) vTaskResume(tStream);
}


// ==== Actually stream content to all connected clients ====
void streamCB(void* pvParameters) {
  char buf[16];
  TickType_t xLastWakeTime;
  TickType_t xFrequency;

  //  Wait until the first frame is captured - only after we have something to send
  ulTaskNotifyTake(pdTRUE,         /* Clear the notification value before exiting. */
                   portMAX_DELAY); /* Block indefinitely. */

  xLastWakeTime = xTaskGetTickCount();
  for (;;) {
    // Default assumption: we are running according to the FPS
    xFrequency = pdMS_TO_TICKS(1000 / FPS);

    //  Only send anything if there is someone watching
    UBaseType_t activeClients = uxQueueMessagesWaiting(streamingClients);
    if (activeClients) {
      // Adjust the period to the number of connected clients
      xFrequency /= activeClients;

      //  Since we are sending the same frame to everyone,
      //  pop a client from the the front of the queue
      WiFiClient* client;
      xQueueReceive(streamingClients, (void*)&client, 0);

      //  Check if this client is still connected.

      if (!client->connected()) {
        //  delete this client reference if it has disconnected
        //  and don't put it back on the queue anymore.
        delete client;
      } else {

        //  OK, this is an actively connected client.
        //  Let's grab a semaphore to prevent frame changes while we are serving the current
        xSemaphoreTake(frameSync, portMAX_DELAY);

        client->write(CTNTTYPE, cntLen);
        sprintf(buf, "%d\r\n\r\n", camSize);
        client->write(buf, strlen(buf));
        client->write((char*)camBuf, (size_t)camSize);
        client->write(BOUNDARY, bdrLen);

        // Since this client is still connected, push it to the end
        // of the queue for further processing
        xQueueSend(streamingClients, (void*)&client, 0);

        //  The frame has been served. Release the semaphore and let other tasks run.
        //  If there is a frame switch ready, it will happen now in between frames
        xSemaphoreGive(frameSync);
        taskYIELD();
      }
    } else {
      //  Since there are no connected clients, there is no reason to waste power running
      vTaskSuspend(NULL);
    }
    //  Let other tasks run after serving every client
    taskYIELD();
    vTaskDelayUntil(&xLastWakeTime, xFrequency);
  }
}


const char JHEADER[] = "HTTP/1.1 200 OK\r\n"
                       "Content-disposition: inline; filename=capture.jpg\r\n"
                       "Content-type: image/jpeg\r\n\r\n";
const int jhdLen = strlen(JHEADER);


// ==== Serve up one JPEG frame =========
void handleJPG(void) {
  WiFiClient client = server.client();

  if (!client.connected()) return;
  cam.run();
  client.write(JHEADER, jhdLen);
  client.write((char*)cam.getfb(), cam.getSize());
}


// ==== Handle invalid URL requests =====
void handleNotFound() {
  String message = "This camera runs fine, you are asking the wrong question!\n

you should only ask for /mjpeg or /jpeg here\n\n";
  message += "URL: ";
  message += server.uri();
  message += "\nMethod: ";
  message += (server.method() == HTTP_GET) ? "GET" : "POST";
  message += "\nArguments: ";
  message += server.args();
  message += "\n";
  server.send(200, "text / plain", message);
}


// we're at the classic setup function


void setup() {
  //disable brownout detector
  WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0);  

  // Configure the camera
  camera_config_t config;
  config.ledc_channel = LEDC_CHANNEL_0;
  config.ledc_timer = LEDC_TIMER_0;
  config.pin_d0 = Y2_GPIO_NUM;
  config.pin_d1 = Y3_GPIO_NUM;
  config.pin_d2 = Y4_GPIO_NUM;
  config.pin_d3 = Y5_GPIO_NUM;
  config.pin_d4 = Y6_GPIO_NUM;
  config.pin_d5 = Y7_GPIO_NUM;
  config.pin_d6 = Y8_GPIO_NUM;
  config.pin_d7 = Y9_GPIO_NUM;
  config.pin_xclk = XCLK_GPIO_NUM;
  config.pin_pclk = PCLK_GPIO_NUM;
  config.pin_vsync = VSYNC_GPIO_NUM;
  config.pin_href = HREF_GPIO_NUM;
  config.pin_sscb_sda = SIOD_GPIO_NUM;
  config.pin_sscb_scl = SIOC_GPIO_NUM;
  config.pin_pwdn = PWDN_GPIO_NUM;
  config.pin_reset = RESET_GPIO_NUM;
  config.xclk_freq_hz = 20000000;
  config.pixel_format = PIXFORMAT_JPEG;

  // Frame parameters: UXGA is ok if we only want a decent framerate of 15fps
    config.frame_size = FRAMESIZE_UXGA;
  //  config.frame_size = FRAMESIZE_SVGA;
  //  config.frame_size = FRAMESIZE_VGA;
  //  config.frame_size = FRAMESIZE_QVGA;
  config.jpeg_quality = 12;
  config.fb_count = 2;

  if (cam.init(config) != ESP_OK) {
    delay(10000);
    ESP.restart();
  }


  //  Configure and connect to WiFi
  WiFi.mode(WIFI_STA);
  WiFi.begin(SSID1, PWD1);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }

  // Start main streaming RTOS task
  xTaskCreatePinnedToCore(
    mjpegCB,
    "mjpeg",
    4 * 1024,
    NULL,
    2,
    &tMjpeg,
    APP_CPU);
}


// variables for wifi reconnect
unsigned long previousMillis = 0;
unsigned long interval = 30000;

void loop() {
  vTaskDelay(1000);
  //Check Wifi status
  unsigned long currentMillis = millis();
  // if WiFi is down, try reconnecting every interval mseconds
  if ((WiFi.status() != WL_CONNECTED) && (currentMillis - previousMillis >= interval)) {
    WiFi.disconnect();
    vTaskDelay(1000);
    WiFi.reconnect();
    previousMillis = currentMillis;
  }
}

Wednesday, March 17, 2021

ESP Scale with calibration and tare

 #include "HX711.h"

#include "Wire.h"

#include "Adafruit_SSD1306.h"

#include "ESP8266WiFi.h"

#include "EEPROM.h"

#include "ESP8266httpUpdate.h"


#define SCREEN_WIDTH 128 

#define SCREEN_HEIGHT 32 

#define OLED_RESET     -1 

#define SCREEN_ADDRESS 0x3C 


#define upd_version "1"

#define upd_reboot true

#define upd_server "10.11.12.13"

#define upd_file "/update/scaleupd.php"


const int LOADCELL_DOUT_PIN = D4;

const int LOADCELL_SCK_PIN = D3;

const int TarePin = D5;

const int OverPin = D6;

const int GoodPin = D7;

const int UnderPin = D8;


int CalEepromAdress = 101;

int TarEepromAdress = 111;


float known_mass = 500;

float readValue = 0;

float calValue = 6192.2;// overrided by EEPROM or/and boot calibration

float tareValue = 472; // overrided by EEPROM or/and manual tare


const char* ssid = "myWiFi";

const char* password = "53cr37p4ssw0rd"; 


HX711 scale;

WiFiClient client;

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); //oled display uses std wire lib - for nodemcu SCL is D1(4), SDA is D2(5) (see pins_arduino.h)


void setup() {

  Serial.begin(115200);

  Serial.println("\nStarting...");

  display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);

  display.clearDisplay();

  display.setTextSize(2);             

  display.setTextColor(SSD1306_WHITE);

  display.setCursor(0, 0);

  display.println("Press to  Calibrate");

  display.display(); 


  pinMode(TarePin, INPUT_PULLUP);

  pinMode(OverPin, OUTPUT);

  pinMode(GoodPin, OUTPUT);

  pinMode(UnderPin, OUTPUT);

  

  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);

  delay(200); 

  scale.set_scale();

  scale.set_scale(calValue); 

  scale.tare();

 

  int wificnt = 0;

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED && wificnt < 10) {

   Serial.print (".");

   wificnt ++;

   delay(200);

  } //try 20sec to connect to Wifi in order to perform OTA update

  ESPhttpUpdate.rebootOnUpdate(upd_reboot);

  t_httpUpdate_return ret = ESPhttpUpdate.update(upd_server, 80, upd_file, upd_version);


  EEPROM.begin(512);

  EEPROM.get(CalEepromAdress, calValue);

  EEPROM.get(TarEepromAdress, tareValue);

 delay(2000); //Giving two sec to perform calibration at boot

  if(digitalRead(TarePin) == LOW){

   Serial.println("\nIn Calibration...");

   scale.set_scale();

   scale.tare();

   digitalWrite(UnderPin, HIGH); //turning all leds ON

   digitalWrite(OverPin, HIGH);

   digitalWrite(GoodPin, HIGH);

   display.clearDisplay();

   display.setCursor(0,0); 

   display.print("Calibrate with "); display.println(known_mass);

   display.display();      

   Serial.print("Calibrate with "); Serial.println(known_mass);

   delay(2000); //Two more seconds to release the button

   while (digitalRead(TarePin) == HIGH){ delay(200); Serial.print("."); } //Wait for the button to be pushed, after putting the calibrated weight on the scale

    calValue = (scale.get_units(10)) / known_mass;

    EEPROM.put(CalEepromAdress, calValue); //Save the calibration to EEPROM

    display.clearDisplay();

    display.setCursor(0,0); 

    display.print("Calibrated to "); display.println(calValue);

    display.display();

    Serial.print("\nCalibrated to "); Serial.println(calValue);

    scale.set_scale(calValue);

    delay(1000);

  }

  

  tareValue = (scale.get_units(20)); //Perform an initial tare

  scale.set_offset(tareValue);

  scale.tare();EEPROM.put(TarEepromAdress, tareValue); //Save TARE into EEPROM - not really needed here

  EEPROM.end();

 

}


void loop() {

 display.clearDisplay();

 

 if(digitalRead(TarePin) == LOW){ //if button pressed - perform tare

  Serial.println("\nTare...");

  digitalWrite(UnderPin, HIGH); //turn all leds on

  digitalWrite(GoodPin, HIGH);

  digitalWrite(OverPin, HIGH);

  

  tareValue = (scale.get_units(20)); 

  scale.set_offset(tareValue);

  display.clearDisplay();

  display.setTextSize(2);            

  display.setCursor(0,0);  

  display.print(" TARE to  "); display.println(tareValue);

  display.display(); 

  Serial.print("TARE to "); Serial.println(tareValue);

 } //end of TARE


 readValue = (scale.get_units(10) - tareValue);

 //readValue = (scale.read_average(20) - tareValue);

 Serial.println(readValue);


 display.clearDisplay();

 display.setTextSize(4);

 display.setCursor(5, 0);

 display.print(readValue); 

 display.setTextSize(2);

 display.setCursor(116, 17);

 display.println("g");

 display.display(); 


 digitalWrite(UnderPin, LOW); //turn all leds off

 digitalWrite(GoodPin, LOW);

 digitalWrite(OverPin, LOW);


 if(readValue < 3.49){

  digitalWrite(UnderPin, HIGH); //Red Led - below 3.5g 

 }

 if(readValue > 3.48 && readValue < 3.80){

  digitalWrite(GoodPin, HIGH); //Green Led - OK

 }

 if(readValue > 3.79){

  digitalWrite(OverPin, HIGH); //Blue Led - too heavy

 }  

//  delay(100);

}

 


 

Wednesday, February 03, 2021

ESP Scale pushing to server

 include "HX711.h"

#include "ESP8266WiFi.h"

#include "EEPROM.h"

#include "ESP8266httpUpdate.h"


#define upd_version "6a"

#define upd_reboot true

#define upd_server "123.45.67.89"

#define upd_file "/update/index.php"


const int LOADCELL_DOUT_PIN = D4;

const int LOADCELL_SCK_PIN = D3;


int CalEepromAdress = 101;

int TarEepromAdress = 111;

int DevEepromAdress = 121;


float calibrationValue = 442.06;// See ScaleCalibrate.ino for this. override by EEPROM

float tareValue = 472; // override by EEPROM

String thisScale = "LC000"; // override by EEPROM


char server[] = "123.45.67.90";

String uploadfile = "process.php";

String postVariable = "data=";

const char* ssid = "MyWiFi";

const char* password = "MyP@ssword";


HX711 scale;

WiFiClient client;

ADC_MODE(ADC_VCC);


void setup() {

//  Serial.begin(115200);

 

  EEPROM.begin(512);

  EEPROM.get(CalEepromAdress, calibrationValue);

  EEPROM.get(TarEepromAdress, tareValue);

  EEPROM.get(DevEepromAdress, thisScale);

  EEPROM.end();

 

  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);

  scale.set_scale(calibrationValue); 

  

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {

   delay(200);

  }


  ESPhttpUpdate.rebootOnUpdate(upd_reboot);

  t_httpUpdate_return ret = ESPhttpUpdate.update(upd_server, 80, upd_file, upd_version);


}


void loop() {

  String postData = postVariable + thisScale + "," + ESP.getVcc() + "," + (scale.get_units(10) - tareValue) + "," + upd_version;


  if (client.connect(server, 80)) {

    client.print("POST /");

    client.print(uploadfile);

    client.println(" HTTP/1.1");

    client.print("Host: ");

    client.println (server);

    client.println("Content-Type: application/x-www-form-urlencoded");

    client.print("Content-Length: ");

    client.println(postData.length());

    client.println();

    client.print(postData);

 }

  if (client.connected()) {

      client.stop();

  }

  scale.power_down();

  ESP.deepSleep(300e6);

}

Wednesday, December 02, 2020

ESP WiFi sniffer with OLED and http upload

#include "ESP8266WiFi.h"
#include "oled.h"
ADC_MODE(ADC_VCC);
OLED Display=OLED(D1,D2);
int RawPort = A0;
char server[] = "192.168.1.101";
String uploadfile = "wifi.php";
String postVariable = "WiFi=";
const char* ssid = "MyWiFi";
const char* password = "P@55w0rd";
String total = "";
void setup() {
//  Serial.begin(115200);
//  Serial.println("");
  Display.begin();
  Display.setTTYMode(true);
  pinMode(LED_BUILTIN, OUTPUT);
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
  delay(100);
  Display.print("Battery: ");
  Display.print(ESP.getVcc());
//  Display.print("\n\rSetup done");
}
void loop() {
//  int Batt = ESP.getVcc();
//  Serial.print("Battery voltage is: "+Batt);
//  Display.print(ESP.getVcc());
//  Serial.print("Wifi scan...");
  Display.set_contrast(128);
  Display.print("\n\rScan...");
  int n = WiFi.scanNetworks();
//  Serial.println(" done");
  Display.println(" done");
  if (n == 0) {
//    Serial.println("No Networks Found");
    Display.printf("No Networks Found");
    digitalWrite(LED_BUILTIN, LOW);
  }
  else {
//    Serial.print(n);
//    Serial.println(" Networks found");
    digitalWrite(LED_BUILTIN, HIGH);
    Display.set_contrast(8);
    Display.printf("%d networks found",n);
    String nr = String(n) + " APs detected. ";
    for (int i = 0; i < n; ++i) {
//      Serial.print(i + 1);
//      Serial.print(": ");
//      Serial.print(WiFi.SSID(i));
//      Serial.print(" RSSI: ");
//      Serial.print(WiFi.RSSI(i)); 
//      Serial.print(" MAC: ");
//      Serial.print(WiFi.BSSIDstr(i));
//      Serial.print(" Enc: ");
//      Serial.println(encType(i));
    Display.println();
    Display.print(i + 1);
    Display.print(": ");
    Display.print(WiFi.SSID(i));
    Display.print(" (");
    Display.print(WiFi.RSSI(i));
    Display.println(")");
    Display.print("   ");
    Display.print(WiFi.BSSIDstr(i));
    total = total+String(i+1)+": "+String(WiFi.SSID(i))+" "+String(WiFi.BSSIDstr(i))+" "+String(WiFi.RSSI(i))+"db "+String(encType(i))+". ";
    delay(500);
    }
 
    WiFi.scanDelete();
    delay (500);
    Display.println("\n\rSleep for 1min...");
    
    WiFiClient client;
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
     delay(500);
    }
    
    
    String postData = postVariable + nr + total;
//    Serial.print("Transmitted data: ");
//    Serial.println(postData);
    
    if (client.connect(server, 80)) {
     client.print("POST /");
     client.print(uploadfile);
     client.println(" HTTP/1.1");
     client.print("Host: ");
     client.println (server);
     client.println("Content-Type: application/x-www-form-urlencoded");
     client.print("Content-Length: ");
     client.println(postData.length());
     client.println();
     client.print(postData);
    }
    if (client.connected()) {
      client.stop();
    }
//   Serial.println("Sleeping ");
   Display.set_power(false);
//   delay(10000);
   ESP.deepSleep(6e+7); // 60 sec  
  }
}
// Readable Encryption Type
String encType(int id){
String type;
  if(WiFi.encryptionType(id) == ENC_TYPE_WEP){ type=" WEP";
  }else if(WiFi.encryptionType(id) == ENC_TYPE_TKIP){ type="WPA-TKIP";    
  }else if(WiFi.encryptionType(id) == ENC_TYPE_CCMP){ type="WPA2-CCMP";    
  }else if(WiFi.encryptionType(id) == ENC_TYPE_AUTO){ type="WPA-WPA2";    
  }else if(WiFi.encryptionType(id) == ENC_TYPE_NONE){ type="OPEN";    
  }
  return type;
}




$cat wifi.php
<?php
$file = fopen("/var/www/html/wifi.txt", "a+") or die("Unable to open file!");
date_default_timezone_set("America/New_York");
$time = date('Y-m-d_H:i:s');
$read = $_POST["WiFi"];
$data = "{$time} - {$read}\n";
fwrite($file, $data);
fclose($file);
?>




Blog Archive