Hieromon Ikasamo
Published © MIT

ESP8266 Connect WiFi Made Easy

WLAN configuration at runtime on the ESP8266 web interface for Arduino.

EasyFull instructions provided1 hour3,238
ESP8266 Connect WiFi Made Easy

Things used in this project

Hardware components

NodeMCU ESP8266 Breakout Board
NodeMCU ESP8266 Breakout Board
×1

Software apps and online services

Arduino IDE
Arduino IDE
Arduino library the AutoConnect and PageBuilder required. https://hieromon.github.io/AutoConnect https://github.com/Hieromon/PageBuilder

Story

Read more

Code

mqttRSSI.ino

Arduino
ESP8266 publish the RSSI as the WiFi signal strength to ThingSpeak channel.
/*
  ESP8266 publish the RSSI as the WiFi signal strength to ThingSpeak channel.
  This example is for explaining how to use the AutoConnect library.

  In order to execute this example, the ThingSpeak account is needed. Sing up
  for New User Account and create a New Channel via My Channels.
  For details, please refer to the project page.
  https://www.hackster.io/hieromon-ikasamo/esp8266-connect-wifi-make-easily-d75f45

  This example is based on the environment as of March 20, 2018.
  Copyright (c) 2018 Hieromon Ikasamo.
  This software is released under the MIT License.
  https://opensource.org/licenses/MIT
*/

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <PubSubClient.h>
#include <AutoConnect.h>

#define MQTT_USER_KEY        "****************"  // Replace to User API Key.
#define CHANNEL_ID           "******"            // Replace to Channel ID.
#define CHANNEL_API_KEY_WR   "****************"  // Replace to the write API Key.

#define MQTT_UPDATE_INTERVAL 15000
#define MQTT_TOPIC           "channels/" CHANNEL_ID "/publish/" CHANNEL_API_KEY_WR
#define MQTT_USER_ID         "anyone" 
#define MQTT_SERVER          "mqtt.thingspeak.com"

AutoConnect  portal;
WiFiClient   wifiClient;
PubSubClient mqttClient(wifiClient);

bool mqttConnect() {
  static const char alphanum[] = "0123456789"
                                 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                                 "abcdefghijklmnopqrstuvwxyz";  // For random generation of client ID.
  char    clientId[9];

  uint8_t retry = 10;
  while (!mqttClient.connected()) {
    Serial.println("Attempting MQTT broker:" MQTT_SERVER);

    for (uint8_t i = 0; i < 8; i++) {
      clientId[i] = alphanum[random(62)];
    }
    clientId[8] = '\0';

    if (mqttClient.connect(clientId, MQTT_USER_ID, MQTT_USER_KEY)) {
      Serial.println("Established:" + String(clientId));
      return true;
    } else {
      Serial.print("Connection failed:" + String(mqttClient.state()));
      if (!--retry)
        return false;
    }
    delay(3000);
  }
}

void mqttPublish(String msg) {
  String path = String(MQTT_TOPIC);
  int    tLen = path.length();
  char   topic[tLen];
  path.toCharArray(topic, tLen + 1);

  int    mLen = msg.length();
  char   payload[mLen];
  msg.toCharArray(payload, mLen + 1);

  mqttClient.publish(topic, payload);
}

unsigned long   lastPub = 0;

void setup() {
  delay(1000);
  Serial.begin(115200);
  Serial.println();

  Serial.print("WiFi ");
  if (portal.begin()) {
    Serial.println("connected:" + WiFi.SSID());
    Serial.println("IP:" + WiFi.localIP().toString());
  } else {
    Serial.println("connection failed:" + String(WiFi.status()));
    while (1) {
      delay(100);
      yield();
    }
  }
  mqttClient.setServer(MQTT_SERVER, 1883);
}

void loop() {
  if (millis() - lastPub > MQTT_UPDATE_INTERVAL) {
    if (!mqttClient.connected()) {
      mqttConnect();
    }
    String item = String("field1=") + String(WiFi.RSSI());
    mqttPublish(item);
    mqttClient.loop();
    lastPub = millis();
  }
  portal.handleClient();
}

simple.ino

Arduino
"Hello, world" web server on ESP8266. It's for the AutoConenct experience.
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <AutoConnect.h>

ESP8266WebServer Server;
AutoConnect      Portal(Server);

void rootPage() {
  char content[] = "Hello, world";
  Server.send(200, "text/plain", content);
}

void setup() {
  delay(1000);
  Serial.begin(115200);
  Serial.println();

  Server.on("/", rootPage);
  if (Portal.begin()) {
    Serial.println("HTTP server:" + WiFi.localIP().toString());
  }
}

void loop() {
    Portal.handleClient();
}

Credits

Hieromon Ikasamo

Hieromon Ikasamo

4 projects • 4 followers
Contact

Comments