Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*2110/2022
- editing nextion OK
- */
- #include <TinyGPS++.h>
- TinyGPSPlus gps;
- //#define ss Serial1
- #include <WiFi.h>
- #define RXD_RS485 33 // rubah dari 27 ke 33
- #define TXD_RS485 32 //14 yg baru 32 yg lm
- #define pinLed 13
- #define RXD_GPS 17
- #define TXD_GPS 16
- String str_lat = "";
- String str_lon = "";
- String str_date = "";
- String str_time = "";
- int myIP;
- int _gauge_display;
- int val_sen, val_min;
- double val_max;
- const char* ssid = "untan";
- const char* password = "";
- int buf_detik, detik = 0;
- int jam, menit;
- int tahun, bulan, tanggal;
- int counter;
- int pin_value = 0;
- int progress_value;
- bool ledState = 0;
- /*Nextion */
- uint16_t voltageGraph; // a variable to store the reading
- // for simplicity reasons, we do not use float and we are going to take the measure in millivolts
- const int volt = 34;
- int potValue = 0;
- const int REFRESH_TIME = 100; // time to refresh the Nextion page every 100 ms
- unsigned long refresh_timer = millis();
- unsigned long cur_time, old_time;
- const byte hum[] = {0x01, 0x03, 0x00, 0x12, 0x00, 0x02, 0x64, 0x0e};
- const byte rh[] = {0x01, 0x03, 0x00, 0x12, 0x00, 0x01, 0x24, 0x0f};
- const byte condutiv[] = {0x01, 0x03, 0x00, 0x15, 0x00, 0x01, 0x95, 0xCE};
- const byte ph[] = {0x01, 0x03, 0x00, 0x06, 0x00, 0x01, 0x64, 0x0B};
- const byte nitro[] = {0x01, 0x03, 0x00, 0x1E, 0x00, 0x01, 0xE4, 0x0C};
- const byte phos[] = {0x01, 0x03, 0x00, 0x1F, 0x00, 0x01, 0xB5, 0xCC};
- const byte pota[] = {0x01, 0x03, 0x00, 0x20, 0x00, 0x01, 0x85, 0xC0};
- //const byte code[]= {0x01, 0x03, 0x00, 0x1e, 0x00, 0x03, 0x65, 0xCD};
- //const byte sens[] = {0x01, 0x03, 0x00, 0x00, 0x00, 0x07, 0x04, 0x08};
- byte values[11];
- int val_hum = 0;
- int val_temp = 0;
- float val_ph = 0;
- int val_con, val_nitro, val_phos, val_pota;
- int val_rh;
- String humid, temper, soilph, nitrogen, posf, kalium;
- static uint8_t npkAddr = 1;
- /*=========================================================================================================VOID SETUP===================================================================================*/
- void setup()
- {
- delay(100);
- pinMode(pinLed, OUTPUT);
- Serial.begin(115200);
- delay(100);
- WiFi.mode(WIFI_STA);
- WiFi.begin(ssid, password);
- int timeout = 10 * 4; // 10 seconds
- while (WiFi.status() != WL_CONNECTED && (timeout-- > 0)) {
- delay(250);
- }
- Serial.println("");
- if (WiFi.status() != WL_CONNECTED) {
- Serial.println("Failed to connect");
- }
- //Serial.print("WiFi connected in: ");
- //Serial.print(millis());
- Serial.print(ssid);
- Serial.print(":");
- Serial.println(WiFi.localIP());
- myIP = WiFi.localIP();
- String statIP = String(myIP);
- tampil_text("IP", statIP);
- Serial.println(statIP);
- Serial.println(myIP);
- delay(100);
- }
- /*============================================================================================================VOID LOOP================================================================================*/
- void loop() {
- //-------------------------------------task 1 -----------------------------------------------
- cur_time = millis();
- if (cur_time - old_time >= 3000) {
- Serial.println("baca Sensor");
- serial_rs485();
- for (int i = 0; i < 5; i++) {
- baca_hum();
- delay(250);
- }
- serial_gps();
- for (int i = 0; i < 5; i++) {
- read_gps();
- delay(250);
- }
- old_time = millis();
- }
- //-------------------------------------task 2 -----------------------------------------------
- if ((millis() - refresh_timer) > REFRESH_TIME) { //IMPORTANT do not have serial print commands in the loop without a delay
- // or an if statement with a timer condition like this one.
- //potValue = analogRead(volt); // Read the analog pin
- //voltageGraph = map(potValue, 0, 4095, 0, 100);
- //Serial.print("volt ");
- //Serial.println(voltageGraph);
- counter++;
- val_max = counter;
- Serial.println(val_max);
- tampil_text("statssid", ssid);
- tampil_text("twea", "35");
- tampil_text("tweh", "79");
- kirim_val("jtemp", "35");
- kirim_slider("jtemp", 3);
- String Moisture = String(val_hum);
- //String Soiltemp = String(val_temp);
- //String con = String (val_rh);
- /*
- tampil_text("tp1", Moisture); //hum
- tampil_text("tp2", Soiltemp); //val_temp
- tampil_text("tp3", soilph); //value 8
- tampil_text("tp4", con); // value tds
- tampil_text("t7", nitrogen); // nitrogen = String (values[8]);
- tampil_text("t8", posf); //String posf = String (values[i]);
- tampil_text("t9", kalium); //String kalium = String (values[i]);*/
- tampil_text("th1", Moisture);
- tampil_text("th2", Moisture);
- tampil_text("th3", Moisture);
- tampil_text("th4", Moisture);
- tampil_text("th5", Moisture);
- tampil_text("th6", Moisture);
- kirim_val("j0", Moisture);
- kirim_val("jn", Moisture);
- kirim_val("jp", Moisture);
- kirim_val("jk", Moisture);
- kirim_val("jth", Moisture);
- kirim_val("jtds", Moisture);
- kirim_val("jtur", Moisture);
- kirim_val("h0", Moisture);
- kirim_val("h1", Moisture);
- kirim_val("h2", Moisture);
- kirim_val("h3", Moisture);
- kirim_val("z0", Moisture);
- kirim_val("z1", Moisture);
- kirim_val("z2", Moisture);
- kirim_val("z3", Moisture);
- kirim_val("zf", Moisture);
- kirim_gauge("z0", val_max);
- kirim_gauge("z1", val_max);
- kirim_gauge("z2", val_max);
- kirim_gauge("z3", val_max);
- kirim_gauge("zf", val_max);
- kirim_slider("j0", val_max);
- kirim_slider("jn", val_max);
- kirim_slider("jp", val_max);
- kirim_slider("jk", val_max);
- kirim_slider("jth", val_max);
- kirim_slider("jtds", val_max);
- kirim_slider("jtur", val_max);
- kirim_slider("h0", val_max);
- kirim_slider("h1", val_max);
- kirim_slider("h2", val_max);
- kirim_slider("h3", val_max);
- tampil_text("tssn", Moisture);
- kirim_val("tssn", Moisture);
- //kirim_slider("tssn", Moisture);
- kirim_wave(val_max);
- refresh_timer = millis(); // Set the timer equal to millis, create a time stamp to start over the "delay"
- //old_time = millis();
- }
- }
- /*===================================================================================================================END VOID LOOP =========================================================================*/
- void serial_rs485() {
- Serial1.begin(4800, SERIAL_8N1, RXD_RS485, TXD_RS485);
- delay(500);
- }
- void serial_gps() {
- Serial1.begin(9600, SERIAL_8N1, RXD_GPS, TXD_GPS);
- delay(500);
- }
- void read_gps() {
- while (Serial1.available() > 0)
- if (gps.encode(Serial1.read()))
- displayInfo();
- if (millis() > 5000 && gps.charsProcessed() < 10)
- {
- Serial.println(F("No GPS detected: check wiring."));
- while (true);
- }
- if (buf_detik != detik) {
- String str_buf_date = "";
- String str_buf_time = "";
- String str_tgl = "";
- String str_bln = "";
- String str_jam = "";
- String str_menit = "";
- String str_detik = "";
- str_bln = bulan < 10 ? "0" + String(bulan) : String(bulan);
- str_tgl = tanggal < 10 ? "0" + String(tanggal) : String(tanggal);
- str_buf_date = str_bln + "/" + str_tgl + "/" + String(tahun) + " ";
- str_date = str_buf_date;
- Serial.print(" tanggal/jam: ");
- Serial.print(str_date);
- tampil_text("t0", str_date);
- str_detik = detik < 10 ? "0" + String(detik) : String(detik);
- str_menit = menit < 10 ? "0" + String(menit) : String(menit);
- str_jam = jam < 10 ? "0" + String(jam) : String(jam);
- str_buf_time = str_jam + ":" + str_menit + ":" + str_detik;
- str_time = str_buf_time;
- Serial.print(str_time);
- Serial.println();
- tampil_text("t1", str_time);
- buf_detik = detik;
- }
- }
- void displayInfo() {
- if (gps.date.isValid())
- {
- bulan = gps.date.month();
- tanggal = gps.date.day();
- tahun = gps.date.year();
- }
- else
- {
- Serial.print(F("INVALID"));
- }
- if (gps.time.isValid())
- {
- jam = gps.time.hour();
- detik = gps.time.second();
- menit = gps.time.minute();
- //convert to local zone, indonesia + 7
- jam = jam + 7;
- if (jam >= 24 ) {
- jam = jam - 24;
- }
- }
- else
- {
- Serial.print(F("INVALID"));
- }
- String str_buf_lat = "";
- String str_buf_lon = "";
- String str_buf_date = "";
- String str_buf_time = "";
- Serial.print(F("Location: "));
- if (gps.location.isValid())
- {
- str_buf_lat = String(gps.location.lat(), 6);
- str_buf_lon = String(gps.location.lng(), 6);
- Serial.print(str_buf_lat); Serial.print(",");
- Serial.println(str_buf_lon);
- tampil_text("tkx", str_buf_lat);
- tampil_text("tky", str_buf_lon);
- }
- else
- {
- Serial.print(F("INVALID"));
- tampil_text("tkx", "LOST");
- tampil_text("tky", "SIGNAL");
- }
- }
- /*====================================================================================================*/
- void baca_hum() {
- delay(10);
- if (Serial1.write(hum, sizeof(hum)) == 8) {
- for (byte i = 0; i < 9; i++) {
- values[i] = Serial1.read();
- Serial.print(values[i], HEX);
- Serial.print(",");
- }
- Serial.println();
- }
- Serial.print(values[3], HEX);
- Serial.println(values[4], HEX);
- Serial.print(values[5], HEX);
- Serial.println(values[6], HEX);
- val_hum = (values[3] << 8);// | values[4];
- val_temp = (values[5] << 8);// | values[6];
- String Moisture = String(val_hum, HEX);
- String Soiltemp = String(val_temp, HEX);
- Serial.print("hum: ");
- Serial.println(val_hum);
- tampil_text("tp1", Moisture); //hum
- Serial.print("temp: ");
- Serial.println(val_temp);
- tampil_text("tp3", soilph); //value 8
- }
- byte baca_rh() {
- delay(10);
- if (Serial1.write(rh, sizeof(rh)) == 8) {
- for (byte i = 0; i < 7; i++) {
- values[i] = Serial1.read();
- Serial.print(values[i], HEX);
- Serial.print(",");
- val_rh = (values[i], HEX);
- String con = String (val_rh);
- tampil_text("tp4", con); // value tds
- }
- Serial.println();
- }
- return values[4];
- }
- byte baca_con() {
- delay(10);
- if (Serial1.write(condutiv, sizeof(condutiv)) == 8) {
- for (byte i = 0; i < 7; i++) {
- values[i] = Serial1.read();
- Serial.print(values[i], HEX);
- Serial.print(",");
- }
- Serial.println();
- }
- return values[4];
- }
- byte baca_ph() {
- delay(100);
- if (Serial1.write(ph, sizeof(ph)) == 8) {
- for (byte i = 0; i < 7; i++) {
- values[i] = Serial1.read();
- Serial.print(values[i], HEX);
- String soilph = String (values[i], HEX);
- tampil_text("tp3", soilph); // ph = String (values[8]);
- Serial.print(",");
- }
- Serial.println();
- }
- return values[4];
- }
- byte baca_nitro() {
- delay(10);
- if (Serial1.write(nitro, sizeof(nitro)) == 8) {
- for (byte i = 0; i < 7; i++) {
- values[i] = Serial1.read();
- Serial.print(values[i], HEX);
- String nitrogen = String (values[i], HEX);
- tampil_text("t7", nitrogen); // nitrogen = String (values[8]);
- Serial.print(",");
- }
- Serial.println();
- }
- return values[4];
- }
- byte baca_phos() {
- delay(10);
- if (Serial1.write(phos, sizeof(phos)) == 8) {
- for (byte i = 0; i < 7; i++) {
- values[i] = Serial1.read();
- Serial.print(values[i], HEX);
- String posf = String (values[i], HEX);
- tampil_text("t8", posf); //String posf = String (values[i]);
- Serial.print(",");
- }
- Serial.println();
- }
- return values[4];
- }
- byte baca_pota() {
- delay(10);
- if (Serial1.write(pota, sizeof(pota)) == 8) {
- for (byte i = 0; i < 7; i++) {
- values[i] = Serial1.read();
- Serial.print(values[i], HEX);
- String kalium = String (values[i], HEX);
- tampil_text("t9", kalium); //String kalium = String (values[i]);
- Serial.print(",");
- }
- Serial.println();
- }
- return values[4];
- }
- void tampil_text(String _add_txt, String _txt_display) {
- String buf_txt = _add_txt + ".txt=" + "\"" + _txt_display + "\"";
- Serial1.print(buf_txt);
- Serial1.write(0xff);
- Serial1.write(0xff);
- Serial1.write(0xff);
- }
- void kirim_val(String _add_slide, String _txt_display) {
- String buf_slider = _add_slide + ".val=" + "\"" + _txt_display + "\"";
- Serial1.print(buf_slider);
- Serial1.write(0xff);
- Serial1.write(0xff);
- Serial1.write(0xff);
- }
- void kirim_gauge(String _add_gauge, int _gauge_display) {
- String warna_jingga = _add_gauge + ".pco=64520";
- String warna_merah = _add_gauge + ".pco=63488";
- String warna_kuning = _add_gauge + ".pco=65519";
- String warna_hijau = _add_gauge + ".pco=34784";
- String warna_bg_green = _add_gauge + ".pco=1024";
- int sensgauge = constrain(_gauge_display, 0, 255); //(buf_senval,0,2048);
- int val_gauge = map(sensgauge, 0, 255, 0, 180); //(sens, 300, 2048, 50, 0);
- Serial1.print("valgauge");
- Serial1.print(val_gauge);
- Serial1.print(_add_gauge);
- Serial1.print(".val=");
- Serial1.print(val_gauge);
- Serial1.write(0xff);
- Serial1.write(0xff);
- Serial1.write(0xff);
- //String buf_gauge = _add_gauge + ".val=" + "\"" + val_gauge+ "\"";
- //Serial1.print(buf_gauge);
- if (val_gauge <= 15) { //}for (val_min > 80; val_min <= 100; val_min++){
- Serial1.print(warna_merah);
- Serial1.write(0xff);
- Serial1.write(0xff);
- Serial1.write(0xff);
- } else if ((val_gauge > 15) && (val_gauge <= 37)) {
- Serial1.print(warna_jingga);
- Serial1.write(0xff);
- Serial1.write(0xff);
- Serial1.write(0xff);
- } else if ((val_gauge > 37) && (val_gauge <= 60)) {
- Serial1.print(warna_kuning);
- Serial1.write(0xff);
- Serial1.write(0xff);
- Serial1.write(0xff);
- } else if ((val_gauge > 60) && (val_gauge <= 80)) {
- Serial1.print(warna_hijau);
- Serial1.write(0xff);
- Serial1.write(0xff);
- Serial1.write(0xff);
- } else if ((val_gauge > 80) && (val_gauge <= 100)) {
- Serial1.print(warna_bg_green);
- Serial1.write(0xff);
- Serial1.write(0xff);
- Serial1.write(0xff);
- } delay(100);
- }
- void kirim_slider(String _add_col, int val_sen) {
- //String warna_biru = _add_col + ".pco=64520";
- String warna_merah = _add_col + ".pco=63488";
- String warna_kuning = _add_col + ".pco=65519";
- String warna_hijau = _add_col + ".pco=34784";
- String warna_bg_green = _add_col + ".pco=1024";
- //int buf_senval = val_sen.toInt();
- int sens = constrain(val_sen, 0, 400); //(buf_senval,0,2048);
- int val_min = map(sens, 0, 400, 0, 100); //(sens, 300, 2048, 50, 0);
- //String buf_slider = _add_col + ".val=" + "\"" + val_min+ "\"";
- //Serial1.print(buf_slider);
- Serial1.print(_add_col);
- Serial1.print(".val=");
- Serial1.print(val_min);
- Serial1.write(0xff);
- Serial1.write(0xff);
- Serial1.write(0xff);
- if (val_min <= 20) { //}for (val_min > 80; val_min <= 100; val_min++){
- Serial1.print(warna_merah);
- Serial1.write(0xff);
- Serial1.write(0xff);
- Serial1.write(0xff);
- } else if ((val_min > 20) && (val_min <= 40)) {
- Serial1.print(warna_hijau);
- Serial1.write(0xff);
- Serial1.write(0xff);
- Serial1.write(0xff);
- } else if ((val_min > 40) && (val_min <= 60)) {
- Serial1.print(warna_bg_green);
- Serial1.write(0xff);
- Serial1.write(0xff);
- Serial1.write(0xff);
- } else if ((val_min > 60) && (val_min <= 80)) {
- Serial1.print(warna_kuning);
- Serial1.write(0xff);
- Serial1.write(0xff);
- Serial1.write(0xff);
- } else if ((val_min > 80) && (val_min <= 100)) {
- Serial1.print(warna_merah);
- Serial1.write(0xff);
- Serial1.write(0xff);
- Serial1.write(0xff);
- } delay(100);
- }
- void kirim_wave(int wave_val) {
- int senswave = constrain(wave_val, 0, 255); //(buf_senval,0,2048);
- int val_wave = map(senswave, 0, 255, 0, 255); //(sens, 300, 2048, 50, 0);
- Serial1.print("valwave");
- Serial1.print(val_wave);
- //Serial1.print("add 1,1," + val_wave);
- String tosend = "add ";
- tosend += 1;//id
- tosend += ",";
- tosend += 1; //channel
- tosend += ",";
- tosend += val_wave;
- Serial1.print(tosend);
- Serial1.write(0xff);
- Serial1.write(0xff);
- Serial1.write(0xff);
- delay(600);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement