NB-IoT

Dependencies:   NDefLib X_NUCLEO_NFC02A1 hdc1080 mbed

Fork of HelloWorld_NFC02A1 by ST

Committer:
giovannivisentini
Date:
Mon Oct 03 11:58:00 2016 +0000
Revision:
9:0bd2f0c13d3d
Parent:
8:9bdd16ebccfb
Child:
11:d0665d9c28a6
update libs and enable tag reading

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rosarium 0:892175366555 1 /**
rosarium 0:892175366555 2 ******************************************************************************
rosarium 0:892175366555 3 * @file main.cpp
rosarium 1:11ae12d41082 4 * @author AMG Central Lab
rosarium 0:892175366555 5 * @version V1.0.0
rosarium 1:11ae12d41082 6 * @date 30 Aug 2016
rosarium 0:892175366555 7 * @brief This demo writes an ndef message with an url inside.
rosarium 0:892175366555 8 ******************************************************************************
rosarium 0:892175366555 9 * @attention
rosarium 0:892175366555 10 *
rosarium 1:11ae12d41082 11 * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
rosarium 0:892175366555 12 *
rosarium 0:892175366555 13 * Redistribution and use in source and binary forms, with or without modification,
rosarium 0:892175366555 14 * are permitted provided that the following conditions are met:
rosarium 0:892175366555 15 * 1. Redistributions of source code must retain the above copyright notice,
rosarium 0:892175366555 16 * this list of conditions and the following disclaimer.
rosarium 0:892175366555 17 * 2. Redistributions in binary form must reproduce the above copyright notice,
rosarium 0:892175366555 18 * this list of conditions and the following disclaimer in the documentation
rosarium 0:892175366555 19 * and/or other materials provided with the distribution.
rosarium 0:892175366555 20 * 3. Neither the name of STMicroelectronics nor the names of its contributors
rosarium 0:892175366555 21 * may be used to endorse or promote products derived from this software
rosarium 0:892175366555 22 * without specific prior written permission.
rosarium 0:892175366555 23 *
rosarium 0:892175366555 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
rosarium 0:892175366555 25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
rosarium 0:892175366555 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
rosarium 0:892175366555 27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
rosarium 0:892175366555 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
rosarium 0:892175366555 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
rosarium 0:892175366555 30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
rosarium 0:892175366555 31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
rosarium 0:892175366555 32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
rosarium 0:892175366555 33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
rosarium 0:892175366555 34 *
rosarium 0:892175366555 35 ******************************************************************************
rosarium 0:892175366555 36 */
rosarium 0:892175366555 37
rosarium 0:892175366555 38 #include "mbed.h"
rosarium 0:892175366555 39
rosarium 0:892175366555 40 #include "X_NUCLEO_NFC02A1.h"
rosarium 0:892175366555 41 #include "DevI2C.h"
rosarium 0:892175366555 42 #include "m24lr.h"
rosarium 1:11ae12d41082 43 #include "NDefLib/NDefNfcTag.h"
rosarium 1:11ae12d41082 44 #include "NDefLib/RecordType/RecordURI.h"
rosarium 8:9bdd16ebccfb 45
rosarium 8:9bdd16ebccfb 46 static volatile bool buttonPress=false; // true when the user press the message
rosarium 8:9bdd16ebccfb 47
rosarium 8:9bdd16ebccfb 48 static void setButtonPress(){
rosarium 8:9bdd16ebccfb 49 buttonPress=true;
rosarium 8:9bdd16ebccfb 50 }//if buttonPress
rosarium 8:9bdd16ebccfb 51
rosarium 8:9bdd16ebccfb 52 static void readAndPrintNfcTag(NDefLib::NDefNfcTag &tag){
rosarium 8:9bdd16ebccfb 53 using namespace NDefLib;
rosarium 8:9bdd16ebccfb 54
rosarium 8:9bdd16ebccfb 55 if(tag.openSession() == true){
rosarium 8:9bdd16ebccfb 56 printf("Open Session\r\n");
rosarium 8:9bdd16ebccfb 57 NDefLib::Message readMsg;
rosarium 8:9bdd16ebccfb 58
rosarium 8:9bdd16ebccfb 59 tag.read(&readMsg);
rosarium 8:9bdd16ebccfb 60 printf("Message Read\r\n");
rosarium 8:9bdd16ebccfb 61
rosarium 8:9bdd16ebccfb 62 if(readMsg.getNRecords()==0){
rosarium 8:9bdd16ebccfb 63 printf("Error Read\r\n");
rosarium 8:9bdd16ebccfb 64 }else{
rosarium 8:9bdd16ebccfb 65 for(uint32_t i=0;i<readMsg.getNRecords();i++){
rosarium 8:9bdd16ebccfb 66 Record *r = readMsg[i];
rosarium 8:9bdd16ebccfb 67 // printRecord(r);
rosarium 8:9bdd16ebccfb 68 //-----
rosarium 8:9bdd16ebccfb 69 RecordURI *const temp = (RecordURI*)r;
rosarium 8:9bdd16ebccfb 70 printf("Read uriId: %d\r\n",temp->getUriId());
rosarium 8:9bdd16ebccfb 71 printf("Read uriType: %s\r\n",temp->getUriType().c_str());
rosarium 8:9bdd16ebccfb 72 printf("Read uriContent: %s\r\n",temp->getContent().c_str());
rosarium 8:9bdd16ebccfb 73 //------
rosarium 8:9bdd16ebccfb 74 delete r;
rosarium 8:9bdd16ebccfb 75 }//for
rosarium 8:9bdd16ebccfb 76 }//if-else
rosarium 8:9bdd16ebccfb 77
rosarium 8:9bdd16ebccfb 78 tag.closeSession();
rosarium 8:9bdd16ebccfb 79 printf("Close session\r\n");
rosarium 8:9bdd16ebccfb 80 }else{
rosarium 8:9bdd16ebccfb 81 printf("Error open read Session\n\r");
rosarium 8:9bdd16ebccfb 82 }
rosarium 8:9bdd16ebccfb 83 }
rosarium 8:9bdd16ebccfb 84
rosarium 0:892175366555 85 /**
rosarium 0:892175366555 86 * Write a Ndef URI message linking to st.com site.
rosarium 0:892175366555 87 */
rosarium 8:9bdd16ebccfb 88
rosarium 0:892175366555 89 int main(void)
rosarium 0:892175366555 90 {
rosarium 0:892175366555 91
rosarium 0:892175366555 92 /*use default board pinout*/
rosarium 0:892175366555 93 DevI2C i2cChannel(X_NUCLEO_NFC02A1::DEFAULT_SDA_PIN,X_NUCLEO_NFC02A1::DEFAULT_SDL_PIN);
rosarium 0:892175366555 94 X_NUCLEO_NFC02A1 *nfcNucleo = X_NUCLEO_NFC02A1::Instance(i2cChannel,
rosarium 0:892175366555 95 X_NUCLEO_NFC02A1::DEFAULT_GPO_PIN,X_NUCLEO_NFC02A1::DEFAULT_RF_DISABLE_PIN,
rosarium 0:892175366555 96 X_NUCLEO_NFC02A1::DEFAULT_LED1_PIN,X_NUCLEO_NFC02A1::DEFAULT_LED2_PIN,
rosarium 0:892175366555 97 X_NUCLEO_NFC02A1::DEFAULT_LED3_PIN);
rosarium 0:892175366555 98
rosarium 2:6f1b1f7f8d12 99 NDefLib::NDefNfcTag& tag =nfcNucleo->getM24LR().getNDefTag();
rosarium 2:6f1b1f7f8d12 100 M24LR & mM24LRp = nfcNucleo->getM24LR();
rosarium 2:6f1b1f7f8d12 101
rosarium 0:892175366555 102 /* Enable Energy Harvesting */
rosarium 1:11ae12d41082 103 mM24LRp.Enable_EnergyHarvesting();
rosarium 0:892175366555 104
rosarium 0:892175366555 105 printf("System Initialization done: !\n\r");
rosarium 0:892175366555 106
rosarium 2:6f1b1f7f8d12 107 /*open the i2c session with the nfc chip*/
rosarium 8:9bdd16ebccfb 108 if(tag.openSession() == true)
rosarium 2:6f1b1f7f8d12 109 {
rosarium 2:6f1b1f7f8d12 110 printf("Session opened\n\r");
rosarium 2:6f1b1f7f8d12 111 nfcNucleo->getLed1()=1;
rosarium 2:6f1b1f7f8d12 112
rosarium 2:6f1b1f7f8d12 113 /*create the NDef message and record*/
rosarium 2:6f1b1f7f8d12 114 NDefLib::Message msg;
rosarium 7:699836375630 115 NDefLib::RecordURI rUri(NDefLib::RecordURI::HTTP_WWW,"st.com/st25");
rosarium 2:6f1b1f7f8d12 116 msg.addRecord(&rUri);
rosarium 2:6f1b1f7f8d12 117
rosarium 2:6f1b1f7f8d12 118 /*write the tag*/
rosarium 8:9bdd16ebccfb 119 if(tag.write(msg) == true)
rosarium 8:9bdd16ebccfb 120 {
rosarium 2:6f1b1f7f8d12 121 printf("Tag written\n\r");
rosarium 2:6f1b1f7f8d12 122 nfcNucleo->getLed2()=1;
rosarium 8:9bdd16ebccfb 123 }
rosarium 8:9bdd16ebccfb 124 else
rosarium 8:9bdd16ebccfb 125 {
rosarium 2:6f1b1f7f8d12 126 printf("Error writing \n\r");
rosarium 2:6f1b1f7f8d12 127 nfcNucleo->getLed1()=0;
rosarium 2:6f1b1f7f8d12 128 }//if-else
rosarium 2:6f1b1f7f8d12 129
rosarium 2:6f1b1f7f8d12 130 /*close the i2c session*/
rosarium 8:9bdd16ebccfb 131 if(tag.closeSession() == true)
rosarium 1:11ae12d41082 132 {
rosarium 2:6f1b1f7f8d12 133 printf("Session closed\n\r");
rosarium 2:6f1b1f7f8d12 134 nfcNucleo->getLed3()=1;
rosarium 1:11ae12d41082 135 }
rosarium 1:11ae12d41082 136 else
rosarium 2:6f1b1f7f8d12 137 {
rosarium 2:6f1b1f7f8d12 138 printf("Error closing the session\n\r");
rosarium 2:6f1b1f7f8d12 139 }//if-else
rosarium 2:6f1b1f7f8d12 140
rosarium 2:6f1b1f7f8d12 141 }
rosarium 2:6f1b1f7f8d12 142 else
rosarium 2:6f1b1f7f8d12 143 printf("Error opening the session\n\r");
giovannivisentini 9:0bd2f0c13d3d 144
giovannivisentini 9:0bd2f0c13d3d 145 #if defined(TARGET_STM)
rosarium 8:9bdd16ebccfb 146 /* enable the button */
rosarium 8:9bdd16ebccfb 147 InterruptIn mybutton(USER_BUTTON);
rosarium 8:9bdd16ebccfb 148
rosarium 8:9bdd16ebccfb 149 mybutton.fall(setButtonPress);
rosarium 8:9bdd16ebccfb 150
rosarium 8:9bdd16ebccfb 151 /*each second change the led status and see if the user press the button*/
rosarium 8:9bdd16ebccfb 152 while(1)
rosarium 8:9bdd16ebccfb 153 {
rosarium 8:9bdd16ebccfb 154 if(buttonPress)
rosarium 8:9bdd16ebccfb 155 {
rosarium 8:9bdd16ebccfb 156 /*write the read message on console*/
rosarium 8:9bdd16ebccfb 157 readAndPrintNfcTag(tag);
rosarium 8:9bdd16ebccfb 158 buttonPress=false;
rosarium 8:9bdd16ebccfb 159 }
rosarium 8:9bdd16ebccfb 160 }
giovannivisentini 9:0bd2f0c13d3d 161 #else
giovannivisentini 9:0bd2f0c13d3d 162 readAndPrintNfcTag(tag);
rosarium 8:9bdd16ebccfb 163 #endif
rosarium 0:892175366555 164 }
rosarium 0:892175366555 165
rosarium 2:6f1b1f7f8d12 166
rosarium 2:6f1b1f7f8d12 167
rosarium 8:9bdd16ebccfb 168
rosarium 2:6f1b1f7f8d12 169 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/