Advertisement
Guest User

Code Timestamp

a guest
Jun 17th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 17.67 KB | None | 0 0
  1. /*
  2.  *  
  3.  *  BOARD ESP8266 2.4.2
  4. */
  5.  
  6. #define SS_PIN D4  
  7. #define RST_PIN D3
  8.  
  9. #define SDA_PIN D2
  10. #define SCL_PIN D1
  11.  
  12. #include <ESP8266WiFi.h>
  13. #include "ESP8266HTTPClient.h" //Lokal im Ordner ist wichtig
  14. #include <ArduinoJson.h>
  15.  
  16. #include <Wire.h>
  17. #include <SPI.h>
  18. #include <MFRC522.h>
  19. #include <LiquidCrystal_I2C.h>
  20.  
  21.  
  22. #include <NTPClient.h>
  23. #include <WifiUdp.h>
  24.  
  25. #define NTP_OFFSET   7200    //  60 * 60 In seconds
  26. #define NTP_INTERVAL 60 * 1000    // In miliseconds
  27. #define NTP_ADDRESS  "0.de.pool.ntp.org"
  28.  
  29.  
  30. WiFiUDP ntpUDP;
  31. NTPClient timeClient(ntpUDP, NTP_ADDRESS, NTP_OFFSET, NTP_INTERVAL);
  32.  
  33.  
  34.  
  35. const char* ssid = "Guest";
  36. const char* password =  "12345678";
  37.  
  38.  
  39. String terminal_id="ZZALPHA";
  40. String booking_process_array[5][3]={
  41.     {"0","Arbeitszeit","1"},
  42.     {"1","Dienstgang","2"},
  43.     {"2","Mittagspause","3"},
  44.     {"3","Pause","4"},
  45.     {"4","Raucherpause","5"}
  46. };
  47.  
  48. String booking_event_array[3][4]={
  49.     {"0","system","system","1"},
  50.     {"1","Beginn","Eingebucht","6"},
  51.     {"2","Ende","Ausgebucht","9"}
  52. };
  53.  
  54. struct return_values {
  55.   bool keypress;
  56.   String card_id;
  57.   int http_response;
  58.   String message;
  59.   String result;
  60. };
  61.  
  62. struct send_response {
  63.   bool succes;
  64.   String result;
  65.   String message;
  66.   int http_response;
  67. };
  68.  
  69.  
  70.  
  71.  
  72. LiquidCrystal_I2C lcd(0x27,20,4);  // set the LCD address to 0x27 for a 16 chars and 2 line display
  73.  
  74.  
  75. MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.
  76. int statuss = 0;
  77. int out = 0;
  78. bool inMenueStruc=false;
  79. int menuePos =0;
  80. bool changeMenue=false;
  81. int menue_seconds=0;
  82. int RetVal=0;
  83. bool button_flag=false;
  84. String button ="";
  85. const long refresh_time_interval = 9900; //885
  86. unsigned long previousSeconds = 0;
  87. unsigned long currentSeconds;
  88. int prev_time_seconds = 0;
  89. bool last_time_state=false;
  90. String divi="";
  91. bool start_up=0;
  92.  
  93.  
  94. void func_write_logo(){
  95.     func_write_LCD_text(true,3,1,"Line1");
  96.     func_write_LCD_text(false,5,2,"Line2");
  97.     func_write_LCD_text(false,7,3,"Line3");
  98. }
  99.  
  100.  
  101. //FUNCTION ANALOG READ
  102. int func_analog_read(){
  103.     yield();
  104.     //====================================
  105.     bool AZ = false;
  106.     //====================================
  107.    
  108.     // GEEKCREIT Board
  109.     int min_1 = 0; //KOMMEN
  110.     int max_1 = 30;
  111.     int min_2 = 19; //HOCH
  112.     int max_2 = 60;
  113.     int min_3 = 90; //RUNTER
  114.     int max_3 = 120;
  115.     int min_4 = 150; //GEHEN
  116.     int max_4 = 200;
  117.     int min_5 = 350; // ESC
  118.     int max_5 = 400;
  119.  
  120.   //AZ Delivery Boards
  121.   /*
  122.     int min_1 = 830; //KOMMEN
  123.     int max_1 = 836;
  124.     int min_2 = 838; //HOCH
  125.     int max_2 = 845;
  126.     int min_3 = 848; //RUNTER
  127.     int max_3 = 855;
  128.     int min_4 = 860; //GEHEN
  129.     int max_4 = 870;
  130.     int min_5 = 880; // ESC
  131.     int max_5 = 900;
  132.  
  133.    
  134.     */
  135.     const int analog_ip = A0; //Naming analog input pin
  136.     int in  = 0;        //Variable to store analog input value
  137.     int out = 0;
  138.    
  139.     in = analogRead (analog_ip); // Analog Values 0 to 1023
  140.     //Serial.println(in);
  141.  
  142.     if(in!=1024){
  143.       Serial.println("Enter Analog_read: " + timeClient.getFormattedTime());
  144.       Serial.println(in);
  145.     }
  146.    
  147.     if(in >= min_1 && in<=max_1)
  148.     { //Wenn der Wert gleich 0 ist
  149.       out=1;  
  150.     }
  151.     else if(in >= min_2 && in <= max_2)
  152.     { //Wenn der Wert zwischen 19 und 20 ist
  153.       out=2;
  154.     }
  155.     else if(in >= min_3 && in <= max_3)
  156.     { //Wenn der Wert zwischen 56 und 57 ist
  157.       out=3;
  158.     }
  159.     else if(in >= min_4 && in <= max_4)
  160.     { //Wenn der Wert zwischen 108 und 109 ist
  161.       out=4;  
  162.     }
  163.     else if(in >= min_5 && in <= max_5)
  164.     { //Wenn der Wert zwischen 230 und 231 ist
  165.       out=5;
  166.     }
  167.     delay(100);
  168.  
  169.    
  170.     //Serial.println("Exit Analog_read: " + timeClient.getFormattedTime());
  171.     //Serial.println("Return Value: " + out);
  172.    
  173.     return out;
  174. }
  175.  
  176.  
  177. struct return_values func_read_rfid_display(String booking_event, String booking_process){
  178. /* https://forum.arduino.cc/index.php?topic=92011.0
  179.  *===========================================          
  180.  * booking_event :  1   kommen
  181.  *                  2   gehen
  182.  *===========================================                          
  183.  *booking_process:  1   normal
  184.  *                  2   pause
  185.  *                  3   Raucherpause
  186.  *                  4   Mittag
  187.  *                  5   Dienstgang
  188.  *===========================================                  
  189.  */
  190.         struct return_values ret_value;
  191.        
  192.         bool read_ok=false;
  193.         String content= "";
  194.         bool key_press=false;
  195.         bool w_exit=false;
  196.         int current_seconds=millis()/1000;
  197.         int prev_seconds=current_seconds;
  198.  
  199.         int pos =5;
  200.        
  201.         do{
  202.             int current_seconds=millis()/1000;
  203.             key_press=false;
  204.             if(mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
  205.               read_ok=true;
  206.              
  207.               byte letter;
  208.               for (byte i = 0; i < mfrc522.uid.size; i++)
  209.               {
  210.                  Serial.println(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
  211.                  Serial.println(mfrc522.uid.uidByte[i], HEX);
  212.                  content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? "0" : ""));
  213.                  content.concat(String(mfrc522.uid.uidByte[i], HEX));
  214.               }
  215.               content.toUpperCase();
  216.               //STRUCT KOMMT ZURÜCK Und wird in STRUCT geschrieben
  217.               struct send_response send_rgw  =func_send_data(content, terminal_id, booking_process, booking_event);
  218.               //ret_value.http_response=func_send_data(content, "zzALPHA", booking_process, booking_event);
  219.               ret_value.http_response = send_rgw.http_response;
  220.               ret_value.result = send_rgw.result;
  221.               ret_value.message = send_rgw.message;
  222.              
  223.             }
  224.             if(func_analog_read()==5){key_press=true;};
  225.             if(read_ok==true || key_press==true){
  226.               w_exit=true;
  227.             }else{
  228.               w_exit=false;
  229.             }
  230.             if(current_seconds - prev_seconds >= 1){
  231.                 func_write_LCD_text(false,pos,1,">");
  232.                 pos++;
  233.                 prev_seconds=current_seconds;
  234.             }
  235.  
  236.             func_write_LCD_text(false,pos,1,">");
  237.            
  238.             //Serial.println(w_exit);
  239.             if(pos>=14){
  240.               w_exit=true;
  241.               key_press=true;
  242.             }
  243.         }
  244.         while(w_exit!=true );
  245.         mfrc522.PICC_HaltA();
  246.         if (key_press==true){
  247.           ret_value.keypress=false;
  248.          
  249.         }else{
  250.           ret_value.keypress=true;
  251.           ret_value.card_id=content;
  252.         }
  253.         Serial.println(timeClient.getEpochTime());
  254.         //int func_send_data(String card_id, String Term_id, String booking_process, String booking_event ){
  255.         //ret_value.http_response=func_send_data(content, "zzALPHA", booking_process, booking_event);
  256.         return ret_value;
  257.        
  258. }
  259.  
  260. void func_write_LCD_menue(int menuePos){
  261.        yield();
  262.        int last, next;
  263.        
  264.        if(menuePos==0){
  265.           last=4;
  266.           next=1;
  267.        }else if( menuePos==4){
  268.           last=3;
  269.           next=0;
  270.        }else {
  271.           last=menuePos-1;
  272.           next=menuePos+1;
  273.        }
  274.  
  275.        func_write_LCD_text(true,0,0,"Abwesend:");
  276.  
  277.        func_write_LCD_text(false,3,1,booking_process_array[last][1]);
  278.        func_write_LCD_text(false,2,2,">" + booking_process_array[menuePos][1]+"<");
  279.        func_write_LCD_text(false,3,3,booking_process_array[next][1]);
  280.  
  281. }
  282.  
  283. void func_write_LCD_text(bool clear, int col, int row, String output_text){
  284.   yield();
  285.   if (clear==true){
  286.      lcd.clear();
  287.   }
  288.   lcd.setCursor(col,row);
  289.   lcd.print(output_text);
  290.  
  291. }
  292.  
  293. struct send_response func_send_data(String card_id, String Term_id, String booking_process, String booking_event ){
  294.      
  295.      //String PostData="{\"transponder_id\":1234567890,\"terminal_id\":1, \"booking_kind\":1,\"booking_process\":1,\"time_stamp\":1554304625}";
  296.     //String PostData="{\"transponder_id\":'" + card_id + "',\"terminal_id\":'" + Term_id + "', \"booking_kind\":'" + booking_event + "',\"booking_process\":'" + booking_process + "',\"time_stamp\":\"";
  297.     String PostData="{\"transponder_id\":\"" + card_id + "\",\"terminal_id\":\"" + Term_id + "\", \"booking_kind\":\"" + booking_event + "\",\"booking_process\":\"" + booking_process + "\",\"time_stamp\":\"";
  298.     int httpResponseCode;
  299.     const char* result;
  300.     StaticJsonDocument<200> doc;
  301.     Serial.println(PostData);
  302.     struct send_response return_response;
  303.     char *cUnix;
  304.    
  305.     if(WiFi.status()== WL_CONNECTED){   //Check WiFi connection status
  306.            
  307.             sprintf(cUnix,"%d",timeClient.getEpochTime());
  308.             String Data= cUnix;
  309.             String sendPostData= PostData + Data + "\"}";
  310.             Serial.println(sendPostData);
  311.             HTTPClient http;  
  312.             http.begin("https://192.168.0.1/CloudRestSrv/api/booking/create.php","16CF85DB1485479777E467B40D1C215AAB745422");  //Specify destination for HTTP request
  313.             http.addHeader("Content-Type", "text/plain");             //Specify content-type header
  314.             httpResponseCode = http.POST(sendPostData);   //Send the actual POST request
  315.             if(httpResponseCode>0){
  316.                 String response = http.getString();                       //Get the response to the reques
  317.                 Serial.println(httpResponseCode);   //Print return code
  318.                 Serial.println(response);           //Print request answer
  319.                 char JSON[response.length()];
  320.                 response.toCharArray(JSON,response.length());
  321.  
  322.                 // Deserialize the JSON document
  323.                 DeserializationError error = deserializeJson(doc, JSON);
  324.              
  325.                 // Test if parsing succeeds.
  326.                 if (error) {
  327.                   Serial.print(F("deserializeJson() failed: "));
  328.                   Serial.println(error.c_str());
  329.                 }
  330.  
  331.                 String result=doc["result"];
  332.                 String message = doc["message"];
  333.                 return_response.result=result;
  334.                 return_response.http_response=httpResponseCode;
  335.                 return_response.message=message;
  336.                 Serial.println(result);  
  337.                
  338.             }else{
  339.                 Serial.print("Error on sending POST: ");
  340.                 Serial.println(httpResponseCode);
  341.             }
  342.             http.end();  //Free resources
  343.              
  344.        }else{
  345.        
  346.           Serial.println("Error in WiFi connection");  
  347.        
  348.        }
  349.        Serial.println(httpResponseCode);
  350.        return return_response;
  351. }
  352.  
  353. void setup()
  354. {
  355.     start_up=1;
  356.     Wire.begin(5,4);
  357.     Serial.begin(115200);   // Initiate a serial communication
  358.     SPI.begin();      // Initiate  SPI bus
  359.     mfrc522.PCD_Init();   // Initiate MFRC522
  360.     // Initialize display
  361.     lcd.init();
  362.     lcd.backlight();
  363.     lcd.setCursor(2,1);
  364.     lcd.print("Wait for WiFi");
  365.     int Count=0;
  366.    
  367.     do
  368.     {
  369.         lcd.setCursor(15+Count,1);
  370.         lcd.print(".");
  371.         Count++;
  372.         delay(1000);
  373.     }
  374.     while(Count!=4);
  375.    
  376.     WiFi.begin(ssid, password);
  377.    
  378.     while (WiFi.status() != WL_CONNECTED)
  379.     //while (WiFi.waitForConnectResult() != WL_CONNECTED)
  380.     //while (WiFi.localIP().toString() == "0.0.0.0")
  381.     { //Check for the connection
  382.         func_write_LCD_text(true,1,1,"Connecting to WiFi");
  383.         delay(1000);    
  384.         Serial.println(WiFi.status());
  385.     }  
  386.     func_write_LCD_text(true,2,1,"Connected to the");
  387.     func_write_LCD_text(false,4,2,"WiFi network");
  388.  
  389.     delay(700);
  390.  
  391.     func_write_LCD_text(true,2,1,"IP Adresse:");
  392.     func_write_LCD_text(false,2,2,WiFi.localIP().toString());
  393.  
  394.    
  395.    
  396.     timeClient.begin();
  397.     Serial.println("Time Update: " + timeClient.update());
  398.  
  399.     delay(100);
  400.    
  401.     func_write_LCD_text(true,3,1,"line1");
  402.     func_write_LCD_text(false,5,2,"line2");
  403.     func_write_LCD_text(false,7,3,"line3");
  404.     //delay(1000);
  405.  
  406.     int booking_event_id=0;
  407.    
  408. } //ENDE SETUP
  409.  
  410.  
  411. void loop(){  
  412.   yield();
  413.   if(inMenueStruc==true && changeMenue==true){
  414.      
  415.      func_write_LCD_menue(menuePos);
  416.      //func_write_LCD_text(true,4,1,booking_process_array[menuePos][1]);
  417.      changeMenue=false;
  418.   }
  419.   //Serial.println(menuePos);
  420.   currentSeconds = millis()/1000;
  421.   if(currentSeconds - previousSeconds >= refresh_time_interval || start_up==1){
  422.       Serial.println("Enter UpdateTime: " + timeClient.getFormattedTime());
  423.       previousSeconds = currentSeconds;
  424.       timeClient.update();
  425.      
  426.       if(start_up==1){
  427.         //timeClient.close();
  428.         delay(500);
  429.         timeClient.begin();
  430.         delay(500);
  431.         timeClient.forceUpdate();
  432.         start_up=0;
  433.       }
  434.      
  435.       Serial.println(timeClient.getEpochTime());
  436.       Serial.println("Exit UpdateTime: " + timeClient.getFormattedTime());
  437.   }
  438.  
  439.   if(currentSeconds - menue_seconds >= 6 && inMenueStruc==true){
  440.      Serial.println("Write Logo im Loop im Menü");
  441.      func_write_logo();
  442.      inMenueStruc=false;
  443.      menuePos=0;
  444.   }
  445. /*
  446.  * TODO checken ob Uhrzeit sich geändert hat, erst dann schreiben
  447.  */
  448.   if(currentSeconds - prev_time_seconds >=1 ){
  449.     prev_time_seconds=currentSeconds;
  450.     if(last_time_state==false){
  451.       divi=" ";
  452.       last_time_state=true;
  453.     }else{
  454.       divi=":";
  455.       last_time_state=false;
  456.     }
  457.    
  458.    
  459.     String nTime=timeClient.getFormattedTime();
  460.     String nTime2=timeClient.getFormattedTime();
  461.     //Serial.println(timeClient.getEpochTime());
  462.     //Serial.println(nTime);
  463.     //Serial.println(WiFi.localIP());
  464.    
  465.     func_write_LCD_text(false,12,0,nTime.substring(0,2)+ divi + nTime2.substring(3,5)+"Uhr");
  466.    
  467.    
  468.    
  469.   }
  470.   //String nTime=timeClient.getFormattedTime();
  471.   int UnixTime=timeClient.getEpochTime();
  472.  
  473.  
  474.  // func_write_LCD_text(false,12,0,nTime.substring(0,5)+"Uhr");
  475.  
  476.   if(button_flag==false){
  477.     RetVal=func_analog_read();
  478.     //Serial.println(RetVal);
  479.     if(RetVal!=0){
  480.       button=RetVal;
  481.       //button_flag=true;
  482.         if(RetVal==1){
  483.           //Serial.println(booking_process_array[menuePos][1]);
  484.           func_write_LCD_text(true,1,0,booking_process_array[menuePos][1]+ " " + booking_event_array[1][1]);
  485.           //func_write_LCD_text(false,6,1,"KOMMEN");
  486.           func_write_LCD_text(false,4,2,"Transponder");
  487.           func_write_LCD_text(false,5,3,"vorhalten");
  488.           button_flag=true;
  489.  
  490.         }else if(RetVal==4){
  491.           func_write_LCD_text(true,2,0,booking_process_array[menuePos][1]+ " " + booking_event_array[2][1]);
  492.           //func_write_LCD_text(false,6,1,"KOMMEN");
  493.           func_write_LCD_text(false,4,2,"Transponder");
  494.           func_write_LCD_text(false,5,3,"vorhalten");
  495.           button_flag=true;
  496.  
  497.         }else if(RetVal==2){
  498.           //Menüstruktur hoch
  499.           if(menuePos==0){
  500.             menuePos=4;
  501.           }else{
  502.             menuePos--;
  503.           }
  504.           inMenueStruc=true;
  505.           changeMenue=true;
  506.           menue_seconds=millis()/1000;
  507.           delay(50);
  508.         }else if(RetVal==3){
  509.           //menüstruktur runter
  510.           if(menuePos==4){
  511.             menuePos=0;
  512.           }else{
  513.             menuePos++;
  514.           }
  515.           inMenueStruc=true;
  516.           changeMenue=true;
  517.           menue_seconds=millis()/1000;
  518.           delay(50);
  519.         }else if(RetVal==5){
  520.           //menüstruktur ESC
  521.           menuePos=0;
  522.           inMenueStruc=false;
  523.           func_write_logo();
  524.         }
  525.     }else
  526.     {
  527.       //Leer
  528.     }
  529.   }
  530.   if(RetVal==1){
  531.         int bookingEvent=1;
  532.        
  533.         struct return_values return_value=func_read_rfid_display(booking_event_array[bookingEvent][3], booking_process_array[menuePos][2]);
  534.        
  535.         if(return_value.keypress==true && return_value.http_response==200 && return_value.result=="OK"    )   {
  536.           func_write_LCD_text(true,3,0,"ERFOLGREICH:");
  537.           func_write_LCD_text(false,3,1,booking_event_array[bookingEvent][2]+ " und");
  538.           func_write_LCD_text(false,3,2,"uebertragen");
  539.           func_write_LCD_text(false, 3,3, return_value.card_id);
  540.         }else if (return_value.keypress==false){
  541.           func_write_LCD_text(true,5,1," ABBRUCH ");
  542.         }else {
  543.           func_write_LCD_text(true,1,0,"FEHLER:");
  544.           func_write_LCD_text(false,1,1,"Uebertragungsfehler");
  545.           func_write_LCD_text(false,1,2,"CAPTAIN APRO");
  546.           func_write_LCD_text(false, 1,3, "fragen");
  547.           delay(2000);
  548.         }
  549.         delay(1000);
  550.         func_write_logo();
  551.         button_flag=false;
  552.         inMenueStruc=false;
  553.         menuePos=0;
  554.        
  555.     }else if(RetVal==4){
  556.        
  557.         int bookingEvent=2;
  558.         struct return_values return_value=func_read_rfid_display(booking_event_array[bookingEvent][3], booking_process_array[menuePos][2]);
  559.        
  560.        if(return_value.keypress==true && return_value.http_response==200){
  561.           func_write_LCD_text(true,3,0,"ERFOLGREICH:");
  562.           func_write_LCD_text(false,3,1,booking_event_array[bookingEvent][2]+ " und");
  563.           func_write_LCD_text(false,3,2,"uebertragen");
  564.           func_write_LCD_text(false, 3,3, return_value.card_id);
  565.         }else if (return_value.keypress==false){
  566.           func_write_LCD_text(true,5,1," ABBRUCH ");
  567.         }else {
  568.           func_write_LCD_text(true,1,0,"FEHLER:");
  569.           func_write_LCD_text(false,1,1,"Uebertragungsfehler");
  570.           func_write_LCD_text(false,1,2,"CAPTAIN IT");
  571.           func_write_LCD_text(false, 1,3, "fragen");
  572.           delay(2000);
  573.         }
  574.         delay(1000);
  575.         func_write_logo();
  576.         button_flag=false;
  577.         inMenueStruc=false;
  578.         menuePos=0;
  579.     }
  580. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement