![](/media/cache/group/ep_LV4vK4D.webp.50x50_q85.jpg)
Testprogramm fuer den SMD IoTKit Shield.
Dependencies: MFRC522 RemoteIR Servo StepperMotorUni mbed-rtos mbed ESP8266 RCSwitch SAA1064 TMP175
Diff: main.cpp
- Revision:
- 1:be86d02cd130
- Parent:
- 0:1b22732d0d8d
- Child:
- 2:e263949513ca
diff -r 1b22732d0d8d -r be86d02cd130 main.cpp --- a/main.cpp Mon Mar 23 08:58:42 2015 +0000 +++ b/main.cpp Mon Mar 23 10:05:28 2015 +0000 @@ -7,6 +7,9 @@ #include "Motor.h" #include "StepperMotorUni.h" #include "MFRC522.h" +#include <string> +#include "ESP8266.h" +#include "RCSwitch.h" // Trace #include "trace.h" @@ -23,6 +26,8 @@ { if ( test > 0 ) test--; + if ( test == 0 ) + test = 15; } /*------------------------------------------------------------------------ @@ -158,6 +163,316 @@ mosfet = 0; } +/** ESP8266 Modem an Serieller Schnittstelle */ +void doESP8266() +{ + INFO("ESP8266 Test"); + ESP8266 esp( PTC15, PTC14 ); // tx, rx (Bluetooth Header) + char snd[255], rcv[1000]; // Strings for sending and receiving commands / data / replies + wait(1); + + printf("Reset ESP\n"); + esp.Reset(); + esp.RcvReply(rcv, 400); + printf("%s", rcv); + wait(2); + + printf("Sending AT\n"); + strcpy(snd, "AT"); + esp.SendCMD(snd); + esp.RcvReply(rcv, 400); + printf("%s", rcv); + wait(2); + printf("Set mode to Client\n"); + esp.SetMode(1); + esp.RcvReply(rcv, 1000); + printf("%s", rcv); + + printf("Receiving Wifi List\n"); + esp.GetList(rcv); + printf("%s", rcv); + + printf("Connecting to AP\n"); + esp.Join("mcbmobile_1EX", "android%123" ); // Replace MyAP and MyPasswd with your SSID and password + esp.RcvReply(rcv, 1000); + printf("%s", rcv); + wait(8); + printf("Getting IP\n"); + esp.GetIP(rcv); + printf("%s", rcv); + printf("Setting multiple connections\r\n"); + esp.SetMultiple(); + esp.RcvReply(rcv, 1000); + printf("%s", rcv); + + printf("THE END\n"); +} + +/** RFID Reader MFRC522 Test */ +void doRFIDReader() +{ + INFO("RFID Reader MFRC522 Test"); + // NFC/RFID Reader an WiFi Header von K64F + MFRC522 rfidReader( PTD6, PTD7, PTD5, PTD4, PTC12 ); + rfidReader.PCD_Init(); + + // RFID Reader + if ( rfidReader.PICC_IsNewCardPresent()) + if ( rfidReader.PICC_ReadCardSerial()) + { + // Print Card UID + printf("Card UID: "); + for (uint8_t i = 0; i < rfidReader.uid.size; i++) + printf("%02X:", rfidReader.uid.uidByte[i]); + printf("\n"); + // Print Card type + uint8_t piccType = rfidReader.PICC_GetType(rfidReader.uid.sak); + printf("PICC Type: %s \n", rfidReader.PICC_GetTypeName(piccType)); + } +} + + /** 3 x 3 Werte */ +unsigned int strip[9]; + +void writeLED( SPI &spi ) +{ + for ( int p = 0; p < 9; p++ ) + spi.write( strip[p] ); +} + +void clearLED( SPI &spi ) +{ + for ( int p = 0; p < 9; p++ ) + { + strip[p] = 0; + spi.write( strip[p] ); + } +} + +/** LED Strip am SPI Bus */ +void doSPILedStrip() +{ + INFO( "LED Strip Test" ); + + SPI spi(D11, NC, D13); // mosi, miso, sclk + spi.format( 8,0 ); + spi.frequency( 800000 ); + + // Gruen, Rot, Blau - von Dunkel bis Hell + for ( int i = 0; i < 128; i+=32 ) + { + // LED 1 + strip[0] = i; + strip[1] = 0; + strip[2] = 0; + // LED 2 + strip[3] = 0; + strip[4] = i; + strip[5] = 0; + // LED 3 + strip[6] = 0; + strip[7] = 0; + strip[8] = i; + writeLED( spi ); + wait( 0.1 ); + } + wait( 1.0 ); + clearLED( spi ); +} + +void LEDStripOff( PwmOut& red, PwmOut& green, PwmOut& blue ) +{ + printf( "off \n" ); + red = 0; + green = 0; + blue = 0; + wait ( 0.2 ); +} + +void LEDStripDim( PwmOut& pin ) +{ + printf( "dim\n" ); + for ( float i = 0.0f; i < 1.0f; i += .01f ) + { + pin = i; + wait( 0.02 ); + } +} + +/** LED Strip 12Volt Test */ +void doLEDStrip() +{ + INFO( "LED Strip V12 Test" ); + PwmOut green( D11 ); + PwmOut red( D12 ); + PwmOut blue( D13 ); + + LEDStripDim( red ); + LEDStripOff( red, green, blue ); + LEDStripDim( green ); + LEDStripOff( red, green, blue ); + LEDStripDim( blue ); + LEDStripOff( red, green, blue ); + + red = 1; + wait( 0.2 ); + LEDStripOff( red, green, blue ); + + green = 1; + wait( 0.2 ); + LEDStripOff( red, green, blue ); + + blue = 1; + wait( 0.2 ); + LEDStripOff( red, green, blue ); + + red = 1; + blue = 1; + green = 1; + wait( 0.2 ); + LEDStripOff( red, green, blue ); +} + +/** Funksteckdose ein/aus */ +void doRC() +{ + INFO( "RCSwitch Test" ); + RCSwitch mySwitch = RCSwitch( D9, D8 ); // Sender / Empfaenger (optional) + + printf( "Sende nach: Adresse 1 - 5 alle On, Geraet B aus A-D " ); + // Adresse 1 - 5 alle On, Geraet B aus A-D + mySwitch.switchOff("11111", "01000"); + wait( 0.5 ); + + printf( "- ON\n" ); + mySwitch.switchOn ("11111", "01000"); +} + +/*------------------------------------------------------------------------ + * Infrarot Funktionen + ------------------------------------------------------------------------*/ + +/** Receive. + * @param format Pointer to a format. + * @param buf Pointer to a buffer. + * @param bufsiz Size of the buffer. + * @return Bit length of the received data. + */ +int receiveIR(ReceiverIR &ir_rx, RemoteIR::Format *format, uint8_t *buf, int bufsiz, int timeout = 100) +{ + int cnt = 0; + while (ir_rx.getState() != ReceiverIR::Received) + { + cnt++; + if (timeout < cnt) + return -1; + } + return ir_rx.getData(format, buf, bufsiz * 8); +} + +/** + * Display a format of a data. + */ +void displayIRFormat(RemoteIR::Format format) +{ + switch (format) + { + case RemoteIR::UNKNOWN: + printf("????????\n"); + break; + case RemoteIR::NEC: + printf("NEC \n"); + break; + case RemoteIR::NEC_REPEAT: + printf("NEC (R)\n"); + break; + case RemoteIR::AEHA: + printf("AEHA \n"); + break; + case RemoteIR::AEHA_REPEAT: + printf("AEHA (R)\n"); + break; + case RemoteIR::SONY: + printf("SONY \n"); + break; + } +} + +/** Display a data. + * @param buf Pointer to a buffer. + * @param bitlength Bit length of a data. + */ +void displayIRData(uint8_t *buf, int bitlength) +{ + const int n = bitlength / 8 + (((bitlength % 8) != 0) ? 1 : 0); + + for (int i = 0; i < n; i++) + printf("%02X", buf[i]); + for (int i = 0; i < 8 - n; i++) + printf("--"); +} + +/** IR Empfaenger abfragen */ +void onReceiveIR() +{ + INFO( "IR Receive Test" ); + ReceiverIR ir_rx( PTB20 ); + + uint8_t buf1[32]; + uint8_t buf2[32]; + int bitlength1; + RemoteIR::Format format; + + memset(buf1, 0x00, sizeof(buf1)); + memset(buf2, 0x00, sizeof(buf2)); + + bitlength1 = receiveIR( ir_rx, &format, buf1, sizeof(buf1)); + if (bitlength1 < 0) + return; + + displayIRData(buf1, bitlength1); + displayIRFormat(format); +} + +/** Buttons auf Shield */ +void doButtons() +{ + INFO ( "Buttons Test" ); + DigitalIn button3( PTC0, PullUp ); + DigitalIn button4( PTC7, PullUp ); + DigitalIn button5( PTC9, PullUp ); + + while ( 1 ) + { + if ( button1 == 0 ) + { + printf( "button 1 pressed\n" ); + return; + } + if ( button2 == 0 ) + { + printf( "button 2 pressed\n" ); + return; + } + if ( button3 == 0 ) + { + printf( "button 3 pressed\n" ); + return; + } + if ( button4 == 0 ) + { + printf( "button 4 pressed\n" ); + return; + } + if ( button5 == 0 ) + { + printf( "button 5 pressed\n" ); + return; + } + wait ( 0.2 ); + } +} + /*------------------------------------------------------------------------ * Hauptprogramm ------------------------------------------------------------------------*/ @@ -172,24 +487,44 @@ switch ( test ) { case 0: - doPrintAnalogeValues(); + doPrintAnalogeValues(); // Analoge Sensoren break; case 1: - doLedTicker(); + doLedTicker(); // Lauflicht break; case 2: - doServoThread(); + doServoThread(); // Kamara Plattform mit Servos break; case 3: - doMotorThread(); + doMotorThread(); // 2 Motoren break; case 4: - doStepperThread(); + doStepperThread(); // Schrittmotor break; case 5: - doMosfet(); + doMosfet(); // MOSFET LED oder LED Strip V12 break; - + case 6: + doESP8266(); // WLAN Modem + break; + case 7: + doRFIDReader(); // RFID Reader + break; + case 8: + doSPILedStrip(); // LED Strip am SPI Bus + break; + case 9: + doLEDStrip(); // LED Strip 12 Volt Variante + break; + case 10: + doRC(); // RC Switch (Funk) + break; + case 11: + onReceiveIR(); // IR Receiver + break; + case 12: + doButtons(); // Buttons auf Shield + break; default: break; }