Skip to content

Instantly share code, notes, and snippets.

@compilerexe
Created March 15, 2016 00:20
Show Gist options
  • Save compilerexe/8c70f0744ce3d3bce43a to your computer and use it in GitHub Desktop.
Save compilerexe/8c70f0744ce3d3bce43a to your computer and use it in GitHub Desktop.
#include <Arduino.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <RTClib.h>
#include <DHT.h>
#include <ESP8266WiFi.h>
#include <Esp.h>
#include <EEPROM.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <string.h>
#define DEBUG_CODE
#define DEBUG_PRINTER Serial
#ifdef DEBUG_CODE
#define DEBUG_PRINT(...) { DEBUG_PRINTER.print(__VA_ARGS__); }
#define DEBUG_PRINTLN(...) { DEBUG_PRINTER.println(__VA_ARGS__); }
#else
#define DEBUG_PRINT(...) {}
#define DEBUG_PRINTLN(...) {}
#endif
#define DHTPIN D4
#define DHTTYPE DHT22
#define ACTIVE_RELEY D0
#define PIN_SOIL A0
#define BTN_LEFT D5
#define BTN_RIGHT D6
#define BTN_CENTER D7
#define BTN_BACK D8
#define I2C_SCL D1
#define I2C_SDA D2
char boot_mode[20] = "";// if setting don't forgot remove.
char directory[20]; // directory in current
char select_current[30] = "Select timer"; // first select
struct Setting_timer
{
int eeprom_addr_h = 100;
int eeprom_addr_m = 101;
int eeprom_addr_end_h = 102;
int eeprom_addr_end_m = 103;
int reboot_time = 6; // second
int move_right = 0;
int hour = 0;
int minute = 0;
int after_hour = 0;
int after_minute = 0;
char display_h[2];
char display_after_h[2];
char display_m[2];
char display_after_m[2];
char display_reboot[2];
}setting_t;
int addr_eeprom_wifi = 0;
int addr_length_ssid = 256;
int addr_length_pass = 257;
int addr_mode_auto = 300;
int check_mode_auto = 0;
/* Dht22 */
int dht_counting_fail = 0;
/* detect button */
char run_left[20];
char run_right[20];
char run_center[20];
char run_back[20];
/*=== ThingSpeak Detail ===*/
const char* thingspeak_api = "2DTO3Y4QC93L2ZR9";
/*=== WiFiAccessPoint ===*/
const char* ssidAP = "NSC18-Primary";
const char* ssidPass = "";
const char* ap_ip[4] = {"192", "168", "0", "100"};
const char* ap_subnet[4] = {"255", "255", "255", "0"};
const char* ap_gateway[4] = {"192", "168", "0", "1"};
int counting_connect = 0;
/*=======================*/
char memory_rx[10];
int read_rx = 0;
int addr_rx = 0, buffer_rx;
/*------ Fast rx ------*/
int buffer_check = 0;
/*---------------------*/
int limit_sms_co = 0, limit_sms_r = 0;
EspClass Esp;
WiFiClient client;
ESP8266WebServer server(80);
DHT dht(DHTPIN, DHTTYPE);
RTC_DS3231 RTC;
LiquidCrystal_I2C lcd(0x27, 20, 4);
unsigned long previousMillis = 0;
unsigned long currentMillis;
const long interval = 2000;
/* ICON */
byte ICON_TIME[8] {B11111,B11001,B11001,B01110,B01110,B10011,B10011,B11111};
byte ICON_TEMP[8] {B00100,B01010,B01010,B01110,B01110,B11111,B11111,B01110};
byte ICON_HUMID[8] {B00100,B00100,B01010,B01010,B10001,B10001,B10001,B01110};
byte ICON_SELECT[8] {B00100,B00110,B00111,B11111,B11111,B00111,B00110,B00100};
byte ICON_SOIL[8] {B00000,B00000,B00000,B11111,B10001,B11111,B11111,B11111};
byte ICON_CONNECT[8] {B11111,B00000,B01110,B00000,B01110,B00100,B00100,B00100};
void wifi_ap() {
WiFi.softAP(ssidAP);
WiFi.softAPConfig(
IPAddress(atoi(ap_ip[0]), atoi(ap_ip[1]), atoi(ap_ip[2]), atoi(ap_ip[3])),
IPAddress(atoi(ap_gateway[0]), atoi(ap_gateway[1]), atoi(ap_gateway[2]), atoi(ap_gateway[3])),
IPAddress(atoi(ap_subnet[0]), atoi(ap_subnet[1]), atoi(ap_subnet[2]), atoi(ap_subnet[3]))
);
DEBUG_PRINTLN();
DEBUG_PRINTLN();
DEBUG_PRINT("WiFi AP : ");
DEBUG_PRINT(ssidAP);
DEBUG_PRINTLN();
IPAddress myIP = WiFi.softAPIP();
DEBUG_PRINT("IP Address : ");
DEBUG_PRINTLN(myIP);
}
void saveWiFi(const char *ssid, const char *pass) {
int counting_connect = 30;
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
if (counting_connect != 0) {
delay(500);
DEBUG_PRINT(".");
counting_connect--;
} else {
delay(500);
lcd.home();
lcd.setCursor(0, 0);
lcd.print("WiFi Status");
lcd.setCursor(0, 2);
lcd.print("Connecting fail.");
}
}
DEBUG_PRINTLN();
DEBUG_PRINT("IP address : ");
DEBUG_PRINTLN(WiFi.localIP());
DEBUG_PRINT("eeprom ssid : ");
char decryp_ssid[strlen(ssid) + 1];
char decryp_pass[strlen(pass) + 1];
for (int i = 0; i < strlen(ssid); i++) {
DEBUG_PRINT(i);
EEPROM.write(addr_eeprom_wifi, ssid[i]);
decryp_ssid[i] = ssid[i];
addr_eeprom_wifi++;
}
addr_eeprom_wifi = strlen(ssid);
DEBUG_PRINTLN();
DEBUG_PRINT("eeprom pass : ");
for (int j = 0; j < strlen(pass); j++) {
DEBUG_PRINT(j);
EEPROM.write(addr_eeprom_wifi, pass[j]);
decryp_pass[j] = pass[j];
addr_eeprom_wifi++;
}
DEBUG_PRINTLN();
DEBUG_PRINT("decryp ssid : ");
DEBUG_PRINT(decryp_ssid);
DEBUG_PRINT(" / len : ");
DEBUG_PRINT(strlen(ssid));
DEBUG_PRINTLN();
DEBUG_PRINT("decryp pass : ");
DEBUG_PRINT(decryp_pass);
DEBUG_PRINT(" / len : ");
DEBUG_PRINT(strlen(pass));
DEBUG_PRINTLN();
EEPROM.write(addr_length_ssid, strlen(ssid));
EEPROM.write(addr_length_pass, strlen(pass));
EEPROM.commit();
Esp.restart();
}
void webserver_display() {
String codeHtml = "\
<html>\
<head>\
<meta name='viewport' content='initial-scale=1.0, user-scalable=no'>\
<title>WiFi Config</title>\
</head>\
<body>\
<form method='get'>\
SSID&ensp;\
<input type='text' name='SSID'>\
</select><br><br>\
PASS&ensp;<input type='text' name='password'><br><br>\
&ensp;&ensp;&ensp;&ensp;<input type='submit' value='Save'>\
</form>\
</body>\
</html>";
server.send(200, "text/html", codeHtml);
if (server.arg(0) != "" && server.arg(1) != "") {
char http_ssid[20];
char http_pass[20];
strcpy(http_ssid, server.arg(0).c_str());
strcpy(http_pass, server.arg(1).c_str());
saveWiFi(http_ssid, http_pass);
}
}
void webserver_config() {
server.on("/", webserver_display);
server.begin();
DEBUG_PRINTLN("HTTP server started");
}
void autoConnect() {
int len_ssid = EEPROM.read(addr_length_ssid);
int len_pass = EEPROM.read(addr_length_pass);
DEBUG_PRINTLN();
char decryp_ssid[len_ssid + 1];
char decryp_pass[len_pass + 1];
String buff_s = "";
String buff_p = "";
for (int i = 0; i <= len_ssid - 1; i++) {
decryp_ssid[i] = (char)EEPROM.read(addr_eeprom_wifi);
buff_s += decryp_ssid[i];
addr_eeprom_wifi++;
}
for (int j = 0; j <= len_pass - 1; j++) {
decryp_pass[j] = (char)EEPROM.read(addr_eeprom_wifi);
buff_p += decryp_pass[j];
addr_eeprom_wifi++;
}
DEBUG_PRINTLN();
DEBUG_PRINT("decryp ssid : ");
DEBUG_PRINT(buff_s);
DEBUG_PRINT(" / len : ");
DEBUG_PRINT(buff_s.length());
DEBUG_PRINTLN();
DEBUG_PRINT("decryp pass : ");
DEBUG_PRINT(buff_p);
DEBUG_PRINT(" / len : ");
DEBUG_PRINT(buff_p.length());
DEBUG_PRINTLN();
DEBUG_PRINT("Connecting to ");
DEBUG_PRINTLN(buff_s);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Status");
lcd.setCursor(0, 2);
lcd.print(" Connecting.");
// lcd.setCursor(0, 2);
// lcd.print("SSID : ");
// lcd.print();
WiFi.begin(buff_s.c_str(), buff_p.c_str());
while (counting_connect <= 30) {
if (WiFi.status() != WL_CONNECTED) {
delay(500);
DEBUG_PRINT(".");
} else {
DEBUG_PRINTLN();
DEBUG_PRINTLN("WiFi connected");
DEBUG_PRINTLN("IP address: ");
DEBUG_PRINTLN(WiFi.localIP());
counting_connect = 31; // exit if connected
}
counting_connect++;
}
}
void SENT_THINGSPEAK(int temp, int humid, int soil) {
char buffer_t[10];
char buffer_h[10];
char buffer_s[10];
String data = "/update?api_key=";
data += thingspeak_api;
data += "&field1=";
sprintf(buffer_t, "%d", temp);
data += buffer_t;
data += "&field2=";
sprintf(buffer_h, "%d", humid);
data += buffer_h;
data += "&field3=";
sprintf(buffer_s, "%d", soil);
data += buffer_s;
if (!client.connect("api.thingspeak.com", 80)) {
// DEBUG_PRINTLN("retry connection");
return;
} else {
// DEBUG_PRINTLN("connection success");
// DEBUG_PRINTLN(data);
}
// client.println(String("POST ") + thingspeak_h + String(data) + " HTTP/1.1");
// client.println(String("Host: ") + thingspeak_h);
// client.println("Connection: close");
// client.println();
// client.println();
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: ");
client.print(thingspeak_api);
client.print("\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(data.length());
client.print("\n\n");
client.print(data);
}
void FILTER_DATE(int n) {
DateTime now = RTC.now();
int buff_n;
if (n == 1) {
buff_n = now.day();
} else if (n == 2) {
buff_n = now.month();
} else if (n == 4) {
buff_n = now.hour();
} else if (n == 5) {
buff_n = now.minute();
}
switch (buff_n) {
case 1 : lcd.print("01"); break;
case 2 : lcd.print("02"); break;
case 3 : lcd.print("03"); break;
case 4 : lcd.print("04"); break;
case 5 : lcd.print("05"); break;
case 6 : lcd.print("06"); break;
case 7 : lcd.print("07"); break;
case 8 : lcd.print("08"); break;
case 9 : lcd.print("09"); break;
case 0 : lcd.print("00"); break;
default : lcd.print(buff_n);
}
}
void LCD_DISPLAY(float temp, float humid) {
DateTime now = RTC.now();
int soil_sensor = analogRead(PIN_SOIL);
FILTER_DATE(1);
lcd.print("/");
FILTER_DATE(2);
lcd.print("/");
lcd.print(now.year(), DEC);
lcd.print(" ");
FILTER_DATE(4);
lcd.print(":");
FILTER_DATE(5);
lcd.print(" ");
if (WiFi.status() == WL_CONNECTED) {
lcd.write(6);
} else {
lcd.print("N");
}
// lcd.write(1);
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.write(2);
lcd.print(" Temp ");
lcd.print(temp);
lcd.print(" C");
lcd.setCursor(0, 2);
lcd.print(" ");
lcd.write(3);
lcd.print(" Humid ");
lcd.print(humid);
lcd.print(" %");
lcd.setCursor(0, 3);
lcd.print(" ");
lcd.write(5);
lcd.print(" Soil ");
if (soil_sensor >= 400) {
lcd.print("0 %");
} else if (soil_sensor >= 300) {
lcd.print("40 %");
} else if (soil_sensor >= 200) {
lcd.print("90 %");
} else {
lcd.print("100 %");
}
}
void DEBUG(float temp, float humid) {
DateTime now = RTC.now();
DEBUG_PRINTLN("====================");
DEBUG_PRINT("Temp : ");
DEBUG_PRINTLN(temp);
DEBUG_PRINT("Humid : ");
DEBUG_PRINTLN(humid);
DEBUG_PRINT("Day : ");
DEBUG_PRINTLN(now.day(), DEC);
DEBUG_PRINT("Month : ");
DEBUG_PRINTLN(now.month(), DEC);
DEBUG_PRINT("Year : ");
DEBUG_PRINTLN(now.year(), DEC);
DEBUG_PRINT("H : ");
DEBUG_PRINTLN(now.hour(), DEC);
DEBUG_PRINT("M : ");
DEBUG_PRINTLN(now.minute(), DEC);
DEBUG_PRINTLN("====================");
DEBUG_PRINT("Setting hour : ");
DEBUG_PRINT(EEPROM.read(setting_t.eeprom_addr_h));
DEBUG_PRINT("-");
DEBUG_PRINTLN(EEPROM.read(setting_t.eeprom_addr_m));
DEBUG_PRINT("After Hour : ");
DEBUG_PRINT(EEPROM.read(setting_t.eeprom_addr_end_h));
DEBUG_PRINT("-");
DEBUG_PRINTLN(EEPROM.read(setting_t.eeprom_addr_end_m));
DEBUG_PRINTLN("====================");
DEBUG_PRINT("Mode auto : ");
DEBUG_PRINTLN(EEPROM.read(addr_mode_auto));
}
void get_heap() {
DEBUG_PRINT("Heap : ");
DEBUG_PRINTLN(Esp.getFreeHeap());
}
void get_modeTimer() {
DateTime now = RTC.now();
int h_old = EEPROM.read(setting_t.eeprom_addr_h);
int m_old = EEPROM.read(setting_t.eeprom_addr_m);
int h_new = EEPROM.read(setting_t.eeprom_addr_end_h);
int m_new = EEPROM.read(setting_t.eeprom_addr_end_m);
if (h_old > 0) {
if (h_old == now.hour() && m_old == now.minute()) {
digitalWrite(ACTIVE_RELEY, 0);
// DEBUG_PRINTLN("relay on");
} else {
if (now.hour() >= h_new && now.minute() >= m_new) {
digitalWrite(ACTIVE_RELEY, 1);
// DEBUG_PRINTLN("relay off");
}
}
}
}
void get_modeAuto() {
if (check_mode_auto == 1) {
int soil_read = analogRead(A0);
if (soil_read > 300) {
digitalWrite(ACTIVE_RELEY, 0);
} else {
digitalWrite(ACTIVE_RELEY, 1);
}
}
}
void FUNCTION_NORMAL() {
DateTime now = RTC.now();
float h = dht.readHumidity();
float t = dht.readTemperature();
int humid = dht.readHumidity();
int temp = dht.readTemperature();
int soil_sensor = analogRead(PIN_SOIL);
if (soil_sensor >= 400) {
soil_sensor = 0;
} else if (soil_sensor >= 300) {
soil_sensor = 40;
} else if (soil_sensor >= 200) {
soil_sensor = 90;
} else {
soil_sensor = 100;
}
lcd.home();
if (isnan(h) || isnan(t)) {
if (dht_counting_fail == 20) {
Esp.restart();
}
dht_counting_fail++;
DEBUG_PRINTLN("Failed to read from DHT sensor!");
return;
}
LCD_DISPLAY(t, h);
if (WiFi.status() == WL_CONNECTED ) {
if (now.minute() == 0) { // Send data every hour.
SENT_THINGSPEAK(temp, humid, soil_sensor);
}
} else {
autoConnect();
}
}
/*================ new =====================*/
// btn_push
int select_menu;
int buffer_left, buffer_right, buffer_center, buffer_back;
int permission_h, permission_m, permission_end_h, permission_end_m;
// setting_timer
// Star to end, hour and minute.
int buffer_h, buffer_m, buffer_end_h, buffer_end_m;
void btn_push() {
if (digitalRead(BTN_LEFT) == 1) {
buffer_left = 1;
} else {
if (buffer_left == 1) {
DEBUG_PRINT("LEFT : ");
buffer_left = 0;
if (permission_h == 1) { // Edit hour.
if (buffer_h == 0) {
buffer_h = 23;
lcd.clear();
} else {
buffer_h--;
lcd.clear();
}
DEBUG_PRINTLN(buffer_h);
} else if (permission_m == 1) { // Edit minute.
if (buffer_m == 0) {
buffer_m = 59;
lcd.clear();
} else {
buffer_m--;
lcd.clear();
}
DEBUG_PRINTLN(buffer_m);
} else if (permission_end_h == 1) { // Edit end hour.
if (buffer_end_h == 0) {
buffer_end_h = 23;
lcd.clear();
} else {
buffer_end_h--;
lcd.clear();
}
DEBUG_PRINTLN(buffer_end_h);
} else if (permission_end_m == 1) { // Edit end minute.
if (buffer_end_m == 0) {
buffer_end_m = 59;
lcd.clear();
} else {
buffer_end_m--;
lcd.clear();
}
DEBUG_PRINTLN(buffer_end_m);
}
}
} // End BTN_LEFT
if (digitalRead(BTN_RIGHT) == 1) {
buffer_right = 1;
} else {
if (buffer_right == 1) {
DEBUG_PRINT("RIGHT : ");
buffer_right = 0;
if (permission_h == 1) { // Edit hour.
if (buffer_h == 23) {
buffer_h = 0;
lcd.clear();
} else {
buffer_h++;
lcd.clear();
}
DEBUG_PRINTLN(buffer_h);
} else if (permission_m == 1){ // Edit minute.
if (buffer_m == 59) {
buffer_m = 0;
lcd.clear();
} else {
buffer_m++;
lcd.clear();
}
DEBUG_PRINTLN(buffer_m);
} if (permission_end_h == 1) { // Edit end hour.
if (buffer_end_h == 23) {
buffer_end_h = 0;
lcd.clear();
} else {
buffer_end_h++;
lcd.clear();
}
DEBUG_PRINTLN(buffer_end_h);
} else if (permission_end_m == 1){ // Edit end minute.
if (buffer_end_m == 59) {
buffer_end_m = 0;
lcd.clear();
} else {
buffer_end_m++;
lcd.clear();
}
DEBUG_PRINTLN(buffer_end_m);
}
if (permission_h == 0 && permission_m == 0) {
if (permission_end_h == 0 & permission_end_m == 0) {
if (select_menu == 3) {
lcd.clear();
select_menu = 0;
} else {
lcd.clear();
select_menu++;
}
}
}
}
} // End BTN_RIGHT
if (digitalRead(BTN_CENTER) == 1) {
buffer_center = 1;
} else {
if (buffer_center == 1) {
DEBUG_PRINTLN("CENTER");
buffer_center = 0;
if (select_menu == 0) {
permission_h = 1;
} else if (select_menu == 1) {
permission_m = 1;
} else if (select_menu == 2) {
permission_end_h = 1;
} else if (select_menu == 3) {
permission_end_m = 1;
}
}
} // End BTN_CENTER
if (digitalRead(BTN_BACK) == 1) {
buffer_back = 1;
} else {
if (buffer_back == 1) {
DEBUG_PRINTLN("BACK");
buffer_back = 0;
if (permission_h == 1) {
permission_h = 0;
} else if (permission_m == 1) {
permission_m = 0;
} else if (permission_end_h == 1) {
permission_end_h = 0;
} else if (permission_end_m == 1) {
permission_end_m = 0;
}
}
} // End BTN_BACK
if (buffer_center == 1 && buffer_back == 1) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Status : success.");
EEPROM.write(setting_t.eeprom_addr_h, buffer_h);
EEPROM.write(setting_t.eeprom_addr_m, buffer_m);
EEPROM.write(setting_t.eeprom_addr_end_h, buffer_end_h);
EEPROM.write(setting_t.eeprom_addr_end_m, buffer_end_m);
EEPROM.commit();
Esp.restart();
}
}
void setting_timer() {
if (select_menu == 0) {
// lcd.home();
lcd.setCursor(0, 0);
lcd.print("Start ");
lcd.write(4);
lcd.print(" Hour : ");
lcd.print(buffer_h);
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.print(" Minute : ");
lcd.print(buffer_m);
lcd.setCursor(0, 2);
lcd.print("End ");
lcd.print(" ");
lcd.print(" Hour : ");
lcd.print(buffer_end_h);
lcd.setCursor(0, 3);
lcd.print(" ");
lcd.print(" Minute : ");
lcd.print(buffer_end_m);
} else if (select_menu == 1) {
// lcd.home();
lcd.setCursor(0, 0);
lcd.print("Start ");
lcd.print(" Hour : ");
lcd.print(buffer_h);
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.write(4);
lcd.print(" Minute : ");
lcd.print(buffer_m);
lcd.setCursor(0, 2);
lcd.print("End ");
lcd.print(" ");
lcd.print(" Hour : ");
lcd.print(buffer_end_h);
lcd.setCursor(0, 3);
lcd.print(" ");
lcd.print(" Minute : ");
lcd.print(buffer_end_m);
} else if (select_menu == 2) {
lcd.setCursor(0, 0);
lcd.print("Start ");
lcd.print(" Hour : ");
lcd.print(buffer_h);
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.print(" Minute : ");
lcd.print(buffer_m);
lcd.setCursor(0, 2);
lcd.print("End ");
lcd.write(4);
lcd.print(" Hour : ");
lcd.print(buffer_end_h);
lcd.setCursor(0, 3);
lcd.print(" ");
lcd.print(" Minute : ");
lcd.print(buffer_end_m);
} else if (select_menu == 3) {
// lcd.home();
lcd.setCursor(0, 0);
lcd.print("Start ");
lcd.print(" Hour : ");
lcd.print(buffer_h);
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.print(" Minute : ");
lcd.print(buffer_m);
lcd.setCursor(0, 2);
lcd.print("End ");
lcd.print(" Hour : ");
lcd.print(buffer_end_h);
lcd.setCursor(0, 3);
lcd.print(" ");
lcd.write(4);
lcd.print(" Minute : ");
lcd.print(buffer_end_m);
}
btn_push();
}
void carbon_sms() {
if (client.connect("www.hiwinkeyelash.com", 80)) {
DEBUG_PRINTLN("Send carbon sms.");
// Make a HTTP request:
client.println("GET /nsc_final/sms_carbon.php HTTP/1.1");
client.println("Host: www.hiwinkeyelash.com");
client.println("Connection: close");
client.println();
} else {
// if you didn't get a connection to the server:
DEBUG_PRINTLN("Send carbon sms failed.");
}
}
void rain_sms() {
if (client.connect("www.hiwinkeyelash.com", 80)) {
DEBUG_PRINTLN("Send rain sms.");
// Make a HTTP request:
client.println("GET /nsc_final/sms_raining.php HTTP/1.1");
client.println("Host: www.hiwinkeyelash.com");
client.println("Connection: close");
client.println();
} else {
// if you didn't get a connection to the server:
DEBUG_PRINTLN("Send rain sms failed.");
}
}
void limit_sms_reset() {
DateTime now = RTC.now();
if (now.minute() == 0 && now.second() <= 10) { // every hour clear limit sms.
limit_sms_co = 0;
limit_sms_r = 0;
}
}
void setup()
{
Serial.begin(115200);
pinMode(BTN_LEFT, INPUT); // detect boot
pinMode(BTN_RIGHT, INPUT); // detect boot setting wifi
EEPROM.begin(512);
lcd.begin();
lcd.home();
if (digitalRead(BTN_LEFT) == 1 || strcmp(boot_mode, "setting") == 0) {
strcpy(boot_mode, "setting");
//pinMode(BTN_RIGHT, INPUT); // RIGHT
pinMode(BTN_CENTER, INPUT); // ENTER
pinMode(BTN_BACK, INPUT); // BACK
lcd.createChar(4, ICON_SELECT);
DEBUG_PRINTLN();
DEBUG_PRINTLN();
DEBUG_PRINTLN("Boot setting");
// ========== new ===========
lcd.clear();
// Star hour and minute.
buffer_h = (int)EEPROM.read(setting_t.eeprom_addr_h);
buffer_m = (int)EEPROM.read(setting_t.eeprom_addr_m);
// End hour and minute.
buffer_end_h = (int)EEPROM.read(setting_t.eeprom_addr_end_h);
buffer_end_m = (int)EEPROM.read(setting_t.eeprom_addr_end_m);
} else {
if (digitalRead(BTN_RIGHT) == 1) {
strcpy(boot_mode, "setting wifi");
pinMode(BTN_LEFT, OUTPUT);
pinMode(BTN_RIGHT, OUTPUT);
pinMode(ACTIVE_RELEY, OUTPUT);
digitalWrite(ACTIVE_RELEY, 1);
DEBUG_PRINTLN();
DEBUG_PRINTLN();
DEBUG_PRINTLN("Boot setting wifi.");
lcd.setCursor(0, 0);
lcd.print("Access Point.");
lcd.setCursor(0, 2);
lcd.print("IP : 192.168.0.100");
wifi_ap();
webserver_config();
} else {
pinMode(BTN_LEFT, OUTPUT);
pinMode(BTN_RIGHT, OUTPUT);
pinMode(ACTIVE_RELEY, OUTPUT);
pinMode(PIN_SOIL, INPUT);
digitalWrite(ACTIVE_RELEY, 1);
autoConnect();
int get_mode = EEPROM.read(addr_mode_auto);
if (get_mode == 1) {
check_mode_auto = 1;
} else {
check_mode_auto = 0;
}
dht.begin();
lcd.begin();
RTC.begin();
if (! RTC.isrunning()) {
DEBUG_PRINTLN("RTC is NOT running!");
RTC.adjust(DateTime(__DATE__, __TIME__));
}
DateTime now = RTC.now();
lcd.createChar(1, ICON_TIME);
lcd.createChar(2, ICON_TEMP);
lcd.createChar(3, ICON_HUMID);
lcd.createChar(4, ICON_SELECT);
lcd.createChar(5, ICON_SOIL);
lcd.createChar(6, ICON_CONNECT);
lcd.home();
}
}
}
void loop() {
if (strcmp(boot_mode, "setting") == 0) {
setting_timer();
} else {
if (strcmp(boot_mode, "setting wifi") == 0) {
server.handleClient();
} else {
// get_heap();
if (buffer_check == 0) {
FUNCTION_NORMAL();
get_modeTimer();
get_modeAuto();
}
limit_sms_reset();
if (Serial.available() > 0) {
limit_sms_reset();
buffer_check = 1;
switch (Serial.read()) {
case 48 : memory_rx[addr_rx] = '0'; addr_rx++; break;
case 49 : memory_rx[addr_rx] = '1'; addr_rx++; break;
case 50 : memory_rx[addr_rx] = '2'; addr_rx++; break;
case 51 : memory_rx[addr_rx] = '3'; addr_rx++; break;
case 52 : memory_rx[addr_rx] = '4'; addr_rx++; break;
case 53 : memory_rx[addr_rx] = '5'; addr_rx++; break;
case 54 : memory_rx[addr_rx] = '6'; addr_rx++; break;
case 55 : memory_rx[addr_rx] = '7'; addr_rx++; break;
case 56 : memory_rx[addr_rx] = '8'; addr_rx++; break;
case 57 : memory_rx[addr_rx] = '9'; addr_rx++; break;
case 65 : memory_rx[addr_rx] = 'A'; addr_rx++; break;
case 67 : memory_rx[addr_rx] = 'C'; addr_rx++; break;
case 72 : memory_rx[addr_rx] = 'H'; addr_rx++; break;
case 77 : memory_rx[addr_rx] = 'M'; addr_rx++; break;
case 82 : memory_rx[addr_rx] = 'R'; addr_rx++; break;
}
// A
if (addr_rx == 2 && memory_rx[0] == 'A') {
// DEBUG_PRINTLN(memory_rx[0]);
// DEBUG_PRINTLN(memory_rx[1]);
if (memory_rx[0] == 'A' && memory_rx[1] == '1') {
DEBUG_PRINTLN("A1")
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" I received msg");
lcd.setCursor(0, 2);
lcd.print(" Auto mode : on.");
delay(1000);
EEPROM.write(300, 1);
} else if (memory_rx[0] == 'A' && memory_rx[1] == '0') {
DEBUG_PRINTLN("A0");
lcd.clear();
lcd.print(" I received msg");
lcd.setCursor(0, 2);
lcd.print(" Auto mode : off.");
delay(1000);
EEPROM.write(300, 0);
}
addr_rx = 0;
EEPROM.commit();
Esp.restart();
}
// C is detect carbon.
if (addr_rx == 1 && memory_rx[0] == 'C') {
if (limit_sms_co == 0) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Status");
lcd.setCursor(0, 2);
lcd.print(" Co : sms sending.");
carbon_sms();
DEBUG_PRINTLN("Co : sms sending.");
delay(1000);
lcd.clear();
limit_sms_co = 1;
addr_rx = 0;
buffer_check = 0;
}
} // End limit sms co.
// R is detect rain.
if (addr_rx == 1 && memory_rx[0] == 'R') {
if (limit_sms_r == 0) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Status");
lcd.setCursor(0, 2);
lcd.print(" Rain : sms sending.");
rain_sms();
DEBUG_PRINTLN("Rain : sms sending.");
delay(1000);
lcd.clear();
limit_sms_r = 1;
addr_rx = 0;
buffer_check = 0;
}
} // End limit sms co.
if (addr_rx == 8) {
if (memory_rx[0] == '1' && memory_rx[1] == 'H') {
if (memory_rx[4] == '1' && memory_rx[5] == 'M') {
DEBUG_PRINT(memory_rx[0]);
DEBUG_PRINT(memory_rx[1]);
DEBUG_PRINT(memory_rx[2]);
DEBUG_PRINT(memory_rx[3]);
DEBUG_PRINT(memory_rx[4]);
DEBUG_PRINT(memory_rx[5]);
DEBUG_PRINT(memory_rx[6]);
DEBUG_PRINT(memory_rx[7]);
DEBUG_PRINTLN();
if (memory_rx[2] == '0') {
buffer_rx = (int)memory_rx[3] - 48;
DEBUG_PRINTLN(buffer_rx);
EEPROM.write(setting_t.eeprom_addr_h, buffer_rx);
} else {
buffer_rx = (int)memory_rx[2] - 48;
int cache_rx = (int)memory_rx[3] - 48;
switch (buffer_rx) {
case 1 : buffer_rx = (buffer_rx * 10) + cache_rx; break;
case 2 : buffer_rx = (buffer_rx * 10) + cache_rx; break;
}
DEBUG_PRINTLN(buffer_rx);
EEPROM.write(setting_t.eeprom_addr_h, buffer_rx);
}
// -------------------------------------------------
if (memory_rx[6] == '0') {
buffer_rx = (int)memory_rx[7] - 48;
DEBUG_PRINTLN(buffer_rx);
EEPROM.write(setting_t.eeprom_addr_m, buffer_rx);
} else {
buffer_rx = (int)memory_rx[6] - 48;
int cache_rx = (int)memory_rx[7] - 48;
switch (buffer_rx) {
case 1 : buffer_rx = (buffer_rx * 10) + cache_rx; break;
case 2 : buffer_rx = (buffer_rx * 10) + cache_rx; break;
case 3 : buffer_rx = (buffer_rx * 10) + cache_rx; break;
case 4 : buffer_rx = (buffer_rx * 10) + cache_rx; break;
case 5 : buffer_rx = (buffer_rx * 10) + cache_rx; break;
}
DEBUG_PRINTLN(buffer_rx);
EEPROM.write(setting_t.eeprom_addr_m, buffer_rx);
}
addr_rx = 0;
buffer_rx = 0;
}
}
// -------------------- Code End timer ---------------------
if (memory_rx[0] == '0' && memory_rx[1] == 'H') {
if (memory_rx[4] == '0' && memory_rx[5] == 'M') {
DEBUG_PRINT(memory_rx[0]);
DEBUG_PRINT(memory_rx[1]);
DEBUG_PRINT(memory_rx[2]);
DEBUG_PRINT(memory_rx[3]);
DEBUG_PRINT(memory_rx[4]);
DEBUG_PRINT(memory_rx[5]);
DEBUG_PRINT(memory_rx[6]);
DEBUG_PRINT(memory_rx[7]);
DEBUG_PRINTLN();
if (memory_rx[2] == '0') {
buffer_rx = (int)memory_rx[3] - 48;
DEBUG_PRINTLN(buffer_rx);
EEPROM.write(setting_t.eeprom_addr_end_h, buffer_rx);
} else {
buffer_rx = (int)memory_rx[2] - 48;
int cache_rx = (int)memory_rx[3] - 48;
switch (buffer_rx) {
case 1 : buffer_rx = (buffer_rx * 10) + cache_rx; break;
case 2 : buffer_rx = (buffer_rx * 10) + cache_rx; break;
}
DEBUG_PRINTLN(buffer_rx);
EEPROM.write(setting_t.eeprom_addr_end_h, buffer_rx);
}
// -------------------------------------------------
if (memory_rx[6] == '0') {
buffer_rx = (int)memory_rx[7] - 48;
DEBUG_PRINTLN(buffer_rx);
EEPROM.write(setting_t.eeprom_addr_end_m, buffer_rx);
} else {
buffer_rx = (int)memory_rx[6] - 48;
int cache_rx = (int)memory_rx[7] - 48;
switch (buffer_rx) {
case 1 : buffer_rx = (buffer_rx * 10) + cache_rx; break;
case 2 : buffer_rx = (buffer_rx * 10) + cache_rx; break;
case 3 : buffer_rx = (buffer_rx * 10) + cache_rx; break;
case 4 : buffer_rx = (buffer_rx * 10) + cache_rx; break;
case 5 : buffer_rx = (buffer_rx * 10) + cache_rx; break;
}
DEBUG_PRINTLN(buffer_rx);
EEPROM.write(setting_t.eeprom_addr_end_m, buffer_rx);
}
EEPROM.commit();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" I received msg");
lcd.setCursor(0, 2);
lcd.print(" Setting timer.");
Esp.restart();
}
}
} // End if addr_rx value 8
} // End read serial port.
}
}
delay(100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment