domingo, 18 de setembro de 2016

Servidor web no ESP-07 usando wittyboard como programador.

C++ code colored by C++2HTML Veja o servidor em ação:

Sketch do arduino adaptado de https://www.arduino.cc/en/Tutorial/WiFiWebServer:

/*
   Tentativa de usar o wifi do witty board
   tentativa 6 baseado na tentativa 5
  objetivo: levantar um servidor web.
  levantou, funcionou, tirei foto (mas nao dah para incluir aqui)
  O copy da pagina no browser deu:
  analog input 0 is 266
analog input 1 is 0
analog input 2 is 1023
analog input 3 is 0
analog input 4 is 1023
analog input 5 is 1023

olha so que fantastico o que o dah para ver no lado do servidor:

SSID set: 0
Scanning available networks...
** Scan Networks **
number of available networks:5
0) Rxxxxx Lxxxx Axxxx  Signal: -91 dBm Encryption: Auto
1) Exxxx Lxxxx  Signal: -91 dBm Encryption: WEP
2) Exxxxxx  Signal: -90 dBm Encryption: Auto
3) andro  Signal: -86 dBm Encryption: Auto
4) 122  Signal: -92 dBm Encryption: WPA
192.168.0.101
new client
GET / HTTP/1.1
Host: 192.168.0.101
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:47.0) Gecko/20100101 Firefox/47.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*//*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Cache-Control: max-age=0

client disonnected
Connected


-- 18.09.2016
Consegui usar com o ESP-7.
A saida pela serial:
Connected
MAC: 71:2B:2:7F:CF:5C
Mode: STA
PHY mode: N
Channel: 11
AP id: 0
Status: 5
Auto connect: 1
SSID (5): andro
Passphrase (15): kkkkkk
BSSID set: 0
Scanning available networks...
** Scan Networks **
number of available networks:3
0) Exxx Lxxxx  Signal: -93 dBm Encryption: WEP
1) Edxxxx  Signal: -76 dBm Encryption: Auto
2) andro  Signal: -72 dBm Encryption: Auto
192.168.0.107
Connected

dump do browser:
analog input 0 is 218
analog input 1 is 0
analog input 2 is 1023
analog input 3 is 0
analog input 4 is 0
analog input 5 is 0

*/

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

#if 0
char ssid[] = "andro";     //  your network SSID (name)
char pass[] = "kkkkkkk";  // your network password
#else
char ssid[] = "G4_8450";     //  your network SSID (name)
char pass[] = "12345678";  // your network password
#endif

wl_status_t status = WL_DISCONNECTED;  /* enum definido em
  fabio@fabio-PORTEGE-M400:~/.arduino15/packages/esp8266/hardware/
  esp8266/2.3.0/libraries/ESP8266WiFi/src/include/wl_definitions.h
*/

WiFiServer server(80);
int no_server=1;

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue:
    while (true);
  }

  // Print WiFi MAC address:
  printMacAddress();
  WiFi.mode(WIFI_STA);
}

void loop() {
  // Print WiFi MAC address:
  printMacAddress();
  WiFi.printDiag(Serial);
  // scan for existing networks:
  Serial.println("Scanning available networks...");
  listNetworks();
  if ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network:
    status = WiFi.begin(ssid, pass); // este status nao informa se
    // existe handshake de conexao em progresso
  } else {
    IPAddress ip;
    ip = WiFi.localIP();
    Serial.println (ip);
    /*https://www.arduino.cc/en/Tutorial/WiFiWebServer */
    if (no_server) {
      server.begin();
      no_server=0;
    }
    WiFiClient client = server.available();
    if (client) {
      Serial.println("new client");
      // an http request ends with a blank line
      boolean currentLineIsBlank = true;
      while (client.connected()) {
        if (client.available()) {
          char c = client.read();
          Serial.write(c);
          // if you've gotten to the end of the line (received a newline
          // character) and the line is blank, the http request has ended,
          // so you can send a reply
          if (c == '\n' && currentLineIsBlank) {
            // send a standard http response header
            client.println("HTTP/1.1 200 OK");
            client.println("Content-Type: text/html");
            client.println("Connection: close");  // the connection will be closed after completion of the response
            client.println("Refresh: 5");  // refresh the page automatically every 5 sec
            client.println();
            client.println("<!DOCTYPE HTML>");
            client.println("<html>");
            // output the value of each analog input pin
            for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
              int sensorReading = analogRead(analogChannel);
              client.print("analog input ");
              client.print(analogChannel);
              client.print(" is ");
              client.print(sensorReading);
              client.println("<br />");
            }
            client.println("</html>");
            break;
          }
          if (c == '\n') {
            // you're starting a new line
            currentLineIsBlank = true;
          }
          else if (c != '\r') {
            // you've gotten a character on the current line
            currentLineIsBlank = false;
          }
        }
      }
      // give the web browser time to receive the data
      delay(1);

      // close the connection:
      client.stop();
      Serial.println("client disonnected");
      /*****/
    }
  }
  printStatus (status);
  delay(10000);
  status = WiFi.status(); // consulta o status novamente para
  // verificar se o handshake terminou com sucesso.

}

void printMacAddress() {
  // the MAC address of your Wifi shield
  byte mac[6];

  // print your MAC address:
  WiFi.macAddress(mac);
  Serial.print("MAC: ");
  Serial.print(mac[5], HEX);
  Serial.print(":");
  Serial.print(mac[4], HEX);
  Serial.print(":");
  Serial.print(mac[3], HEX);
  Serial.print(":");
  Serial.print(mac[2], HEX);
  Serial.print(":");
  Serial.print(mac[1], HEX);
  Serial.print(":");
  Serial.println(mac[0], HEX);
}

void listNetworks() {
  // scan for nearby networks:
  Serial.println("** Scan Networks **");
  int numSsid = WiFi.scanNetworks();
  if (numSsid == -1) {
    Serial.println("Couldn't get a wifi connection");
    while (true);
  }

  // print the list of networks seen:
  Serial.print("number of available networks:");
  Serial.println(numSsid);

  // print the network number and name for each network found:
  for (int thisNet = 0; thisNet < numSsid; thisNet++) {
    Serial.print(thisNet);
    Serial.print(") ");
    Serial.print(WiFi.SSID(thisNet));
    Serial.print("\tSignal: ");
    Serial.print(WiFi.RSSI(thisNet));
    Serial.print(" dBm");
    Serial.print("\tEncryption: ");
    printEncryptionType(WiFi.encryptionType(thisNet));
  }
}

void printEncryptionType(int thisType) {
  // read the encryption type and print out the name:
  switch (thisType) {
    case ENC_TYPE_WEP:
      Serial.println("WEP");
      break;
    case ENC_TYPE_TKIP:
      Serial.println("WPA");
      break;
    case ENC_TYPE_CCMP:
      Serial.println("WPA2");
      break;
    case ENC_TYPE_NONE:
      Serial.println("None");
      break;
    case ENC_TYPE_AUTO:
      Serial.println("Auto");
      break;
  }
}

void printStatus (wl_status_t st) {
  // string message of the status
  switch (st) {
    case WL_NO_SHIELD:
      Serial.println("No shield (should not happen on ESP/WITTYBOARD/NODEMCU)");
      break;
    case WL_IDLE_STATUS:
      Serial.println("Idle status");
      break;
    case WL_NO_SSID_AVAIL:
      Serial.println("No SSID Available");
      break;
    case WL_SCAN_COMPLETED:
      Serial.println("Scan Completed");
      break;
    case WL_CONNECTED:
      Serial.println("Connected");
      break;
    case WL_CONNECT_FAILED:
      Serial.println("CONNECTION FAILED");
      break;
    case WL_CONNECTION_LOST:
      Serial.println("CONNECTION LOST");
      break;
    case WL_DISCONNECTED:
      Serial.println("DISCONNECTED");
      break;
    default:
      Serial.print(st);
      Serial.println("unknown status");
      break;
  }
}

Nenhum comentário:

Postar um comentário