#include "Domoticz.h" Domoticz::Domoticz(String domoticz, String port, const char* ssid, const char* pass) { _domoticz = domoticz; _port = port; _ssid = ssid; _pass = pass; // // Domo // _domoc[_domoticz.length() + 1]; // _domoticz.toCharArray(_domoc, _domoticz.length() + 1); // // // Port // char portc[_port.length() + 1]; // _port.toCharArray(portc, _port.length() + 1); // _iport = atoi(portc); } boolean Domoticz::connect() { // Domo char _domoc[_domoticz.length() + 1]; _domoticz.toCharArray(_domoc, _domoticz.length() + 1); // Port char portc[_port.length() + 1]; _port.toCharArray(portc, _port.length() + 1); int _iport = atoi(portc); boolean connected = _client.connect(_domoc, _iport); Serial.print(_domoc); Serial.print(" "); Serial.print(_iport); Serial.print(" "); Serial.print(" connected "); Serial.print(connected); Serial.print(" "); Serial.println(WiFi.localIP()); return connected; } void Domoticz::close() { } void Domoticz::initWifi() { WiFi.mode(WIFI_AP); WiFi.begin(_ssid, _pass); //Connect to local Wifi Serial.println(); Serial.print("Connecting to WiFi"); while (WiFi.status() != WL_CONNECTED) { Serial.print("."); delay(500); } Serial.println("WiFi Connected!"); Serial.print("Connexion au reseau "); Serial.println(WiFi.localIP()); } String Domoticz::generateKey() { // WiFi.mode(WIFI_AP); // Do a little work to get a unique-ish name. Append the // last two bytes of the MAC (HEX'd) to "Thing-": uint8_t mac[WL_MAC_ADDR_LENGTH]; WiFi.softAPmacAddress(mac); String macID = String(mac[WL_MAC_ADDR_LENGTH - 2], HEX) + String(mac[WL_MAC_ADDR_LENGTH - 1], HEX); macID.toUpperCase(); String AP_NameString = "ESP8266 Thing " + macID; char AP_NameChar[AP_NameString.length() + 1]; memset(AP_NameChar, 0, AP_NameString.length() + 1); for (int i = 0; i < AP_NameString.length(); i++) { AP_NameChar[i] = AP_NameString.charAt(i); } // WiFi.softAP(AP_NameChar, WiFiAPPSK); Serial.println("macID=" + macID); _macID = macID; return macID; } void Domoticz::executeJson(String json, String svalue, String nvalue) { // Domo char _domoc[_domoticz.length() + 1]; _domoticz.toCharArray(_domoc, _domoticz.length() + 1); // Port char portc[_port.length() + 1]; _port.toCharArray(portc, _port.length() + 1); int _iport = atoi(portc); _client.print("GET " + json); //"GET /json.htm?type=command¶m=getuservariables"); if (svalue != "") { _client.print("&svalue=" + svalue); } if (nvalue != "") { _client.print("&nvalue=" + nvalue); } _client.println(" HTTP/1.1"); _client.print("Host: "); _client.print(_domoc); _client.print(":"); _client.println(_iport); _client.println("User-Agent: Arduino-ethernet"); _client.println("Connection: close"); _client.println(); Serial.println("json=" + json); } //-------------------------------------------------------------------------------------------------- // Read current supply voltage //-------------------------------------------------------------------------------------------------- String Domoticz::readVcc() { // most exact output uint16_t v = ESP.getVcc(); float_t v_cal = ((float) v / 1024.0f); char v_str[10]; dtostrf(v_cal, 5, 3, v_str); sprintf(v_str, "%s", v_str); Serial.print("Tension lue "); Serial.println(String(v_str)); return String(v_str); //ESP.getVcc() / 1024.0f; // Vcc in millivolts } String Domoticz::getIndexOfString(String data, String separator, int index) { int found = 0; int strIndex[] = { 0, -1 }; int maxIndex = data.length() - 1; for (int i = 0; i <= maxIndex && found <= index; i++) { if (data.charAt(i) == separator.charAt(0) || i == maxIndex) { found++; strIndex[0] = strIndex[1] + 1; strIndex[1] = (i == maxIndex) ? i + 1 : i; } } return found > index ? data.substring(strIndex[0], strIndex[1]) : ""; } String Domoticz::readLine() { return _client.readStringUntil('\n'); } void Domoticz::readTempDayValues(String idx) { String json = "/json.htm?type=graph&sensor=temp&idx=" + idx + "&range=day"; int nb = 0; int nbVal = 0; data = ""; labels = ""; String label = ""; Serial.println("readTempDayValues " + idx); connect(); executeJson(json, "", ""); while (1) { // Avoid WDT reset during long process yield(); String line = readLine(); if (line == "") { break; } if (line.indexOf("\"d\"") != -1) { //Serial.println(line + " "); label = line.substring(line.indexOf(":") + 2, line.length()); } if (line.indexOf("\"te\"") != -1) { // Serial.println(line + " "); nbVal++; if (nbVal % 10) { String val = line.substring(line.indexOf(":") + 2, line.indexOf(".") + 2); if (val != NULL) { //Serial.println(val); if (data == "") { data += val; labels = "\"0\""; } else { char charBuf[50]; // val.toCharArray(charBuf, 50); data += "," + val; //printf("%2.1f", atof(charBuf)); // data += "," + val; // Serial.println(printf("%2.1f", atof(charBuf))); labels = labels + label; } if (data.length() > 500) { data = data.substring(data.indexOf(",") + 1); labels = labels.substring(labels.indexOf(",") + 1); } } } } nb++; } // Avoid WDT reset during long process yield(); // Serial.println(data); // Serial.println(labels); yield(); Serial.print("Nombre de lignes "); Serial.println(nb); close(); } void Domoticz::getIdFromDomoticz(String macID, Params * params) { String separator = ","; int nb = 0; Serial.println("Dans getIdFromDomoticz " + macID); // ------------------- // LECTURE PARAM // ------------------- if (params->esp8266_id == "") { boolean connected = connect(); if (connected) { Serial.println("Connected to domoticz"); executeJson("/json.htm?type=command¶m=getuservariables", "", ""); // Read the first line of the request while (1) { String req = readLine(); if (req.indexOf(macID) != -1) { Serial.println(req); req = readLine(); req = readLine(); String val = req.substring(req.indexOf(":") + 2, req.length() - 1); Serial.println(val); val.replace("\"", ""); params->esp8266_id = getIndexOfString(val, separator, 0); String tmp = getIndexOfString(val, separator, 1); char tmpc[tmp.length() + 1]; tmp.toCharArray(tmpc, tmp.length() + 1); params->sleepTime = atoi(tmpc); if (params->sleepTime <= 0) { params->sleepTime = 60; } params->esp8266_id_Vcc = getIndexOfString(val, separator, 2); Serial.println("esp8266_id=" + params->esp8266_id); Serial.println("sleepTime=" + tmp); Serial.println("esp8266_id_Vcc=" + params->esp8266_id_Vcc); break; } if (req == "" || nb > 500) { break; } nb++; } _client.stop(); delay(100); } } }