Jack Hansdampf / Mbed OS mqtt_Autofaehrschiff

Dependencies:   LCD_i2c_GSOE ESP8266MQTT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "PubSubClient.h"   //MQTT- Bibliothek
00003 #include "LCD.h"            //LCD-Display-Bibliothek
00004 
00005 #define A 0b00001
00006 #define B 0b00010
00007 #define C 0b00100
00008 #define D 0b01000
00009 #define Voll 0b00000
00010 
00011 PortOut zustand(PortC,0x1F);
00012 
00013 
00014 PubSubClient client;        //Deklaration des MQTT-Clients
00015 InterruptIn Lichtschranke(PA_1);
00016 char buffer[64];
00017 int anzahl;
00018 bool einfahrt=false;
00019 
00020 void lichtschranke()
00021 {
00022     if (einfahrt==false)
00023     {
00024         switch (zustand)
00025         {
00026             case A: anzahl++; if (anzahl==22) zustand=B; break; 
00027             case B: anzahl++; if (anzahl==44) zustand=C; break;
00028             case C: anzahl++; if (anzahl==66) zustand=D; break;
00029             case D: anzahl++; if (anzahl==88) zustand=Voll; break;           
00030         }
00031         einfahrt=true;
00032     }
00033 }
00034 
00035 //Subscribe-Callbacks
00036 void callback(MessageData& mymessage)
00037 {
00038         zustand=A;
00039         anzahl=0;
00040         sprintf(buffer, "Fahrzeuge=%d", anzahl); //Potiwert in buffer speichern
00041         client.publish("MBED/oehringen/faehrschiff", buffer); //unter Topic veröffentlichen 
00042 }
00043 
00044 void mqtt()
00045 {
00046     client.loop();  //Auf neue Botschaften prüfen
00047     if (einfahrt)
00048     {
00049         sprintf(buffer, "Fahrzeuge=%d", anzahl); //Potiwert in buffer speichern
00050         client.publish("MBED/oehringen/faehrschiff", buffer); //unter Topic veröffentlichen  
00051         HAL_Delay(200); 
00052         einfahrt=false;
00053     }
00054 }
00055 
00056 void init()
00057 {
00058     client.connect((char*)"joerg"); 
00059     client.subscribe("MBED/oehringen/faehrschiffLeer", QOS0, callback); 
00060     Lichtschranke.fall(&lichtschranke);
00061     Lichtschranke.mode(PullDown);
00062     zustand=A;
00063     anzahl=0;    
00064 }
00065 
00066 int main()
00067 {
00068     init();
00069     while(true)
00070     {
00071         mqtt();
00072     }
00073 }
00074 
00075 
00076 
00077