/* * 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 #include #include #include #include #include #include int flash_addr = 0; //sleep needs wire from rst to d0 #define DOSLEEP #define SLEEP_DELAY 600000000 //#define SLEEP_DELAY 6000 //#define LEDBLINK #define METEOSN "meteo2" #define ONE_WIRE_BUS 5 #define REDLED 0 #define GREENLED 2 #define FLASH_ENTRIES 10 #define FLASH_SIZE 1 + 80 * FLASH_ENTRIES 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) OneWire oneWire(ONE_WIRE_BUS); DallasTemperature sensors(&oneWire); void ledblink(int led) { #ifdef LEDBLINK digitalWrite(led, HIGH); delay(100); digitalWrite(led, LOW); #endif } 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; } } 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[16]; xstrcpy(ip, WiFi.localIP().toString()); return ip; } char * get_ssid() { char ssid[30]; xstrcpy(ssid, WiFi.SSID().c_str()); return ssid; } void turnOff(int pin) { pinMode(pin, OUTPUT); digitalWrite(pin, 1); } int send_data(char * data) { HTTPClient http; Serial.println("HTTP"); Serial.println("HTTP: sending"); http.begin("http://grezl.eu/wiot/v1/sensor"); http.addHeader("Content-Type", "application/x-www-form-urlencoded"); int http_code = http.POST(data); if (http_code > 0) { if (http_code == HTTP_CODE_OK) { String payload = http.getString(); if (!strcmp(payload.c_str(), "OK")) { ledblink(GREENLED); Serial.println("*GREEN*"); } else { ledblink(REDLED); Serial.printf("[HTTP] bad response: %s\n",payload.c_str()); Serial.println("*RED*"); } } else { Serial.printf("[HTTP] http code: %d\n", http_code); Serial.println("*RED*"); ledblink(REDLED); } } else { Serial.printf("[HTTP] failed, error: %s\n", http.errorToString(http_code).c_str()); ledblink(REDLED); Serial.println("*RED*"); } http.end(); return http_code; } void create_line(char * type, char * sn, char * data, float value, int count) { char buf[12]; strcpy(data, "w_sensor=type:METEO,"); strcat(data, "sn:"); strcat(data, sn); strcat(data, ",temp:"); dtostrf(value, 5, 2, buf); strcat(data, buf); strcat(data, ",count:"); itoa(count, buf, 10); strcat(data, buf); strcat(data, ";"); } void create_net_line(char * type, char * sn, char * data) { char buf[12]; strcpy(data, "w_sensor=type:METEO,"); strcat(data, "sn:"); strcat(data, sn); strcat(data, ",ip:"); strcat(data, get_ip()); strcat(data, ",ssid:"); strcat(data, get_ssid()); strcat(data, ";"); } 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"; } } int find_wifi() { int i,ii; Serial.println("* WIFI *"); 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]); if((WiFi.waitForConnectResult() == WL_CONNECTED)) { Serial.print("FOUND, ip "); Serial.println(get_ip()); return 1; } else { Serial.println("NOPE"); WiFi.disconnect(); } } Serial.println("couldn't find a shit"); return 0; } 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 infoled_setup() { #ifdef LEDBLINK digitalWrite(GREENLED, LOW); digitalWrite(REDLED, LOW); #endif } void wifi_essids_setup() { essids[0] = "walley"; essids[1] = "HZSOL-WRK"; essids[2] = "waredmi"; essids[3] = "MIFI_WR706_507"; passwords[0] = "aaaaaaaaa"; passwords[1] = "HZSOL231wpa"; passwords[2] = "aaaaaaaaa"; passwords[3] = "1234567890"; } void do_stuff(int count) { char tstr[20]; float t; char data[100]; sensors.requestTemperatures(); t = sensors.getTempCByIndex(0); create_line("x", METEOSN, data, t, count); Serial.println(data); flash_write(data); } void flash_clear() { for (int i = 0; i < FLASH_SIZE; i++) { EEPROM.write(i, 0); } EEPROM.commit(); } void flash_show() { char count = EEPROM.read(0); char c; Serial.printf("count: %i\n",count); for (int i = 0; i < count; i++) { Serial.print(i); Serial.print(":"); for (int j = 0; j < 80; j++) { c = EEPROM.read(1 + i * 80 + j); if (!c) { Serial.print("*"); } else if (c < 32 || c > 126) { Serial.print("!"); } else { Serial.print(c); } } Serial.println(); } } void flash_write_entry(int address, char * data) { for (int i = 0; i < strlen(data); i++) { EEPROM.write(address + i, data[i]); } EEPROM.commit(); } void flash_write(char * data) { char count = EEPROM.read(0); if (count >= FLASH_ENTRIES) { Serial.println("Flash is full."); return; } Serial.printf("count: %i\n",count); count++; flash_write_entry((count -1) * 80 + 1, data); EEPROM.write(0, count); EEPROM.commit(); } char * flash_read_send() { char data[100]; char count = EEPROM.read(0); char c; Serial.printf("count: %i\n",count); for (int i = 0; i < count; i++) { Serial.print("entry:"); Serial.println(i); for (int j = 0; j < 80; j++) { c = EEPROM.read(1 + i * 80 + j); data[j] = c; } send_data(data); } create_net_line("x", METEOSN, data); send_data(data); } void sleep_now(int rf) { #ifdef DOSLEEP if (rf) { ESP.deepSleep(SLEEP_DELAY, WAKE_RF_DEFAULT); } else { ESP.deepSleep(SLEEP_DELAY, WAKE_RF_DISABLED); } #endif } void do_last_step() { //send data, sleep wifi disabled Serial.println("send data, disable wifi and sleep"); flash_show(); wifi_essids_setup(); WiFi.persistent(false); //disables the storage to flash. WiFi.mode(WIFI_STA); int retries = 0; if(WiFi.status() != WL_CONNECTED) { list_networks(); while(!find_wifi() && retries < 2){ retries++; }; } Serial.println(WiFi.macAddress()); if((WiFi.status() == WL_CONNECTED)) { ledblink(GREENLED); flash_read_send(); Serial.print("connected to "); Serial.println(get_ip()); Serial.println(WiFi.SSID()); } else { Serial.println("not connected"); ledblink(REDLED); } flash_clear(); sleep_now(0); } void do_step(int count) { if (count < 9) { //sleep, wake wifi disabled do_stuff(count); sleep_now(0); } else if (count == 9) { //sleep, wake wifi enabled do_stuff(count); sleep_now(1); } else if (count == 10) { do_last_step(); } else { Serial.println("count is wrong"); flash_clear(); sleep_now(0); } } void setup() { EEPROM.begin(FLASH_SIZE); Serial.begin(9600); turn_off_pins(); infoled_setup(); sensors.begin(); char count = EEPROM.read(0); Serial.printf("\n *** Start, count: %i ***\n", count); do_step(count); } void loop() { // -- debug stuff //flash_clear(); //char count = EEPROM.read(0); //do_step(count); //flash_show(); //delay(SLEEP_DELAY); }