Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define mS_TO_S_FACTOR 1000 /* Conversion factor for milliseconds to seconds */
- int TIME_TO_SLEEP = 3000;
- unsigned int bridgeMillis = 0;
- #include <WiFi.h>
- const char* ssid = "<SSID>";
- const char* password = "<PW>";
- #include <WiFiClientSecure.h>
- IPAddress local_IP(<local_ip>);
- IPAddress gateway(<gateway>);
- IPAddress subnet(<subnet>);
- IPAddress primaryDNS(<dns1>);
- IPAddress secondaryDNS(<dns2>);
- WiFiClientSecure client;
- const char* host = "<ip>";
- const int httpPort = 443;
- const char* hueBridgeHost = "<ip>";
- const char* hueBridgeUser = "<api_key>";
- unsigned long scanStart;
- unsigned long scanDuration;
- unsigned int clientStart;
- unsigned int clientDuration;
- int counter = 0;
- unsigned const int wifiTimeoutSeconds = 5;
- RTC_DATA_ATTR int timeoutCount = 0;
- const bool debug = true;
- void deepSleep() {
- esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * mS_TO_S_FACTOR);
- if (debug) {
- Serial.println("Going to sleep now for " + String(TIME_TO_SLEEP) + " milliseconds");
- Serial.println("");
- }
- if (debug) Serial.flush();
- esp_deep_sleep_start();
- }
- bool wifiConnect() {
- scanStart = millis();
- if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
- if (debug) Serial.println("STA Failed to configure");
- return false;
- }
- if (debug) {
- Serial.println("Connecting to " + String(ssid));
- }
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- if (debug) Serial.print(".");
- delay(500);
- counter++;
- if (counter >= wifiTimeoutSeconds) { //after x seconds timeout - reset board
- if (debug) Serial.println("Wifi Connection Timeout..");
- timeoutCount++;
- return false;
- }
- }
- if (debug) {
- Serial.println("");
- Serial.println("WiFi connected!");
- }
- scanDuration = millis() - scanStart;
- if (debug) {
- Serial.print("Duration wifiConnect: ");
- Serial.println(String(scanDuration));
- }
- return true;
- }
- bool clientConnect() {
- clientStart = millis();
- if (!client.connected()) {
- if (debug) Serial.println("Starting connection to host " + String(host) + " port " + String(httpPort));
- if (!client.connect(host, httpPort)) {
- Serial.println("clientConnect connection failed");
- delay(500); //new, for led visibility..
- return false;
- } else {
- clientDuration = millis() - clientStart;
- if (debug) Serial.println("Duration clientConnect " + String(clientDuration));
- return true;
- }
- } else {
- return true;
- }
- }
- String getRequest(String url2) {
- unsigned int wait = 1000;
- if (url2.substring(1, 7).equals("lights")) {
- wait = 100;
- } else if (url2.substring(1, 7).equals("groups")) {
- wait = 500;
- } else if (url2.substring(1, 7).equals("scenes")) {
- wait = 500;
- } else if (url2.substring(1, 8).equals("sensors")) {
- wait = 500;
- }
- while (millis() < bridgeMillis + wait) {
- }
- if (!clientConnect()) {
- return "error"; //TODO!
- }
- bridgeMillis = millis();
- client.print(String("GET ") + "/api/" + hueBridgeUser + url2 + " HTTP/1.1\r\n" +
- "Host: " + hueBridgeHost + "\r\n" +
- "\r\n");
- String json;
- String line;
- int retCode = 0;
- while (client.connected()) {
- line = client.readStringUntil('\n');
- if (line.startsWith("HTTP/1.1")) {
- retCode = line.substring(9, 12).toInt(); // Get HTTP return code
- if (retCode != 200) {
- if (debug) Serial.println("http return code: " + String(retCode));
- return "error";
- }
- }
- if (line == "\r")break;
- }
- while (client.available()) {
- line = client.readStringUntil('\n');
- line = client.readStringUntil('\n');
- json = line;
- line = client.readStringUntil('\n');
- line = client.readStringUntil('\n');
- if (line == "\r")break;
- }
- return json;
- }
- void setSerial() {
- Serial.begin(115200);
- while (!Serial) continue;
- }
- void runApplication() {
- if (wifiConnect()) {
- String api = "/lights";
- String request_url = api + "/1";
- String myJSON = getRequest(request_url);
- if (debug) Serial.println("myJSON: " + myJSON);
- request_url = api + "/2";
- myJSON = getRequest(request_url);
- if (debug) Serial.println("myJSON: " + myJSON);
- request_url = api + "/3";
- myJSON = getRequest(request_url);
- if (debug) Serial.println("myJSON: " + myJSON);
- request_url = api + "/4";
- myJSON = getRequest(request_url);
- if (debug) Serial.println("myJSON: " + myJSON);
- request_url = api + "/5";
- myJSON = getRequest(request_url);
- if (debug) Serial.println("myJSON: " + myJSON);
- request_url = api + "/6";
- myJSON = getRequest(request_url);
- if (debug) Serial.println("myJSON: " + myJSON);
- request_url = api + "/8";
- myJSON = getRequest(request_url);
- if (debug) Serial.println("myJSON: " + myJSON);
- request_url = api + "/9";
- myJSON = getRequest(request_url);
- if (debug) Serial.println("myJSON: " + myJSON);
- request_url = api + "/11";
- myJSON = getRequest(request_url);
- if (debug) Serial.println("myJSON: " + myJSON);
- } else {
- if (debug) Serial.println("NOT WifiConnect");
- }
- }
- void setup() {
- if (debug) setSerial();
- runApplication();
- if (debug) Serial.println("Cyletime: " + String(millis()));
- deepSleep();
- }
- void loop() {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement