Advertisement
mikroavr

ESP32GPS

Oct 23rd, 2022 (edited)
1,283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 16.07 KB | None | 0 0
  1. /*2110/2022
  2.    editing nextion OK
  3. */
  4.  
  5. #include <TinyGPS++.h>
  6. TinyGPSPlus gps;
  7. //#define ss Serial1
  8. #include <WiFi.h>
  9.  
  10. #define RXD_RS485 33 // rubah dari 27 ke 33
  11. #define TXD_RS485 32  //14 yg baru 32 yg lm
  12.  
  13. #define pinLed 13
  14.  
  15. #define RXD_GPS 17
  16. #define TXD_GPS 16
  17.  
  18. String str_lat = "";
  19. String str_lon = "";
  20. String str_date = "";
  21. String str_time = "";
  22.  
  23. int myIP;
  24. int _gauge_display;
  25. int val_sen, val_min;
  26. double val_max;
  27. const char* ssid     = "untan";
  28. const char* password = "";
  29.  
  30. int buf_detik, detik = 0;
  31. int jam, menit;
  32. int tahun, bulan, tanggal;
  33. int counter;
  34. int pin_value = 0;
  35. int progress_value;
  36. bool ledState = 0;
  37.  
  38. /*Nextion */
  39. uint16_t voltageGraph;   // a variable to store the reading
  40. // for simplicity reasons, we do not use float and we are going to take the measure in millivolts
  41.  
  42. const int volt = 34;
  43. int potValue = 0;
  44. const int REFRESH_TIME = 100;           // time to refresh the Nextion page every 100 ms
  45. unsigned long refresh_timer = millis();
  46. unsigned long cur_time, old_time;
  47.  
  48. const byte hum[] =      {0x01, 0x03, 0x00, 0x12, 0x00, 0x02, 0x64, 0x0e};
  49. const byte rh[] =       {0x01, 0x03, 0x00, 0x12, 0x00, 0x01, 0x24, 0x0f};
  50. const byte condutiv[] = {0x01, 0x03, 0x00, 0x15, 0x00, 0x01, 0x95, 0xCE};
  51. const byte ph[] =       {0x01, 0x03, 0x00, 0x06, 0x00, 0x01, 0x64, 0x0B};
  52. const byte nitro[] =    {0x01, 0x03, 0x00, 0x1E, 0x00, 0x01, 0xE4, 0x0C};
  53. const byte phos[] =     {0x01, 0x03, 0x00, 0x1F, 0x00, 0x01, 0xB5, 0xCC};
  54. const byte pota[] =     {0x01, 0x03, 0x00, 0x20, 0x00, 0x01, 0x85, 0xC0};
  55.  
  56. //const byte code[]= {0x01, 0x03, 0x00, 0x1e, 0x00, 0x03, 0x65, 0xCD};
  57. //const byte sens[] = {0x01, 0x03, 0x00, 0x00, 0x00, 0x07, 0x04, 0x08};
  58.  
  59.  
  60. byte values[11];
  61. int val_hum = 0;
  62. int val_temp = 0;
  63. float val_ph = 0;
  64.  
  65. int val_con, val_nitro, val_phos, val_pota;
  66. int val_rh;
  67. String humid, temper, soilph, nitrogen, posf, kalium;
  68.  
  69. static uint8_t npkAddr = 1;
  70.  
  71.  
  72. /*=========================================================================================================VOID SETUP===================================================================================*/
  73. void setup()
  74. {
  75.   delay(100);
  76.   pinMode(pinLed, OUTPUT);
  77.   Serial.begin(115200);
  78.   delay(100);
  79.   WiFi.mode(WIFI_STA);
  80.   WiFi.begin(ssid, password);
  81.   int timeout = 10 * 4; // 10 seconds
  82.   while (WiFi.status() != WL_CONNECTED  && (timeout-- > 0)) {
  83.     delay(250);
  84.   }
  85.   Serial.println("");
  86.  
  87.   if (WiFi.status() != WL_CONNECTED) {
  88.     Serial.println("Failed to connect");
  89.   }
  90.   //Serial.print("WiFi connected in: ");
  91.   //Serial.print(millis());
  92.   Serial.print(ssid);
  93.   Serial.print(":");
  94.   Serial.println(WiFi.localIP());
  95.   myIP = WiFi.localIP();
  96.   String statIP = String(myIP);
  97.   tampil_text("IP", statIP);
  98.   Serial.println(statIP);
  99.   Serial.println(myIP);
  100.   delay(100);
  101. }
  102.  
  103. /*============================================================================================================VOID LOOP================================================================================*/
  104.  
  105. void loop() {
  106.  
  107. //-------------------------------------task 1 -----------------------------------------------
  108.   cur_time = millis();
  109.   if (cur_time - old_time >= 3000) {
  110.     Serial.println("baca Sensor");
  111.     serial_rs485();
  112.     for (int i = 0; i < 5; i++) {
  113.       baca_hum();
  114.       delay(250);
  115.     }
  116.  
  117.     serial_gps();
  118.  
  119.     for (int i = 0; i < 5; i++) {
  120.       read_gps();
  121.       delay(250);
  122.     }
  123.  
  124.     old_time = millis();
  125.  
  126.   }
  127.  
  128. //-------------------------------------task 2 -----------------------------------------------
  129.  
  130.   if ((millis() - refresh_timer) > REFRESH_TIME) { //IMPORTANT do not have serial print commands in the loop without a delay
  131.     // or an if statement with a timer condition like this one.
  132.  
  133.     //potValue = analogRead(volt);  // Read the analog pin
  134.     //voltageGraph = map(potValue, 0, 4095, 0, 100);
  135.     //Serial.print("volt  ");
  136.     //Serial.println(voltageGraph);
  137.     counter++;
  138.     val_max = counter;
  139.     Serial.println(val_max);
  140.     tampil_text("statssid", ssid);
  141.     tampil_text("twea", "35");
  142.     tampil_text("tweh", "79");
  143.     kirim_val("jtemp", "35");
  144.     kirim_slider("jtemp", 3);
  145.     String Moisture = String(val_hum);
  146.     //String Soiltemp = String(val_temp);
  147.     //String con = String (val_rh);
  148.     /*
  149.  
  150.       tampil_text("tp1", Moisture); //hum
  151.       tampil_text("tp2", Soiltemp); //val_temp
  152.       tampil_text("tp3", soilph); //value 8
  153.       tampil_text("tp4", con); // value tds
  154.       tampil_text("t7", nitrogen); // nitrogen = String (values[8]);
  155.       tampil_text("t8", posf); //String posf = String (values[i]);
  156.       tampil_text("t9", kalium); //String kalium = String (values[i]);*/
  157.     tampil_text("th1", Moisture);
  158.     tampil_text("th2", Moisture);
  159.     tampil_text("th3", Moisture);
  160.     tampil_text("th4", Moisture);
  161.     tampil_text("th5", Moisture);
  162.     tampil_text("th6", Moisture);
  163.  
  164.     kirim_val("j0", Moisture);
  165.     kirim_val("jn", Moisture);
  166.     kirim_val("jp", Moisture);
  167.     kirim_val("jk", Moisture);
  168.     kirim_val("jth", Moisture);
  169.     kirim_val("jtds", Moisture);
  170.     kirim_val("jtur", Moisture);
  171.  
  172.  
  173.     kirim_val("h0", Moisture);
  174.     kirim_val("h1", Moisture);
  175.     kirim_val("h2", Moisture);
  176.     kirim_val("h3", Moisture);
  177.  
  178.     kirim_val("z0", Moisture);
  179.     kirim_val("z1", Moisture);
  180.     kirim_val("z2", Moisture);
  181.     kirim_val("z3", Moisture);
  182.     kirim_val("zf", Moisture);
  183.  
  184.     kirim_gauge("z0", val_max);
  185.     kirim_gauge("z1", val_max);
  186.     kirim_gauge("z2", val_max);
  187.     kirim_gauge("z3", val_max);
  188.     kirim_gauge("zf", val_max);
  189.  
  190.     kirim_slider("j0", val_max);
  191.     kirim_slider("jn", val_max);
  192.     kirim_slider("jp", val_max);
  193.     kirim_slider("jk", val_max);
  194.     kirim_slider("jth", val_max);
  195.     kirim_slider("jtds", val_max);
  196.     kirim_slider("jtur", val_max);
  197.  
  198.     kirim_slider("h0", val_max);
  199.     kirim_slider("h1", val_max);
  200.     kirim_slider("h2", val_max);
  201.     kirim_slider("h3", val_max);
  202.  
  203.  
  204.  
  205.     tampil_text("tssn", Moisture);
  206.     kirim_val("tssn", Moisture);
  207.     //kirim_slider("tssn", Moisture);
  208.     kirim_wave(val_max);
  209.  
  210.     refresh_timer = millis();  // Set the timer equal to millis, create a time stamp to start over the "delay"
  211.     //old_time = millis();
  212.  
  213.   }
  214. }
  215.  
  216. /*===================================================================================================================END  VOID LOOP =========================================================================*/
  217.  
  218. void serial_rs485() {
  219.   Serial1.begin(4800, SERIAL_8N1, RXD_RS485, TXD_RS485);
  220.   delay(500);
  221. }
  222.  
  223. void serial_gps() {
  224.   Serial1.begin(9600, SERIAL_8N1, RXD_GPS, TXD_GPS);
  225.   delay(500);
  226. }
  227. void read_gps() {
  228.   while (Serial1.available() > 0)
  229.     if (gps.encode(Serial1.read()))
  230.       displayInfo();
  231.  
  232.   if (millis() > 5000 && gps.charsProcessed() < 10)
  233.   {
  234.     Serial.println(F("No GPS detected: check wiring."));
  235.     while (true);
  236.   }
  237.   if (buf_detik != detik) {
  238.     String str_buf_date = "";
  239.     String str_buf_time = "";
  240.  
  241.     String str_tgl = "";
  242.     String str_bln = "";
  243.  
  244.     String str_jam = "";
  245.     String str_menit = "";
  246.     String str_detik = "";
  247.  
  248.     str_bln = bulan < 10 ? "0" + String(bulan) : String(bulan);
  249.     str_tgl = tanggal < 10 ? "0" + String(tanggal) : String(tanggal);
  250.  
  251.     str_buf_date = str_bln + "/" + str_tgl + "/" + String(tahun) + " ";
  252.     str_date = str_buf_date;
  253.  
  254.     Serial.print(" tanggal/jam: ");
  255.     Serial.print(str_date);
  256.     tampil_text("t0", str_date);
  257.  
  258.  
  259.     str_detik = detik < 10 ? "0" + String(detik) : String(detik);
  260.     str_menit = menit < 10 ? "0" + String(menit) : String(menit);
  261.     str_jam = jam < 10 ? "0" + String(jam) : String(jam);
  262.  
  263.  
  264.     str_buf_time = str_jam + ":" + str_menit + ":" + str_detik;
  265.     str_time = str_buf_time;
  266.     Serial.print(str_time);
  267.     Serial.println();
  268.     tampil_text("t1", str_time);
  269.     buf_detik = detik;
  270.  
  271.   }
  272. }
  273.  
  274. void displayInfo() {
  275.  
  276.   if (gps.date.isValid())
  277.   {
  278.     bulan = gps.date.month();
  279.     tanggal = gps.date.day();
  280.     tahun = gps.date.year();
  281.  
  282.   }
  283.   else
  284.   {
  285.     Serial.print(F("INVALID"));
  286.   }
  287.  
  288.   if (gps.time.isValid())
  289.   {
  290.     jam =  gps.time.hour();
  291.     detik = gps.time.second();
  292.     menit = gps.time.minute();
  293.  
  294.     //convert to local zone, indonesia + 7
  295.     jam = jam + 7;
  296.     if (jam >= 24 ) {
  297.       jam = jam - 24;
  298.     }
  299.   }
  300.   else
  301.   {
  302.     Serial.print(F("INVALID"));
  303.   }
  304.   String str_buf_lat = "";
  305.   String str_buf_lon = "";
  306.   String str_buf_date = "";
  307.   String str_buf_time = "";
  308.   Serial.print(F("Location: "));
  309.   if (gps.location.isValid())
  310.   {
  311.     str_buf_lat = String(gps.location.lat(), 6);
  312.     str_buf_lon = String(gps.location.lng(), 6);
  313.     Serial.print(str_buf_lat); Serial.print(",");
  314.     Serial.println(str_buf_lon);
  315.     tampil_text("tkx", str_buf_lat);
  316.     tampil_text("tky", str_buf_lon);
  317.   }
  318.   else
  319.   {
  320.     Serial.print(F("INVALID"));
  321.     tampil_text("tkx", "LOST");
  322.     tampil_text("tky", "SIGNAL");
  323.   }
  324. }
  325.  
  326. /*====================================================================================================*/
  327.  
  328. void baca_hum() {
  329.  
  330.   delay(10);
  331.   if (Serial1.write(hum, sizeof(hum)) == 8) {
  332.     for (byte i = 0; i < 9; i++) {
  333.       values[i] = Serial1.read();
  334.       Serial.print(values[i], HEX);
  335.       Serial.print(",");
  336.     }
  337.     Serial.println();
  338.   }
  339.   Serial.print(values[3], HEX);
  340.   Serial.println(values[4], HEX);
  341.  
  342.   Serial.print(values[5], HEX);
  343.   Serial.println(values[6], HEX);
  344.  
  345.   val_hum = (values[3] << 8);// | values[4];
  346.   val_temp = (values[5] << 8);// | values[6];
  347.   String Moisture = String(val_hum, HEX);
  348.   String Soiltemp = String(val_temp, HEX);
  349.  
  350.   Serial.print("hum: ");
  351.   Serial.println(val_hum);
  352.   tampil_text("tp1", Moisture); //hum
  353.   Serial.print("temp: ");
  354.   Serial.println(val_temp);
  355.   tampil_text("tp3", soilph); //value 8
  356.  
  357. }
  358.  
  359. byte baca_rh() {
  360.   delay(10);
  361.   if (Serial1.write(rh, sizeof(rh)) == 8) {
  362.     for (byte i = 0; i < 7; i++) {
  363.       values[i] = Serial1.read();
  364.       Serial.print(values[i], HEX);
  365.       Serial.print(",");
  366.       val_rh = (values[i], HEX);
  367.       String con = String (val_rh);
  368.       tampil_text("tp4", con); // value tds
  369.  
  370.     }
  371.     Serial.println();
  372.   }
  373.   return values[4];
  374. }
  375.  
  376. byte baca_con() {
  377.   delay(10);
  378.   if (Serial1.write(condutiv, sizeof(condutiv)) == 8) {
  379.     for (byte i = 0; i < 7; i++) {
  380.       values[i] = Serial1.read();
  381.  
  382.       Serial.print(values[i], HEX);
  383.       Serial.print(",");
  384.     }
  385.     Serial.println();
  386.   }
  387.   return values[4];
  388. }
  389.  
  390. byte baca_ph() {
  391.   delay(100);
  392.   if (Serial1.write(ph, sizeof(ph)) == 8) {
  393.     for (byte i = 0; i < 7; i++) {
  394.       values[i] = Serial1.read();
  395.       Serial.print(values[i], HEX);
  396.       String soilph = String (values[i], HEX);
  397.       tampil_text("tp3", soilph); // ph = String (values[8]);
  398.  
  399.       Serial.print(",");
  400.     }
  401.     Serial.println();
  402.   }
  403.   return values[4];
  404. }
  405.  
  406. byte baca_nitro() {
  407.   delay(10);
  408.   if (Serial1.write(nitro, sizeof(nitro)) == 8) {
  409.     for (byte i = 0; i < 7; i++) {
  410.       values[i] = Serial1.read();
  411.       Serial.print(values[i], HEX);
  412.       String nitrogen = String (values[i], HEX);
  413.       tampil_text("t7", nitrogen); // nitrogen = String (values[8]);
  414.  
  415.       Serial.print(",");
  416.     }
  417.     Serial.println();
  418.   }
  419.   return values[4];
  420. }
  421.  
  422. byte baca_phos() {
  423.   delay(10);
  424.   if (Serial1.write(phos, sizeof(phos)) == 8) {
  425.     for (byte i = 0; i < 7; i++) {
  426.       values[i] = Serial1.read();
  427.       Serial.print(values[i], HEX);
  428.       String posf = String (values[i], HEX);
  429.       tampil_text("t8", posf); //String posf = String (values[i]);
  430.  
  431.       Serial.print(",");
  432.     }
  433.     Serial.println();
  434.   }
  435.   return values[4];
  436. }
  437.  
  438. byte baca_pota() {
  439.   delay(10);
  440.   if (Serial1.write(pota, sizeof(pota)) == 8) {
  441.     for (byte i = 0; i < 7; i++) {
  442.       values[i] = Serial1.read();
  443.       Serial.print(values[i], HEX);
  444.       String kalium = String (values[i], HEX);
  445.       tampil_text("t9", kalium); //String kalium = String (values[i]);
  446.  
  447.       Serial.print(",");
  448.     }
  449.     Serial.println();
  450.   }
  451.   return values[4];
  452. }
  453.  
  454.  
  455.  
  456. void tampil_text(String _add_txt, String _txt_display) {
  457.   String buf_txt = _add_txt + ".txt="  + "\"" + _txt_display + "\"";
  458.   Serial1.print(buf_txt);
  459.   Serial1.write(0xff);
  460.   Serial1.write(0xff);
  461.   Serial1.write(0xff);
  462. }
  463.  
  464. void kirim_val(String _add_slide, String _txt_display) {
  465.   String buf_slider = _add_slide + ".val="  + "\"" + _txt_display + "\"";
  466.   Serial1.print(buf_slider);
  467.   Serial1.write(0xff);
  468.   Serial1.write(0xff);
  469.   Serial1.write(0xff);
  470. }
  471. void kirim_gauge(String _add_gauge, int _gauge_display) {
  472.   String warna_jingga = _add_gauge + ".pco=64520";
  473.   String warna_merah = _add_gauge + ".pco=63488";
  474.   String warna_kuning = _add_gauge + ".pco=65519";
  475.   String warna_hijau = _add_gauge + ".pco=34784";
  476.   String warna_bg_green = _add_gauge + ".pco=1024";
  477.   int sensgauge = constrain(_gauge_display, 0, 255); //(buf_senval,0,2048);
  478.   int val_gauge = map(sensgauge, 0, 255, 0, 180); //(sens, 300, 2048, 50, 0);
  479.   Serial1.print("valgauge");
  480.   Serial1.print(val_gauge);
  481.   Serial1.print(_add_gauge);
  482.   Serial1.print(".val=");
  483.   Serial1.print(val_gauge);
  484.   Serial1.write(0xff);
  485.   Serial1.write(0xff);
  486.   Serial1.write(0xff);
  487.   //String buf_gauge = _add_gauge + ".val="  + "\"" + val_gauge+ "\"";
  488.   //Serial1.print(buf_gauge);
  489.   if (val_gauge <= 15) { //}for (val_min > 80; val_min <= 100; val_min++){
  490.     Serial1.print(warna_merah);
  491.     Serial1.write(0xff);
  492.     Serial1.write(0xff);
  493.     Serial1.write(0xff);
  494.   } else if ((val_gauge > 15) && (val_gauge <= 37)) {
  495.     Serial1.print(warna_jingga);
  496.     Serial1.write(0xff);
  497.     Serial1.write(0xff);
  498.     Serial1.write(0xff);
  499.   } else if ((val_gauge > 37) && (val_gauge <= 60)) {
  500.     Serial1.print(warna_kuning);
  501.     Serial1.write(0xff);
  502.     Serial1.write(0xff);
  503.     Serial1.write(0xff);
  504.   } else if ((val_gauge > 60) && (val_gauge <= 80)) {
  505.     Serial1.print(warna_hijau);
  506.     Serial1.write(0xff);
  507.     Serial1.write(0xff);
  508.     Serial1.write(0xff);
  509.   } else if ((val_gauge > 80) && (val_gauge <= 100)) {
  510.     Serial1.print(warna_bg_green);
  511.     Serial1.write(0xff);
  512.     Serial1.write(0xff);
  513.     Serial1.write(0xff);
  514.   } delay(100);
  515. }
  516.  
  517.  
  518. void kirim_slider(String _add_col, int val_sen) {
  519.   //String warna_biru = _add_col + ".pco=64520";
  520.   String warna_merah = _add_col + ".pco=63488";
  521.   String warna_kuning = _add_col + ".pco=65519";
  522.   String warna_hijau = _add_col + ".pco=34784";
  523.   String warna_bg_green = _add_col + ".pco=1024";
  524.  
  525.   //int buf_senval = val_sen.toInt();
  526.   int sens = constrain(val_sen, 0, 400); //(buf_senval,0,2048);
  527.   int val_min = map(sens, 0, 400, 0, 100); //(sens, 300, 2048, 50, 0);
  528.   //String buf_slider = _add_col + ".val="  + "\"" + val_min+ "\"";
  529.   //Serial1.print(buf_slider);
  530.   Serial1.print(_add_col);
  531.   Serial1.print(".val=");
  532.   Serial1.print(val_min);
  533.   Serial1.write(0xff);
  534.   Serial1.write(0xff);
  535.   Serial1.write(0xff);
  536.   if (val_min <= 20) { //}for (val_min > 80; val_min <= 100; val_min++){
  537.     Serial1.print(warna_merah);
  538.     Serial1.write(0xff);
  539.     Serial1.write(0xff);
  540.     Serial1.write(0xff);
  541.   } else if ((val_min > 20) && (val_min <= 40)) {
  542.     Serial1.print(warna_hijau);
  543.     Serial1.write(0xff);
  544.     Serial1.write(0xff);
  545.     Serial1.write(0xff);
  546.   } else if ((val_min > 40) && (val_min <= 60)) {
  547.     Serial1.print(warna_bg_green);
  548.     Serial1.write(0xff);
  549.     Serial1.write(0xff);
  550.     Serial1.write(0xff);
  551.   } else if ((val_min > 60) && (val_min <= 80)) {
  552.     Serial1.print(warna_kuning);
  553.     Serial1.write(0xff);
  554.     Serial1.write(0xff);
  555.     Serial1.write(0xff);
  556.   } else if ((val_min > 80) && (val_min <= 100)) {
  557.     Serial1.print(warna_merah);
  558.     Serial1.write(0xff);
  559.     Serial1.write(0xff);
  560.     Serial1.write(0xff);
  561.   } delay(100);
  562. }
  563.  
  564. void kirim_wave(int wave_val) {
  565.   int senswave = constrain(wave_val, 0, 255); //(buf_senval,0,2048);
  566.   int val_wave = map(senswave, 0, 255, 0, 255); //(sens, 300, 2048, 50, 0);
  567.   Serial1.print("valwave");
  568.   Serial1.print(val_wave);
  569.   //Serial1.print("add 1,1," + val_wave);
  570.   String tosend = "add ";
  571.   tosend += 1;//id
  572.   tosend += ",";
  573.   tosend += 1; //channel
  574.   tosend += ",";
  575.   tosend += val_wave;
  576.   Serial1.print(tosend);
  577.   Serial1.write(0xff);
  578.   Serial1.write(0xff);
  579.   Serial1.write(0xff);
  580.   delay(600);
  581. }
  582.  
  583.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement