Skip to content

Instantly share code, notes, and snippets.

@compilerexe
Last active August 29, 2015 14:27
Show Gist options
  • Save compilerexe/8c332ad2a2335a41cac8 to your computer and use it in GitHub Desktop.
Save compilerexe/8c332ad2a2335a41cac8 to your computer and use it in GitHub Desktop.
ESP8266-12 Config wifi on web browser / send message to MQTT
#include <PubSubClient.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
/*=== OLED ===*/
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <ESP_Adafruit_SSD1306.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2
#define LOGO16_GLCD_HEIGHT 16
#define LOGO16_GLCD_WIDTH 16
int SizeArrayChicken = 0;
byte chickenCode [] = {
0x00, 0x01, 0x83, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x8F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
0x9E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xBC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xF8, 0x00,
0x00, 0x00, 0x00, 0x00, 0x03, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0xFF, 0xFF, 0xFF, 0xFC,
0x00, 0x00, 0x3F, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x7C, 0x00, 0x00, 0x00, 0x0F, 0x80, 0x00,
0xF0, 0x00, 0x00, 0x00, 0x03, 0xC0, 0x01, 0xE0, 0x00, 0x00, 0x00, 0x01, 0xE0, 0x01, 0xC0, 0x00,
0x00, 0x00, 0x00, 0xE0, 0x03, 0x80, 0x00, 0x00, 0x00, 0x00, 0x70, 0x03, 0x80, 0x00, 0x00, 0x00,
0x00, 0x70, 0x03, 0x80, 0x00, 0x00, 0x07, 0xC0, 0x70, 0x03, 0x80, 0x00, 0x00, 0x0F, 0xE0, 0x70,
0x03, 0x80, 0x00, 0x00, 0x1E, 0xE0, 0x70, 0x03, 0x80, 0x00, 0x00, 0x1C, 0x70, 0x70, 0x03, 0x80,
0x00, 0x00, 0x1C, 0x70, 0x7E, 0x03, 0x80, 0x00, 0x00, 0x1C, 0xE0, 0x3F, 0x03, 0x80, 0x00, 0x00,
0x0F, 0xE0, 0x0F, 0x03, 0x80, 0x00, 0x00, 0x07, 0xC0, 0x1E, 0x03, 0x80, 0x00, 0x00, 0x00, 0x00,
0x3C, 0x03, 0x80, 0x00, 0x00, 0x00, 0x00, 0x38, 0x03, 0x80, 0x00, 0x00, 0x00, 0x00, 0x70, 0x03,
0x80, 0x00, 0x00, 0x00, 0x00, 0x70, 0x03, 0x80, 0x00, 0x00, 0x00, 0x00, 0x70, 0x03, 0x80, 0x00,
0x00, 0x00, 0x00, 0x70, 0x03, 0x80, 0x00, 0x00, 0x00, 0x00, 0x70, 0x03, 0x80, 0x00, 0x00, 0x00,
0x00, 0x70, 0x03, 0x80, 0x00, 0x00, 0x00, 0x00, 0x70, 0x03, 0x80, 0x00, 0x00, 0x00, 0x00, 0x70,
0x03, 0x80, 0x00, 0x00, 0x00, 0x00, 0x70, 0x03, 0x80, 0x00, 0x00, 0x00, 0x00, 0x70, 0x03, 0x80,
0x00, 0x00, 0x00, 0x00, 0x70, 0x03, 0x80, 0x00, 0x00, 0x00, 0x00, 0x70, 0x03, 0x80, 0x00, 0x00,
0x00, 0x00, 0x70, 0x03, 0x80, 0x00, 0x00, 0x00, 0x00, 0x70, 0x03, 0x80, 0x00, 0x00, 0x00, 0x00,
0x70, 0x03, 0x80, 0x00, 0x00, 0x00, 0x00, 0x70, 0x03, 0x80, 0x00, 0x00, 0x00, 0x00, 0x70, 0x03,
0x80, 0x00, 0x00, 0x00, 0x00, 0x70, 0xE3, 0x80, 0x00, 0x00, 0x00, 0x00, 0x70, 0xE3, 0x80, 0x00,
0x00, 0x00, 0x00, 0x70, 0xFF, 0x80, 0x00, 0x00, 0x00, 0x00, 0x70, 0xFF, 0x00, 0x00, 0x00, 0x00,
0x00, 0x70, 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70,
0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x3C, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0x1F, 0x00,
0x00, 0x00, 0x00, 0x01, 0xE0, 0x0F, 0xE0, 0x00, 0x00, 0x00, 0x03, 0xC0, 0x03, 0xFC, 0x00, 0x00,
0x00, 0x0F, 0x80, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x0F, 0xFF, 0xFF, 0xFF, 0xFC,
0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x38, 0x70, 0x00, 0x00, 0x00,
0x00, 0x00, 0x30, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00,
0x70, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x18,
0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0x1C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x18, 0x00, 0x00
};
/*=== END OLED ===*/
/*=== WiFiAccessPoint ===*/
const char* ssidAP = "esp1"; // Change your name access point
const char* ssidPass = ""; // Change your password access point
const char* ap_ip[4] = {"192", "168", "10", "100"}; // Change your ip local network
const char* ap_subnet[4] = {"255", "255", "255", "0"}; // Change your subnet local network
const char* ap_gateway[4] = {"192", "168", "10", "1"}; // Change your gateway local network
const char* wifi_ip[4] = {"192", "168", "0", "100"}; // Change your ip local network
const char* wifi_subnet[4] = {"255", "255", "255", "0"}; // Change your subnet local network
const char* wifi_gateway[4] = {"192", "168", "0", "1"}; // Change your gateway local network
const char* ssid = "";
const char* password = "";
uint8_t countConnect = 0;
uint8_t state_internet = 0;
uint8_t state_move = 0;
bool state_mqtt = false;
bool readMessage = true;
#define BUFFER_SIZE 100
ESP8266WiFiClass WiFiClass;
ESP8266WebServer server(80);
String getSSID = "";
String getPASS = "";
String getMode = "";
const char* foundWiFi[20];
IPAddress serverMQTT(0, 0, 0, 0);
WiFiClient wclient;
PubSubClient client(wclient, serverMQTT);
void WebServer_Config() {
server.on("/", webserver_display);
server.begin();
Serial.println("HTTP server started");
}
void webserver_display() {
if (state_internet == 0) {
String codeHtml = "\
<html>\
<head>\
<meta name='viewport' content='initial-scale=1.0, user-scalable=no'>\
<title>WiFi Config</title>\
</head>\
<body>\
<form method='get'>\
SSID&ensp;\
<select name='SSID'>";
if (foundWiFi[0] == "")
codeHtml += "no networks found";
else
{
for (uint8_t i = 0; i <= 20; ++i)
{
codeHtml += "<option value='";
codeHtml += foundWiFi[i];
codeHtml += "''>";
codeHtml += foundWiFi[i];
codeHtml += "</option>";
delay(10);
}
}
codeHtml += "\
</select><br><br>\
PASS&ensp;<input type='text' name='password'><br><br>\
<input type='submit' value='Connect'>\
</form>\
</body>\
</html>";
server.send(200, "text/html", codeHtml);
}
if (server.arg(0) != "" && server.arg(1) != "") {
getSSID = server.arg(0);
getPASS = server.arg(1);
Serial.print("SSID : ");
Serial.println(getSSID);
Serial.print("PASS : ");
Serial.println(getPASS);
ssid = getSSID.c_str();
password = getPASS.c_str();
WiFi.begin(ssid, password);
// WiFi.config(
// IPAddress(atoi(wifi_ip[0]), atoi(wifi_ip[1]), atoi(wifi_ip[2]), atoi(wifi_ip[3])),
// IPAddress(atoi(wifi_gateway[0]), atoi(wifi_gateway[1]), atoi(wifi_gateway[2]), atoi(wifi_gateway[3])),
// IPAddress(atoi(wifi_subnet[0]), atoi(wifi_subnet[1]), atoi(wifi_subnet[2]), atoi(wifi_subnet[3]))
// );
while (WiFi.status() != WL_CONNECTED && countConnect != 30) {
delay(500);
Serial.print(".");
countConnect++;
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println();
Serial.println("WiFi connected");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address : ");
Serial.println(WiFi.localIP());
byte mac[6];
WiFiClass.macAddress(mac);
Serial.print("Mac Address : ");
Serial.print(mac[0],HEX);
Serial.print(":");
Serial.print(mac[1],HEX);
Serial.print(":");
Serial.print(mac[2],HEX);
Serial.print(":");
Serial.print(mac[3],HEX);
Serial.print(":");
Serial.print(mac[4],HEX);
Serial.print(":");
Serial.println(mac[5],HEX);
MessageConnect();
state_internet = 1;
} else {
Serial.println();
Serial.println("WiFi connect failed");
countConnect = 0;
}
}else {
// internetConnected();
}
}
// void internetConnected() {
// String codeHtml = "<html>\
// <head>\
// <title>WiFi Config</title>\
// <link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css'>\
// </head>\
// <body>\
// <nav class='navbar navbar-default'>\
// <ul class='nav navbar-nav'>\
// <li class='active'><a href='#'>Display Mode</li>\
// </ul>\
// </nav>\
// <div class='container'>\
// <div class='row'>\
// <div class='col-md-12'>\
// <div class='panel panel-info'>\
// <div class='panel-body'>\
// <div class='form-group'>\
// <form method='get'>\
// <input type='hidden' name='Temperature' value='Temperature'>\
// <input type='submit' class='btn btn-info' value='Temperature'>\
// </form>\
// </div>\
// </div>\
// </div>\
// </div>\
// </div>\
// </div>\
// </body>\
// </html>";
// server.send(200, "text/html", codeHtml);
// getMode = server.arg(0);
// // Serial.println(getMode);
// if (getMode == "Temperature") {
// Serial.println("Mode Temperature");
// }
// }
void WiFi_AP() {
WiFi.softAP(ssidAP);
WiFi.softAPConfig(
IPAddress(atoi(ap_ip[0]), atoi(ap_ip[1]), atoi(ap_ip[2]), atoi(ap_ip[3])),
IPAddress(atoi(ap_gateway[0]), atoi(ap_gateway[1]), atoi(ap_gateway[2]), atoi(ap_gateway[3])),
IPAddress(atoi(ap_subnet[0]), atoi(ap_subnet[1]), atoi(ap_subnet[2]), atoi(ap_subnet[3]))
);
Serial.println();
Serial.println("SmartEmo1");
Serial.print("WiFi AP : ");
Serial.print(ssidAP);
Serial.println();
IPAddress myIP = WiFi.softAPIP();
Serial.print("IP Address : ");
Serial.println(myIP);
byte mac[6];
WiFiClass.macAddress(mac);
Serial.print("Mac Address : ");
Serial.print(mac[0],HEX);
Serial.print(":");
Serial.print(mac[1],HEX);
Serial.print(":");
Serial.print(mac[2],HEX);
Serial.print(":");
Serial.print(mac[3],HEX);
Serial.print(":");
Serial.print(mac[4],HEX);
Serial.print(":");
Serial.println(mac[5],HEX);
}
void WiFiScan() {
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
int n = WiFi.scanNetworks();
if (n == 0)
Serial.println("no networks found");
else
{
Serial.print(n);
Serial.println("networks found");
for (int i = 0; i < n; i++)
{
foundWiFi[i] = WiFi.SSID(i);
delay(10);
}
}
}
void callback(const MQTT::Publish& pub) {
Serial.print(pub.topic());
Serial.print(" => ");
if (pub.has_stream()) {
uint8_t buf[BUFFER_SIZE];
int read;
while (read = pub.payload_stream()->read(buf, BUFFER_SIZE)) {
Serial.write(buf, read);
}
pub.payload_stream()->stop();
Serial.println("");
} else {
Serial.println(pub.payload_string());
}
// MQTT::Publish newpub("SmartEmo1/data", pub.payload(), pub.payload_len());
// client.publish(newpub);
}
void setup(void) {
Serial.begin(115200);
pinMode(14, INPUT);
WiFiScan();
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
Messagebegin();
WiFi_AP();
WebServer_Config();
}
void loop(void) {
if (state_internet == 1) {
if (state_mqtt == false) {
if (readMessage == true) {
Serial.println();
Serial.println("State MQTT Subscribe");
while (!client.connected()) {
client.connect("SmartEmo1-Subscribe");
Serial.print(".");
delay(100);
}
Serial.println();
Serial.println("MQTT Connected");
readMessage = false;
}
client.set_callback(callback);
client.subscribe("SmartEmo2/data");
// state_mqtt = true;
}
if (digitalRead(14) == LOW) {
if (state_move == 0) {
// state_move = 1;
emoMove();
// state_mqtt = false;
}
}
}
server.handleClient();
delay(500);
}
void Messagebegin() {
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.write(67);
display.write(111);
display.write(110);
display.write(102);
display.write(105);
display.write(103);
display.write(87);
display.write(105);
display.write(70);
display.write(105);
display.write(13);
display.write(115);
display.write(115);
display.write(105);
display.write(100);
display.write(58);
display.write(101);
display.write(115);
display.write(112);
display.write(49);
display.write(32); //SPACE
display.write(32); //SPACE
display.write(32); //SPACE
display.write(32); //SPACE
display.write(32); //SPACE
display.write(32); //SPACE
display.write(32); //SPACE
display.write(32); //SPACE
display.write(32); //SPACE
display.write(32); //SPACE
display.write(32); //SPACE
display.write(32); //SPACE
display.setTextSize(1);
display.write(49); //1
display.write(57); //9
display.write(50); //2
display.write(46); //.
display.write(49); //1
display.write(54); //6
display.write(56); //8
display.write(46); //.
display.write(49); //1
display.write(48); //0
display.write(46); //.
display.write(49); //1
display.write(48); //0
display.write(48); //0
display.display();
}
void MessageConnect() {
display.clearDisplay();
display.drawBitmap(40, 5, chickenCode, 56, 64, 1);
display.display();
}
void MessageSent() {
display.clearDisplay();
display.write(83);
display.write(101);
display.write(110);
display.write(116);
display.display();
}
void emoMove() {
uint8_t n_move = 40;
while (n_move != 128) {
display.clearDisplay();
display.drawBitmap(n_move, 5, chickenCode, 56, 64, 1);
display.display();
n_move++;
}
display.clearDisplay();
display.display();
write_payload();
// state_move = 0;
}
void write_payload() {
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,0);
// display.write(80); //P
// display.write(108); //l
// display.write(101); //e
// display.write(97); //a
// display.write(115); //s
// display.write(101); //e
// display.write(32); //
display.write(87); //W
display.write(97); //a
display.write(105); //i
display.write(116); //t
display.display();
SizeArrayChicken = sizeof(chickenCode) / sizeof(chickenCode[0]);
Serial.println();
Serial.println("State MQTT Publish");
while (!client.connected()) {
client.connect("SmartEmo1-Publish");
Serial.print(".");
delay(100);
}
Serial.println();
Serial.println("MQTT Connected");
for (int l = 0; l < SizeArrayChicken; l++) {
char buffer[10];
itoa(chickenCode[l], buffer, 10);
while (!client.publish("SmartEmo2/data", buffer)) {
client.connect("SmartEmo1-Publish");
Serial.print(".");
delay(10);
}
Serial.printf("%s", buffer);
Serial.println();
}
MessageConnect();
// state_mqtt = false;
Serial.println();
Serial.println("Published Complete");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment