Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- *
- * BOARD ESP8266 2.4.2
- */
- #define SS_PIN D4
- #define RST_PIN D3
- #define SDA_PIN D2
- #define SCL_PIN D1
- #include <ESP8266WiFi.h>
- #include "ESP8266HTTPClient.h" //Lokal im Ordner ist wichtig
- #include <ArduinoJson.h>
- #include <Wire.h>
- #include <SPI.h>
- #include <MFRC522.h>
- #include <LiquidCrystal_I2C.h>
- #include <NTPClient.h>
- #include <WifiUdp.h>
- #define NTP_OFFSET 7200 // 60 * 60 In seconds
- #define NTP_INTERVAL 60 * 1000 // In miliseconds
- #define NTP_ADDRESS "0.de.pool.ntp.org"
- WiFiUDP ntpUDP;
- NTPClient timeClient(ntpUDP, NTP_ADDRESS, NTP_OFFSET, NTP_INTERVAL);
- const char* ssid = "Guest";
- const char* password = "12345678";
- String terminal_id="ZZALPHA";
- String booking_process_array[5][3]={
- {"0","Arbeitszeit","1"},
- {"1","Dienstgang","2"},
- {"2","Mittagspause","3"},
- {"3","Pause","4"},
- {"4","Raucherpause","5"}
- };
- String booking_event_array[3][4]={
- {"0","system","system","1"},
- {"1","Beginn","Eingebucht","6"},
- {"2","Ende","Ausgebucht","9"}
- };
- struct return_values {
- bool keypress;
- String card_id;
- int http_response;
- String message;
- String result;
- };
- struct send_response {
- bool succes;
- String result;
- String message;
- int http_response;
- };
- LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
- MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
- int statuss = 0;
- int out = 0;
- bool inMenueStruc=false;
- int menuePos =0;
- bool changeMenue=false;
- int menue_seconds=0;
- int RetVal=0;
- bool button_flag=false;
- String button ="";
- const long refresh_time_interval = 9900; //885
- unsigned long previousSeconds = 0;
- unsigned long currentSeconds;
- int prev_time_seconds = 0;
- bool last_time_state=false;
- String divi="";
- bool start_up=0;
- void func_write_logo(){
- func_write_LCD_text(true,3,1,"Line1");
- func_write_LCD_text(false,5,2,"Line2");
- func_write_LCD_text(false,7,3,"Line3");
- }
- //FUNCTION ANALOG READ
- int func_analog_read(){
- yield();
- //====================================
- bool AZ = false;
- //====================================
- // GEEKCREIT Board
- int min_1 = 0; //KOMMEN
- int max_1 = 30;
- int min_2 = 19; //HOCH
- int max_2 = 60;
- int min_3 = 90; //RUNTER
- int max_3 = 120;
- int min_4 = 150; //GEHEN
- int max_4 = 200;
- int min_5 = 350; // ESC
- int max_5 = 400;
- //AZ Delivery Boards
- /*
- int min_1 = 830; //KOMMEN
- int max_1 = 836;
- int min_2 = 838; //HOCH
- int max_2 = 845;
- int min_3 = 848; //RUNTER
- int max_3 = 855;
- int min_4 = 860; //GEHEN
- int max_4 = 870;
- int min_5 = 880; // ESC
- int max_5 = 900;
- */
- const int analog_ip = A0; //Naming analog input pin
- int in = 0; //Variable to store analog input value
- int out = 0;
- in = analogRead (analog_ip); // Analog Values 0 to 1023
- //Serial.println(in);
- if(in!=1024){
- Serial.println("Enter Analog_read: " + timeClient.getFormattedTime());
- Serial.println(in);
- }
- if(in >= min_1 && in<=max_1)
- { //Wenn der Wert gleich 0 ist
- out=1;
- }
- else if(in >= min_2 && in <= max_2)
- { //Wenn der Wert zwischen 19 und 20 ist
- out=2;
- }
- else if(in >= min_3 && in <= max_3)
- { //Wenn der Wert zwischen 56 und 57 ist
- out=3;
- }
- else if(in >= min_4 && in <= max_4)
- { //Wenn der Wert zwischen 108 und 109 ist
- out=4;
- }
- else if(in >= min_5 && in <= max_5)
- { //Wenn der Wert zwischen 230 und 231 ist
- out=5;
- }
- delay(100);
- //Serial.println("Exit Analog_read: " + timeClient.getFormattedTime());
- //Serial.println("Return Value: " + out);
- return out;
- }
- struct return_values func_read_rfid_display(String booking_event, String booking_process){
- /* https://forum.arduino.cc/index.php?topic=92011.0
- *===========================================
- * booking_event : 1 kommen
- * 2 gehen
- *===========================================
- *booking_process: 1 normal
- * 2 pause
- * 3 Raucherpause
- * 4 Mittag
- * 5 Dienstgang
- *===========================================
- */
- struct return_values ret_value;
- bool read_ok=false;
- String content= "";
- bool key_press=false;
- bool w_exit=false;
- int current_seconds=millis()/1000;
- int prev_seconds=current_seconds;
- int pos =5;
- do{
- int current_seconds=millis()/1000;
- key_press=false;
- if(mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
- read_ok=true;
- byte letter;
- for (byte i = 0; i < mfrc522.uid.size; i++)
- {
- Serial.println(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
- Serial.println(mfrc522.uid.uidByte[i], HEX);
- content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? "0" : ""));
- content.concat(String(mfrc522.uid.uidByte[i], HEX));
- }
- content.toUpperCase();
- //STRUCT KOMMT ZURÜCK Und wird in STRUCT geschrieben
- struct send_response send_rgw =func_send_data(content, terminal_id, booking_process, booking_event);
- //ret_value.http_response=func_send_data(content, "zzALPHA", booking_process, booking_event);
- ret_value.http_response = send_rgw.http_response;
- ret_value.result = send_rgw.result;
- ret_value.message = send_rgw.message;
- }
- if(func_analog_read()==5){key_press=true;};
- if(read_ok==true || key_press==true){
- w_exit=true;
- }else{
- w_exit=false;
- }
- if(current_seconds - prev_seconds >= 1){
- func_write_LCD_text(false,pos,1,">");
- pos++;
- prev_seconds=current_seconds;
- }
- func_write_LCD_text(false,pos,1,">");
- //Serial.println(w_exit);
- if(pos>=14){
- w_exit=true;
- key_press=true;
- }
- }
- while(w_exit!=true );
- mfrc522.PICC_HaltA();
- if (key_press==true){
- ret_value.keypress=false;
- }else{
- ret_value.keypress=true;
- ret_value.card_id=content;
- }
- Serial.println(timeClient.getEpochTime());
- //int func_send_data(String card_id, String Term_id, String booking_process, String booking_event ){
- //ret_value.http_response=func_send_data(content, "zzALPHA", booking_process, booking_event);
- return ret_value;
- }
- void func_write_LCD_menue(int menuePos){
- yield();
- int last, next;
- if(menuePos==0){
- last=4;
- next=1;
- }else if( menuePos==4){
- last=3;
- next=0;
- }else {
- last=menuePos-1;
- next=menuePos+1;
- }
- func_write_LCD_text(true,0,0,"Abwesend:");
- func_write_LCD_text(false,3,1,booking_process_array[last][1]);
- func_write_LCD_text(false,2,2,">" + booking_process_array[menuePos][1]+"<");
- func_write_LCD_text(false,3,3,booking_process_array[next][1]);
- }
- void func_write_LCD_text(bool clear, int col, int row, String output_text){
- yield();
- if (clear==true){
- lcd.clear();
- }
- lcd.setCursor(col,row);
- lcd.print(output_text);
- }
- struct send_response func_send_data(String card_id, String Term_id, String booking_process, String booking_event ){
- //String PostData="{\"transponder_id\":1234567890,\"terminal_id\":1, \"booking_kind\":1,\"booking_process\":1,\"time_stamp\":1554304625}";
- //String PostData="{\"transponder_id\":'" + card_id + "',\"terminal_id\":'" + Term_id + "', \"booking_kind\":'" + booking_event + "',\"booking_process\":'" + booking_process + "',\"time_stamp\":\"";
- String PostData="{\"transponder_id\":\"" + card_id + "\",\"terminal_id\":\"" + Term_id + "\", \"booking_kind\":\"" + booking_event + "\",\"booking_process\":\"" + booking_process + "\",\"time_stamp\":\"";
- int httpResponseCode;
- const char* result;
- StaticJsonDocument<200> doc;
- Serial.println(PostData);
- struct send_response return_response;
- char *cUnix;
- if(WiFi.status()== WL_CONNECTED){ //Check WiFi connection status
- sprintf(cUnix,"%d",timeClient.getEpochTime());
- String Data= cUnix;
- String sendPostData= PostData + Data + "\"}";
- Serial.println(sendPostData);
- HTTPClient http;
- http.begin("https://192.168.0.1/CloudRestSrv/api/booking/create.php","16CF85DB1485479777E467B40D1C215AAB745422"); //Specify destination for HTTP request
- http.addHeader("Content-Type", "text/plain"); //Specify content-type header
- httpResponseCode = http.POST(sendPostData); //Send the actual POST request
- if(httpResponseCode>0){
- String response = http.getString(); //Get the response to the reques
- Serial.println(httpResponseCode); //Print return code
- Serial.println(response); //Print request answer
- char JSON[response.length()];
- response.toCharArray(JSON,response.length());
- // Deserialize the JSON document
- DeserializationError error = deserializeJson(doc, JSON);
- // Test if parsing succeeds.
- if (error) {
- Serial.print(F("deserializeJson() failed: "));
- Serial.println(error.c_str());
- }
- String result=doc["result"];
- String message = doc["message"];
- return_response.result=result;
- return_response.http_response=httpResponseCode;
- return_response.message=message;
- Serial.println(result);
- }else{
- Serial.print("Error on sending POST: ");
- Serial.println(httpResponseCode);
- }
- http.end(); //Free resources
- }else{
- Serial.println("Error in WiFi connection");
- }
- Serial.println(httpResponseCode);
- return return_response;
- }
- void setup()
- {
- start_up=1;
- Wire.begin(5,4);
- Serial.begin(115200); // Initiate a serial communication
- SPI.begin(); // Initiate SPI bus
- mfrc522.PCD_Init(); // Initiate MFRC522
- // Initialize display
- lcd.init();
- lcd.backlight();
- lcd.setCursor(2,1);
- lcd.print("Wait for WiFi");
- int Count=0;
- do
- {
- lcd.setCursor(15+Count,1);
- lcd.print(".");
- Count++;
- delay(1000);
- }
- while(Count!=4);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED)
- //while (WiFi.waitForConnectResult() != WL_CONNECTED)
- //while (WiFi.localIP().toString() == "0.0.0.0")
- { //Check for the connection
- func_write_LCD_text(true,1,1,"Connecting to WiFi");
- delay(1000);
- Serial.println(WiFi.status());
- }
- func_write_LCD_text(true,2,1,"Connected to the");
- func_write_LCD_text(false,4,2,"WiFi network");
- delay(700);
- func_write_LCD_text(true,2,1,"IP Adresse:");
- func_write_LCD_text(false,2,2,WiFi.localIP().toString());
- timeClient.begin();
- Serial.println("Time Update: " + timeClient.update());
- delay(100);
- func_write_LCD_text(true,3,1,"line1");
- func_write_LCD_text(false,5,2,"line2");
- func_write_LCD_text(false,7,3,"line3");
- //delay(1000);
- int booking_event_id=0;
- } //ENDE SETUP
- void loop(){
- yield();
- if(inMenueStruc==true && changeMenue==true){
- func_write_LCD_menue(menuePos);
- //func_write_LCD_text(true,4,1,booking_process_array[menuePos][1]);
- changeMenue=false;
- }
- //Serial.println(menuePos);
- currentSeconds = millis()/1000;
- if(currentSeconds - previousSeconds >= refresh_time_interval || start_up==1){
- Serial.println("Enter UpdateTime: " + timeClient.getFormattedTime());
- previousSeconds = currentSeconds;
- timeClient.update();
- if(start_up==1){
- //timeClient.close();
- delay(500);
- timeClient.begin();
- delay(500);
- timeClient.forceUpdate();
- start_up=0;
- }
- Serial.println(timeClient.getEpochTime());
- Serial.println("Exit UpdateTime: " + timeClient.getFormattedTime());
- }
- if(currentSeconds - menue_seconds >= 6 && inMenueStruc==true){
- Serial.println("Write Logo im Loop im Menü");
- func_write_logo();
- inMenueStruc=false;
- menuePos=0;
- }
- /*
- * TODO checken ob Uhrzeit sich geändert hat, erst dann schreiben
- */
- if(currentSeconds - prev_time_seconds >=1 ){
- prev_time_seconds=currentSeconds;
- if(last_time_state==false){
- divi=" ";
- last_time_state=true;
- }else{
- divi=":";
- last_time_state=false;
- }
- String nTime=timeClient.getFormattedTime();
- String nTime2=timeClient.getFormattedTime();
- //Serial.println(timeClient.getEpochTime());
- //Serial.println(nTime);
- //Serial.println(WiFi.localIP());
- func_write_LCD_text(false,12,0,nTime.substring(0,2)+ divi + nTime2.substring(3,5)+"Uhr");
- }
- //String nTime=timeClient.getFormattedTime();
- int UnixTime=timeClient.getEpochTime();
- // func_write_LCD_text(false,12,0,nTime.substring(0,5)+"Uhr");
- if(button_flag==false){
- RetVal=func_analog_read();
- //Serial.println(RetVal);
- if(RetVal!=0){
- button=RetVal;
- //button_flag=true;
- if(RetVal==1){
- //Serial.println(booking_process_array[menuePos][1]);
- func_write_LCD_text(true,1,0,booking_process_array[menuePos][1]+ " " + booking_event_array[1][1]);
- //func_write_LCD_text(false,6,1,"KOMMEN");
- func_write_LCD_text(false,4,2,"Transponder");
- func_write_LCD_text(false,5,3,"vorhalten");
- button_flag=true;
- }else if(RetVal==4){
- func_write_LCD_text(true,2,0,booking_process_array[menuePos][1]+ " " + booking_event_array[2][1]);
- //func_write_LCD_text(false,6,1,"KOMMEN");
- func_write_LCD_text(false,4,2,"Transponder");
- func_write_LCD_text(false,5,3,"vorhalten");
- button_flag=true;
- }else if(RetVal==2){
- //Menüstruktur hoch
- if(menuePos==0){
- menuePos=4;
- }else{
- menuePos--;
- }
- inMenueStruc=true;
- changeMenue=true;
- menue_seconds=millis()/1000;
- delay(50);
- }else if(RetVal==3){
- //menüstruktur runter
- if(menuePos==4){
- menuePos=0;
- }else{
- menuePos++;
- }
- inMenueStruc=true;
- changeMenue=true;
- menue_seconds=millis()/1000;
- delay(50);
- }else if(RetVal==5){
- //menüstruktur ESC
- menuePos=0;
- inMenueStruc=false;
- func_write_logo();
- }
- }else
- {
- //Leer
- }
- }
- if(RetVal==1){
- int bookingEvent=1;
- struct return_values return_value=func_read_rfid_display(booking_event_array[bookingEvent][3], booking_process_array[menuePos][2]);
- if(return_value.keypress==true && return_value.http_response==200 && return_value.result=="OK" ) {
- func_write_LCD_text(true,3,0,"ERFOLGREICH:");
- func_write_LCD_text(false,3,1,booking_event_array[bookingEvent][2]+ " und");
- func_write_LCD_text(false,3,2,"uebertragen");
- func_write_LCD_text(false, 3,3, return_value.card_id);
- }else if (return_value.keypress==false){
- func_write_LCD_text(true,5,1," ABBRUCH ");
- }else {
- func_write_LCD_text(true,1,0,"FEHLER:");
- func_write_LCD_text(false,1,1,"Uebertragungsfehler");
- func_write_LCD_text(false,1,2,"CAPTAIN APRO");
- func_write_LCD_text(false, 1,3, "fragen");
- delay(2000);
- }
- delay(1000);
- func_write_logo();
- button_flag=false;
- inMenueStruc=false;
- menuePos=0;
- }else if(RetVal==4){
- int bookingEvent=2;
- struct return_values return_value=func_read_rfid_display(booking_event_array[bookingEvent][3], booking_process_array[menuePos][2]);
- if(return_value.keypress==true && return_value.http_response==200){
- func_write_LCD_text(true,3,0,"ERFOLGREICH:");
- func_write_LCD_text(false,3,1,booking_event_array[bookingEvent][2]+ " und");
- func_write_LCD_text(false,3,2,"uebertragen");
- func_write_LCD_text(false, 3,3, return_value.card_id);
- }else if (return_value.keypress==false){
- func_write_LCD_text(true,5,1," ABBRUCH ");
- }else {
- func_write_LCD_text(true,1,0,"FEHLER:");
- func_write_LCD_text(false,1,1,"Uebertragungsfehler");
- func_write_LCD_text(false,1,2,"CAPTAIN IT");
- func_write_LCD_text(false, 1,3, "fragen");
- delay(2000);
- }
- delay(1000);
- func_write_logo();
- button_flag=false;
- inMenueStruc=false;
- menuePos=0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement