Use this forum to chat about hardware specific topics for the ESP8266 (peripherals, memory, clocks, JTAG, programming)

User avatar
By CBRAlex
#82417 Hi.
I want to put a Sonoff Th16 with a external button in a box. When i press the button, i want to have access to the portal of Wifimanager to put a new password. Is this possible? This is the code that i want to use. Thanks for your help!
Code: Select all#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <WiFiClientSecure.h>
#include <ArduinoJson.h> // https://github.com/bblanchon/ArduinoJson
#include <NTPClient.h> // https://github.com/arduino-libraries/NTPClient

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", timezonemins*60, 60000);
DynamicJsonBuffer jsonBuffer;
WiFiClientSecure client;

static char respBuffer[4096];
uint8_t relayPin = 12; // this is the relay Pin
uint8_t ledPin = 13; // this is the green led Pin
uint16_t minutesNow;
uint16_t startMins;
uint16_t switchOffMin;
uint16_t switchOnMin;
boolean relay = HIGH;

void setup() {
  pinMode(relayPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  digitalWrite(relayPin, HIGH); // switch on the light by default
 
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    // Waiting to connect to wifi network
    delay (500);
    digitalWrite(ledPin, !digitalRead(ledPin)); // this blinks the LED
  }
  digitalWrite(ledPin, HIGH); // switches off the LED

  const char request[] =
      "GET /json?lat=" LATTITUDE "&lng=" LONGITUDE "&formatted=0 HTTP/1.1\r\n"
      "User-Agent: ESP8266/1.0\r\n"
      "Accept: */*\r\n"
      "Host: api.sunrise-sunset.org \r\n"
      "Connection: close \r\n"
      "\r\n";

  while (!client.connect("api.sunrise-sunset.org", 443)) {
    // Waiting to get the sunrise and sunset time
    delay(3000);
  }
  client.print(request);
  client.flush();

  uint16_t index = 0;
  while(client.connected()) {
    if (client.available()) {
      respBuffer[index++] = client.read();
      delay(1);
    }
  }
  client.stop();

  char* json = strchr(respBuffer, '{');
  JsonObject& req = jsonBuffer.parseObject(json);
  JsonObject& results = req["results"];
  String sr = results["sunrise"];
  String ss = results["sunset"];
  switchOffMin = sr.substring(11,13).toInt()*60 + sr.substring(14,16).toInt();
  switchOffMin = switchOffMin + timezonemins + sunriseDelay;
  if (switchOffMin>1440) {switchOffMin = switchOffMin -1440;} // day correction

  switchOnMin = ss.substring(11,13).toInt()*60 + ss.substring(14,16).toInt();
  switchOnMin = (switchOnMin + timezonemins + sunsetDelay) % 1440;

  timeClient.begin();
  while (!timeClient.update()) {
    // Waiting to find the current time
    delay(3000);
  }
 
  startMins = timeClient.getHours()*60 + timeClient.getMinutes();
  startMins = startMins - (millis()/60000); // compensate for the current elapsed time
 
}

void loop() {
  if (timeClient.update()) {
    minutesNow = timeClient.getHours()*60 + timeClient.getMinutes();
  } else {
    minutesNow = (startMins + (millis()/60000)) % 1440;
  }
 
  //timeClient.update();
  minutesNow = timeClient.getHours()*60 + timeClient.getMinutes();
 
  if ((minutesNow > switchOffMin) && (minutesNow < switchOnMin) && (relay == HIGH)) {
    digitalWrite(relayPin, LOW); // switch off the light
    digitalWrite(ledPin, LOW); // switches on the LED
    relay = LOW;
  } else if (((minutesNow < switchOffMin) || (minutesNow > switchOnMin)) && (relay == LOW)) {
    digitalWrite(relayPin, HIGH); // switch on the light
    digitalWrite(ledPin, HIGH); // switches off the LED
    relay = HIGH;
  }
 
  delay(3000);
}