/* https://github.com/bremme/arduino-tm1637 The circuit: connect TM1637 pin CLK to Arduino pin D4 connect TM1637 pin DIO to Arduino pin D5 connect TM1637 pin Vcc to Arduino pin 5V connect TM1637 pin GND to Arduino pin GND */ #include "SevenSegmentTM1637.h" #include "SevenSegmentExtended.h" #include "SevenSegmentFun.h" #include #include #include #include #include extern "C" { uint16 readvdd33(void); } #define DEBUG 1 #define ONE_WIRE_BUS_IN 5 #define ONE_WIRE_BUS_OUT 4 #define REDLED 15 #define GREENLED 0 #define ORANGELED 2 //#define DOSLEEP #define LEDBLINK #define RELAY 13 #define TYPE "THERMO" ADC_MODE(ADC_VCC); //vcc read const char * essids[10]; const char * passwords[10]; const byte PIN_CLK = 12; // define CLK pin (any digital pin) const byte PIN_DIO = 14; // define DIO pin (any digital pin) SevenSegmentFun display(PIN_CLK, PIN_DIO); OneWire one_wire_in(ONE_WIRE_BUS_IN); OneWire one_wire_out(ONE_WIRE_BUS_OUT); DallasTemperature sensors_in(&one_wire_in); DallasTemperature sensors_out(&one_wire_out); int relay_state = 0; float temp_lo = 8.0; float temp_hi = 9.0; void relay_on() { relay_state = 1; digitalWrite(RELAY, LOW); ledon(REDLED); ledoff(GREENLED); } void relay_off() { relay_state = 0; digitalWrite(RELAY, HIGH); ledoff(REDLED); ledon(GREENLED); } void ledblink(int led) { #ifdef LEDBLINK digitalWrite(led, HIGH); delay(100); digitalWrite(led, LOW); #endif } void ledon(int led) { #ifdef LEDBLINK digitalWrite(led, HIGH); #endif } void relay_led() { #ifdef LEDBLINK if (relay_state) { ledon(REDLED); ledoff(GREENLED); } else { ledon(GREENLED); ledoff(REDLED); } #endif } void ledoff(int led) { #ifdef LEDBLINK digitalWrite(led, LOW); #endif } void semaphore() { int i = 0; for (i; i < 3; i++) { ledblink(GREENLED); ledblink(REDLED); ledblink(ORANGELED); } } void list_networks() { // scan for nearby networks: Serial.println("** Scan Networks **"); int numSsid = WiFi.scanNetworks(); if (numSsid == -1) { Serial.println("Couldn't get a wifi connection"); while (true); } // print the list of networks seen: Serial.print("number of available networks:"); Serial.println(numSsid); // print the network number and name for each network found: for (int thisNet = 0; thisNet < numSsid; thisNet++) { Serial.print(thisNet); Serial.print(") "); Serial.print(WiFi.SSID(thisNet)); Serial.print("\tSignal: "); Serial.print(WiFi.RSSI(thisNet)); Serial.print(" dBm"); Serial.print("\tEncryption: "); printEncryptionType(WiFi.encryptionType(thisNet)); } } void printEncryptionType(int thisType) { // read the encryption type and print out the name: switch (thisType) { case ENC_TYPE_WEP: Serial.println("WEP"); break; case ENC_TYPE_TKIP: Serial.println("WPA"); break; case ENC_TYPE_CCMP: Serial.println("WPA2"); break; case ENC_TYPE_NONE: Serial.println("None"); break; case ENC_TYPE_AUTO: Serial.println("Auto"); break; } } void say(char * str) { if (DEBUG) { Serial.printf("%s\n", str); } display.print(str); delay(700); } void say(float f) { if (DEBUG) { Serial.printf("say debug: %.2f\n", f); Serial.print("say debug:"); Serial.println(f); } display.printf("%.2f", f); delay(700); } char * xstrcpy(char * dest, String src) { int i; for (i = 0; i < src.length(); i++) { dest[i] = src[i]; } dest[i] = '\0'; return dest; } char * get_ip() { char * ip; //[17]; ip = (char*)malloc(17); xstrcpy(ip, WiFi.localIP().toString()); return ip; } void turnOff(int pin) { pinMode(pin, OUTPUT); digitalWrite(pin, 1); } int send_data(char * data) { HTTPClient http; Serial.print("[HTTP] begin\n"); Serial.printf("[HTTP] data: %s\n", data); http.begin("http://grezl.eu/wiot/v1/sensor"); Serial.print("[HTTP] POST\n"); http.addHeader("Content-Type", "application/x-www-form-urlencoded"); int http_code = http.POST(data); Serial.printf("[HTTP] http code: %d\n", http_code); if (http_code > 0) { /* if (http_code == HTTP_CODE_OK) { String payload = http.getString(); Serial.println(payload); } */ } else { Serial.printf("[HTTP] failed, error: %s\n", http.errorToString(http_code).c_str()); } http.end(); return http_code; } char * get_data(char * uri) { HTTPClient http; String payload; Serial.print("get_data(): GET\n"); http.begin(uri); http.addHeader("Content-Type", "text/plain"); int http_code = http.GET(); Serial.printf("[HTTP] http code: %d\n", http_code); if (http_code == HTTP_CODE_OK) { payload = http.getString(); Serial.println("get_data():" + payload); } else { payload = "err:" + http_code; Serial.printf("[HTTP] failed, error: %s\n", http.errorToString(http_code).c_str()); } http.end(); char * ret = (char*) malloc(20); xstrcpy(ret, payload); Serial.print("\nget_data() ret:"); Serial.println(ret); return ret; } void create_line(char * type, char * sn, char * data, float value, float value2) { char buf[12]; char * ip = get_ip(); strcpy(data, "w_sensor="); strcat(data, "type:"); strcat(data, type); strcat(data, ","); strcat(data, "sn:"); strcat(data, sn); strcat(data, ","); strcat(data, "tin:"); dtostrf(value, 5, 2, buf); strcat(data, buf); strcat(data, ","); strcat(data, "tout:"); dtostrf(value2, 5, 2, buf); strcat(data, buf); strcat(data, ",ip:"); strcat(data, ip); strcat(data, ","); strcat(data, "relay:"); itoa(relay_state, buf, 10); strcat(data, buf); strcat(data, ";"); free(ip); } char * get_wifi_status(int m) { switch (m) { case 0: return "WL_IDLE_STATUS"; case 1: return "WL_NO_SSID_AVAIL"; case 2: return "WL_SCAN_COMPLETED"; case 3: return "WL_CONNECTED"; case 4: return "WL_CONNECT_FAILED"; case 5: return "WL_CONNECTION_LOST"; case 6: return "WL_DISCONNECTED"; } } void find_wifi() { int i, ii; for (i = 0; i < 4; i++) { Serial.print("trying "); Serial.print(essids[i]); Serial.print(":"); Serial.println(passwords[i]); WiFi.begin(essids[i], passwords[i]); Serial.println(get_wifi_status(WiFi.status())); delay(20); if (WiFi.waitForConnectResult() == WL_CONNECTED) { Serial.println("FOUND"); say("found"); ledblink(ORANGELED); ledblink(GREENLED); ledblink(GREENLED); return; } else { Serial.println("NOPE"); say("nope"); ledblink(ORANGELED); ledblink(REDLED); ledblink(REDLED); WiFi.disconnect(); } } Serial.println("couldn't find a shit"); say ("no wifi"); ledblink(ORANGELED); ledblink(ORANGELED); ledblink(ORANGELED); ledblink(REDLED); ledblink(REDLED); ledblink(REDLED); } void turn_off_pins() { turnOff(10); turnOff(9); //turnOff(16); //turnOff(5); turnOff(4); turnOff(0); turnOff(2); turnOff(14); turnOff(12); //turnOff(13); //turnOff(15); //turnOff(3); //turnOff(1); } void setup_pins() { pinMode(RELAY, OUTPUT); pinMode(10, INPUT_PULLUP); } void setup_infoled() { pinMode(REDLED, OUTPUT); pinMode(GREENLED, OUTPUT); pinMode(ORANGELED, OUTPUT); digitalWrite(GREENLED, LOW); digitalWrite(REDLED, LOW); digitalWrite(ORANGELED, LOW); semaphore(); } void wifi_essids_setup() { essids[0] = "walley"; essids[1] = "HZSOL-WRK"; essids[2] = "waredmi"; essids[3] = "3GWiFi_55357C"; passwords[0] = "aaaaaaaaa"; passwords[1] = "HZSOL231wpa"; passwords[2] = "aaaaaaaaa"; passwords[3] = "1245678"; } void display_temperature(float t) { int tt; byte rawData; display.setColonOn(true); tt = t * 10; display.printf("%03d", tt); Serial.printf("%03d\n", tt); // display degree symbol on position 3 plus set lower colon rawData = B11100011; display.printRaw(rawData, 3); delay(900); display.setColonOn(false); } void relay_stuff(float t_in, float t_out) { Serial.println("Comparing:"); Serial.println(t_in); Serial.println(t_out); Serial.println(temp_hi); Serial.println(temp_lo); say("----"); say("t_hi"); display_temperature(temp_hi); say("----"); say("t_lo"); display_temperature(temp_lo); if (t_out > 70) { Serial.print("\nerror temp too high\n"); return; } if (t_out < -20) { Serial.print("\nerror temp too low\n"); return; } if (t_out > temp_hi) { relay_off(); Serial.print("\n*relay off*\n"); } if (t_out <= temp_lo) { relay_on(); Serial.println("\n*relay on*\n"); } say("----"); say("relay"); if (relay_state) { say(" on "); } else { say("off "); } } void do_stuff() { char tstr[20]; float t_in; float t_out; char data[50]; char * ip; char * f; float x; Serial.println(digitalRead(10)); say("----"); ip = get_ip(); say("ip"); say(ip); free(ip); // say("----"); // say("volt"); // float vdd = ESP.getVcc() / 1000.0; // dtostrf(vdd, 2, 2, tstr); // say(tstr); sensors_in.requestTemperatures(); sensors_out.requestTemperatures(); t_in = sensors_in.getTempCByIndex(0); t_out = sensors_out.getTempCByIndex(0); say("----"); say("tin"); display_temperature(t_in); Serial.print("\nt_in\n"); Serial.println(t_in); say("----"); say("tout"); display_temperature(t_out); Serial.print("\nt_out\n"); Serial.println(t_out); create_line(TYPE, "thermostat0", data, t_in, t_out); Serial.println(data); if ((WiFi.status() == WL_CONNECTED)) { send_data(data); Serial.print("connected to "); Serial.println(WiFi.SSID()); ledblink(GREENLED); say("sent"); f = get_data("http://grezl.eu/wiot/v1/data/thermostat0/low"); if (!strstr(f, "err")) { temp_lo = atoi(f); } else { Serial.println("err:"); Serial.println(f); } free(f); f = get_data("http://grezl.eu/wiot/v1/data/thermostat0/hi"); if (!strstr(f, "err")) { temp_hi = atoi(f); } else { Serial.println("err:"); Serial.println(f); } free(f); } else { Serial.println("not connected"); ledblink(ORANGELED); say("failed"); } relay_stuff(t_in, t_out); } /********************************************************************************/ void setup() { Serial.begin(9600); wifi_essids_setup(); turn_off_pins(); setup_pins(); setup_infoled(); display.begin(); display.setBacklight(100); sensors_in.begin(); sensors_out.begin(); // WiFi.persistent(false); //disables the storage to flash. WiFi.mode(WIFI_STA); if (WiFi.status() != WL_CONNECTED) { WiFi.begin(); } delay(100); if (WiFi.status() != WL_CONNECTED) { Serial.println("start: not connected"); ledblink(ORANGELED); ledblink(ORANGELED); ledblink(ORANGELED); say("wifi"); find_wifi(); } else { Serial.println("start: connected"); say("connected"); ledblink(ORANGELED); } Serial.println(WiFi.macAddress()); do_stuff(); #ifdef DOSLEEP ESP.deepSleep(59000000); #endif } void loop() { semaphore(); relay_led(); do_stuff(); }