Hello all. Get ready to hate me :o
I have been playing around with an Adafruit Huzzah Feather ESP8266 module. Very nice.
I ma TRYING to get my head around clients, servers etc... it's happening slowly.
// DEMO V1
// Huzzah Feather ESP8266 module
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 display = Adafruit_SSD1306(128, 32, &Wire);
const char *ssid = "Demo"; //Set the network name
const char *password = "password"; //Set the network password
float timer;
#define LED 14
#define BUTTON 12
ESP8266WebServer server(80);
//------------------------------------------------------------------------------
void setup() {
Serial.begin(115200);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally + Address 0x3C for 128x32
pinMode(LED, OUTPUT);
pinMode(BUTTON,INPUT);
Serial.println("OLED begun");
display.clearDisplay();// Clear the buffer.
display.display();
delay(1000);
Serial.println();
Serial.println("Configuring access point...");
//WiFi.softAP(ssid, password, channel, hidden, max_connection)
WiFi.softAP(ssid, password, 1, false, 8); // Channel = 1-13. Default is channel 1.
// Hidden: True will hide ssid.
// Max connection. Number of stations 0-8. Default is 4
// ip address default is 192.168.4.1
IPAddress local_IP(192,168,1,200); // Set our ip address
IPAddress gateway(192,168,4,200);
IPAddress subnet(255,255,255,0);
WiFi.softAPConfig(local_IP, gateway, subnet); //Setup the network
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
server.on("/", handleRoot);
server.begin();
Serial.println("HTTP server started");
Serial.println("");
display.setTextSize(3);
display.setTextColor(WHITE);
display.drawRoundRect(0, 1, 128, 31, 4, WHITE);
display.setCursor(2,6);
display.print("TEST");
display.display();
delay(2000);
setup_main_page ();
Check_stations ();
}
//=====================================================================================
void loop() {
server.handleClient();
timer++;
if (timer>200000) {digitalWrite(LED, HIGH);Check_stations();timer=0;delay(50);digitalWrite(LED, LOW);} //Check for new stations
if (digitalRead(BUTTON)==LOW) {
Serial.println("Button pressed");
server.send(200, "text/html", "<h1>Button on ESP8266 pressed</h1>");
display.clearDisplay();
display.setTextSize(3);
display.setTextColor(WHITE);
display.setCursor(10,6);
display.print("BUTTON");
display.display();
delay(1500);
setup_main_page ();
}
}
//=====================================================================================
void Check_stations (){
display.fillRect(114, 23, 128, 34, BLACK); //Clear the last digit
Serial.printf("Stations connected:= %d\n", WiFi.softAPgetStationNum()); //Display how many Stations are connected to the AP
display.setCursor(115,24);
display.print(WiFi.softAPgetStationNum()); //Display how many Stations are connected to the AP
display.display();
}
//-------------------------------------------------------------------------------------
void handleRoot() {
server.send(200, "text/html", "<h1>Welcome to Test network. Broadcasting across Wiltshire using Huzzah Feather ESP8266. Version 2</h1>");
}
//--------------------------------------------------------------------------------------
void setup_main_page (){
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0,0);
display.println("Test is Broadcasting");
display.setCursor(0,12);
display.println("IP: 192.168.1.200");
display.setCursor(0,24);
display.println("STATIONS CONNECTED: ");
display.display();
}
This code works. I can log into the webpage and see the text stating that its broadcasting across Wiltshire (well... 8 feet from my desk).
The Oled screen is for debugging - also working fine.
But, when you press the button, it serial.prints the fact that the button has been pressed, but does not send the message to the webpage using the line:
server.send(200, "text/html", "
Button on ESP8266 pressed
");Why is that. What am I missing/doing wrong?
Thanks