I'm trying Sim800l and ESP8066 As my serial communication to Mega r3.
when i try sim800l code side, something like this: which i refined the code a little bit. In Serial monitor handshakes are ok. but when new sms arrives it doesn't show the right content.
Why with same code I see different results?
void setup()
{
Serial.begin(9600);
Serial1.begin(9600);
Serial.println("Starting...");
delay(1000);
Serial1.println("AT");
pollSms();
Serial1.println("AT+CMGF=1");
pollSms();
Serial1.println("AT+CNMI=1,2,0,0,0");
pollSms();
}
void loop()
{
pollSms();
}
void pollSms()
{
delay(500);
while (Serial.available())
{
Serial1.write(Serial.read());
}
while(Serial1.available())
{
Serial.write(Serial1.read());
}
}
everything is just fine but when I try
#include <SPI.h>//Communicate via WiFi
#include <DHT.h>
#include <avr/sleep.h>
#include <avr/interrupt.h>
#include <WiFi.h>
/* Libraries */
/* Variables */
char id[] = "id";//id of Wifi network
char pass[] = "password";//password of Wifi network
const char* host = "esp8266sd";
float humadity1 , temprature1=25;//Solon 1
float humadity2 , temprature2;//Solon 2
int Soil_humadity1,Soil_humadity2,Soil_humadity3;
char c;
String WiFi_instruction="";
String SMSmessage="";
String number, Answer;
boolean valid;
#define TWO_HRS 7200000
unsigned long startTime1,startTime2;
/* Variables */
/* AM2301 */
#define DHTTYPE DHT21 // It defines which kind of humadity and temprature sensor is connecter (AM2301)
DHT dht1(DHTPIN1, DHTTYPE);
DHT dht2(DHTPIN2, DHTTYPE);
/* AM2301 */
/* Android App */
WiFiServer server(80);
/* Android App */
/* Valid numbers */
String num1="9016057005";
String num2="9026057005";//change it
/* Valid numbers */
void setup()
{
/* Pins Settings */
/* Serial communications */
Serial.begin(9600); // setup baudrate for transition to "serial monitor"
Serial1.begin(9600);//Sim800l Serial Communication
Serial3.begin(9600);//ESP8266 Serial Communication
/* SMS Settings */
Serial.println("Start!...");
Serial1.println("AT"); //Send AT command it will says "OK" if everything is fine
Serialcom(); //description below
Serial1.println("AT+CMGF=0"); // Configuring TEXT mode
Serialcom();
Serial1.println("AT+IPR=9600");
Serial1.println("AT+CNMI=1,2,0,0,0");
Serialcom();
/* SMS Settings */
/* Serial communications */
/* Timers */
startTime1 = millis();
startTime2 = millis();
/* Timers */
/* Sensors */
dht1.begin(); //setup humadity and temprature sensor: Solon 1
dht2.begin(); //setup humadity and temprature sensor: Solon 2
humadity1 = dht1.readHumidity();
temprature1 = dht1.readTemperature();
humadity2 = dht2.readHumidity();
temprature2 = dht2.readTemperature();
/* Sensors */
}//setup ended
void loop()
{
char x;
/* Receiving instruction from WiFi' serial */
while (Serial3.available())
{
x=Serial3.read();
WiFi_instruction.concat(x);
}
Serial.println("WiFi instructions");
/* Check for any instruction*/
WiFi_instruction="";// WiFi_instruction is empty now.
}//if WiFi_instruction.length()!-0
/* Receiving instruction from WiFi' serial */
/* Yl-69 Sensors */
Soil_humadity1=digitalRead(YL1);//If it return 1, soil is ok otherwise not
Soil_humadity2=digitalRead(YL2);
Soil_humadity3=digitalRead(YL3);
/* Yl-69 Sensors */
/* AS2301 Sensors's Data */
float temp_temprature1,temp_temprature2,temp_humadity1,temp_humadity2;
temp_temprature1= dht1.readTemperature();
temp_temprature2= dht2.readTemperature();
temp_humadity1=dht1.readHumidity();
temp_humadity2=dht2.readHumidity();
//Whether data is NaN or 0 or some none sense increment-decrement, new numbers won't be considered
if(isnan(temp_temprature1) ||isnan(temp_temprature2)||
isnan(temp_humadity1)||isnan(temp_humadity2))
{
/* Do nothing */
}
else
{
if(temp_temprature1!=0 &&
temp_temprature2!=0&&
temp_humadity1!=0 &&
temp_humadity2!=0)
{
if(abs(dht1.readHumidity()-humadity1)<10
&& abs(dht2.readHumidity()-humadity2)<10
&& abs(dht1.readTemperature()-temprature1)<10
&& abs(dht2.readTemperature()-temprature2)<10)
{
Serial.print(" Sensor values have been changed ");
humadity1 = dht1.readHumidity();
temprature1 = dht1.readTemperature();
humadity2 = dht2.readHumidity();
temprature2 = dht2.readTemperature();
}
}
}
/* AS2301 Sensors's Data */
/* Check for any SMS */
while(Serial1.available())
{
x=Serial1.read();
SMSmessage.concat(x);
}
Serial.println("SMS:");
Serial.println(SMSmessage);
if(SMSmessage.length()>0)
{
int valid1=SMSmessage.indexOf(num1);
int valid2=SMSmessage.indexOf(num2);
if(valid1==0 || valid2==0)
{
Serial.println("number is valid");
valid=true;
}
/*Extranct the number to andwer */
if(valid1==0)
number=num1;
if(valid2==0)
number=num2;
/*Extranct the number to andwer */
/*Check for any sms */
/* Answering via SMS */
//If there is the Code for a specific Instruction it return 1 otherwise returns -1
if(valid)
{
inst00=SMSmessage.indexOf(inst0);
inst01_CHECK=SMSinst01=SMSmessage.indexOf(inst1);//automatic
inst02_CHECK=SMSinst02=SMSmessage.indexOf(inst2);
inst03_CHECK=SMSinst03=SMSmessage.indexOf(inst3);
inst04_CHECK=SMSinst04=SMSmessage.indexOf(inst4);
if(SMSinst02 ==-1 && SMSinst03==-1 && SMSinst04==-1)// if none of the non-automatic in selected it mast remain automatic
SMSinst01=0;
inst05_CHECK=SMSinst05=SMSmessage.indexOf(inst5);//automatic
inst06_CHECK=SMSinst06=SMSmessage.indexOf(inst6);
inst07_CHECK=SMSinst07=SMSmessage.indexOf(inst7);
inst08_CHECK=SMSinst08=SMSmessage.indexOf(inst8);
if(SMSinst06 ==-1 && SMSinst07==-1 && SMSinst08==-1)// if none of the non-automatic in selected it mast remain automatic
SMSinst05=0;
inst09_CHECK=SMSinst09=SMSmessage.indexOf(inst9);//automatic
inst010_CHECK=SMSinst010=SMSmessage.indexOf(inst10);
inst011_CHECK=SMSinst011=SMSmessage.indexOf(inst11);
if(SMSinst010 ==-1 && SMSinst011==-1)// if none of the non-automatic in selected it mast remain automatic
SMSinst09=0;
inst012_CHECK=SMSinst012=SMSmessage.indexOf(inst12);//automatic
inst013_CHECK=SMSinst013=SMSmessage.indexOf(inst13);
inst014_CHECK=SMSinst014=SMSmessage.indexOf(inst14);
if(SMSinst013 ==-1 && SMSinst014==-1)// if none of the non-automatic in selected it mast remain automatic
inst012=0;
inst015_CHECK=SMSinst015=SMSmessage.indexOf(inst15);//automatic
inst016_CHECK=SMSinst016=SMSmessage.indexOf(inst16);
inst017_CHECK=SMSinst017=SMSmessage.indexOf(inst17);
if(SMSinst016 ==-1 && SMSinst017==-1)// if none of the non-automatic in selected it mast remain automatic
SMSinst015=0;
inst018_CHECK=SMSinst018=SMSmessage.indexOf(inst18);//automatic
inst019_CHECK=SMSinst019=SMSmessage.indexOf(inst19);
inst020_CHECK=SMSinst020=SMSmessage.indexOf(inst20);
if(SMSinst019 ==-1 && SMSinst020==-1)// if none of the non-automatic in selected it mast remain automatic
SMSinst018=0;
inst021_CHECK=SMSinst021=SMSmessage.indexOf(inst21);//automatic
inst022_CHECK=SMSinst022=SMSmessage.indexOf(inst22);
inst023_CHECK=SMSinst023=SMSmessage.indexOf(inst23);
if(SMSinst022 ==-1 && SMSinst023==-1)// if none of the non-automatic in selected it mast remain automatic
SMSinst021=0;
inst024_CHECK=SMSinst024=SMSmessage.indexOf(inst24);//automatic
inst025_CHECK=SMSinst025=SMSmessage.indexOf(inst25);
inst026_CHECK=SMSinst026=SMSmessage.indexOf(inst26);
if(SMSinst025 ==-1 && SMSinst026==-1)// if none of the non-automatic in selected it mast remain automatic
SMSinst024=0;
inst027_CHECK=SMSinst027=SMSmessage.indexOf(inst27);//automatic
inst028_CHECK=SMSinst028=SMSmessage.indexOf(inst28);
inst029_CHECK=SMSinst029=SMSmessage.indexOf(inst29);
if(SMSinst028 ==-1 && SMSinst029==-1)// if none of the non-automatic in selected it mast remain automatic
SMSinst027=0;
SMSmessage="";//SMSmessage now is empty until next sms
}//if valid
}// if SMS.length()
Serial.println("SMS instructions");
if(inst00)
{
String statSoil1,statSoil2,statSoil3;
/* YL, Soil's humadity Sensors results */
}//Loop ends
/*Functions */
/*
void Serialcom()
{
delay(500);
while(Serial.available()) // IDE serial l serial Serial1
{
Serial1.write(Serial.read());//Forward what Serial received to Software Serial Port
}
while(Serial1.available()) //serialSerial1 l serial dial IDE
{
Serial.write(Serial1.read());//Forward what Software Serial received to Serial Port
}
}
/*Functions */
Related
Hey i would like to write a long string like "Hello my name is james and i live in NewYork" in a rfid Tag using arduino.
The code for writing in a single block is the following one but i can only write 16 bytes in 1 block ....
but the only issue is that it does only write into 1 block.
So i would like that 16 First bytes goes to Block 1 ect ...
#include <SPI.h>
#include <MFRC522.h>
/*Using Hardware SPI of Arduino */
/*MOSI (11), MISO (12) and SCK (13) are fixed */
/*You can configure SS and RST Pins*/
#define SS_PIN 10 /* Slave Select Pin */
#define RST_PIN 7 /* Reset Pin */
/* Create an instance of MFRC522 */
MFRC522 mfrc522(SS_PIN, RST_PIN);
/* Create an instance of MIFARE_Key */
MFRC522::MIFARE_Key key;
/* Set the block to which we want to write data */
/* Be aware of Sector Trailer Blocks */
int blockNum = 2;
/* Create an array of 16 Bytes and fill it with data */
/* This is the actual data which is going to be written into the card */
byte blockData [16] = {"data-to-write-to-card"};
/* Create another array to read data from Block */
/* Legthn of buffer should be 2 Bytes more than the size of Block (16 Bytes) */
byte bufferLen = 18;
byte readBlockData[18];
MFRC522::StatusCode status;
void setup()
{
/* Initialize serial communications with the PC */
Serial.begin(9600);
/* Initialize SPI bus */
SPI.begin();
/* Initialize MFRC522 Module */
mfrc522.PCD_Init();
Serial.println("Scan a MIFARE 1K Tag to write data...");
}
void loop()
{
/* Prepare the ksy for authentication */
/* All keys are set to FFFFFFFFFFFFh at chip delivery from the factory */
for (byte i = 0; i < 6; i++)
{
key.keyByte[i] = 0xFF;
}
/* Look for new cards */
/* Reset the loop if no new card is present on RC522 Reader */
if ( ! mfrc522.PICC_IsNewCardPresent())
{
return;
}
/* Select one of the cards */
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}
Serial.print("\n");
Serial.println("**Card Detected**");
/* Print UID of the Card */
Serial.print(F("Card UID:"));
for (byte i = 0; i < mfrc522.uid.size; i++)
{
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
}
Serial.print("\n");
/* Print type of card (for example, MIFARE 1K) */
Serial.print(F("PICC type: "));
MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
Serial.println(mfrc522.PICC_GetTypeName(piccType));
/* Call 'WriteDataToBlock' function, which will write data to the block */
Serial.print("\n");
Serial.println("Writing to Data Block...");
WriteDataToBlock(blockNum, blockData);
/* Read data from the same block */
Serial.print("\n");
Serial.println("Reading from Data Block...");
ReadDataFromBlock(blockNum, readBlockData);
/* If you want to print the full memory dump, uncomment the next line */
//mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
/* Print the data read from block */
Serial.print("\n");
Serial.print("Data in Block:");
Serial.print(blockNum);
Serial.print(" --> ");
for (int j=0 ; j<16 ; j++)
{
Serial.write(readBlockData[j]);
}
Serial.print("\n");
}
void WriteDataToBlock(int blockNum, byte blockData[])
{
/* Authenticating the desired data block for write access using Key A */
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, blockNum, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK)
{
Serial.print("Authentication failed for Write: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
else
{
Serial.println("Authentication success");
}
/* Write data to the block */
status = mfrc522.MIFARE_Write(blockNum, blockData, 16);
if (status != MFRC522::STATUS_OK)
{
Serial.print("Writing to Block failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
else
{
Serial.println("Data was written into Block successfully");
}
}
void ReadDataFromBlock(int blockNum, byte readBlockData[])
{
/* Authenticating the desired data block for Read access using Key A */
byte status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, blockNum, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK)
{
Serial.print("Authentication failed for Read: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
else
{
Serial.println("Authentication success");
}
/* Reading data from the Block */
status = mfrc522.MIFARE_Read(blockNum, readBlockData, &bufferLen);
if (status != MFRC522::STATUS_OK)
{
Serial.print("Reading failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
else
{
Serial.println("Block was read successfully");
}
I'm working on Arduino RC 522 RFID module. I'm want to write some data in the RFID card and read that data to operate a relay. So is that possible? I want to store integer data in the RFID CARD(13.5Mhz).
for writing the card you can try with this code, change the variable "blockData" with the integer, the lenght should be 16 bytes or you have to change the dimensions of the arrays and for cycles.
#include <SPI.h>
#include <MFRC522.h>
/*Using Hardware SPI of Arduino */
/*MOSI (11), MISO (12) and SCK (13) are fixed */
/*You can configure SS and RST Pins*/
#define SS_PIN 10 /* Slave Select Pin */
#define RST_PIN 7 /* Reset Pin */
/* Create an instance of MFRC522 */
MFRC522 mfrc522(SS_PIN, RST_PIN);
/* Create an instance of MIFARE_Key */
MFRC522::MIFARE_Key key;
/* Set the block to which we want to write data */
/* Be aware of Sector Trailer Blocks */
int blockNum = 2;
/* Create an array of 16 Bytes and fill it with data */
/* This is the actual data which is going to be written into the card */
byte blockData [16] = {"Electronics-Hub-"};
/* Create another array to read data from Block */
/* Legthn of buffer should be 2 Bytes more than the size of Block (16 Bytes) */
byte bufferLen = 18;
byte readBlockData[18];
MFRC522::StatusCode status;
void setup()
{
/* Initialize serial communications with the PC */
Serial.begin(9600);
/* Initialize SPI bus */
SPI.begin();
/* Initialize MFRC522 Module */
mfrc522.PCD_Init();
Serial.println("Scan a MIFARE 1K Tag to write data...");
}
void loop()
{
/* Prepare the ksy for authentication */
/* All keys are set to FFFFFFFFFFFFh at chip delivery from the factory */
for (byte i = 0; i < 6; i++)
{
key.keyByte[i] = 0xFF;
}
/* Look for new cards */
/* Reset the loop if no new card is present on RC522 Reader */
if ( ! mfrc522.PICC_IsNewCardPresent())
{
return;
}
/* Select one of the cards */
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}
Serial.print("\n");
Serial.println("**Card Detected**");
/* Print UID of the Card */
Serial.print(F("Card UID:"));
for (byte i = 0; i < mfrc522.uid.size; i++)
{
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
}
Serial.print("\n");
/* Print type of card (for example, MIFARE 1K) */
Serial.print(F("PICC type: "));
MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
Serial.println(mfrc522.PICC_GetTypeName(piccType));
/* Call 'WriteDataToBlock' function, which will write data to the block */
Serial.print("\n");
Serial.println("Writing to Data Block...");
WriteDataToBlock(blockNum, blockData);
/* Read data from the same block */
Serial.print("\n");
Serial.println("Reading from Data Block...");
ReadDataFromBlock(blockNum, readBlockData);
/* If you want to print the full memory dump, uncomment the next line */
//mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
/* Print the data read from block */
Serial.print("\n");
Serial.print("Data in Block:");
Serial.print(blockNum);
Serial.print(" --> ");
for (int j=0 ; j<16 ; j++)
{
Serial.write(readBlockData[j]);
}
Serial.print("\n");
}
void WriteDataToBlock(int blockNum, byte blockData[])
{
/* Authenticating the desired data block for write access using Key A */
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, blockNum, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK)
{
Serial.print("Authentication failed for Write: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
else
{
Serial.println("Authentication success");
}
/* Write data to the block */
status = mfrc522.MIFARE_Write(blockNum, blockData, 16);
if (status != MFRC522::STATUS_OK)
{
Serial.print("Writing to Block failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
else
{
Serial.println("Data was written into Block successfully");
}
}
void ReadDataFromBlock(int blockNum, byte readBlockData[])
{
/* Authenticating the desired data block for Read access using Key A */
byte status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, blockNum, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK)
{
Serial.print("Authentication failed for Read: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
else
{
Serial.println("Authentication success");
}
/* Reading data from the Block */
status = mfrc522.MIFARE_Read(blockNum, readBlockData, &bufferLen);
if (status != MFRC522::STATUS_OK)
{
Serial.print("Reading failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
else
{
Serial.println("Block was read successfully");
}
}
For reading data use this code, change "mioCod" with the UID of the card
void ceckRFID(){
//check new cards
if (!mfrc522.PICC_IsNewCardPresent())
{
return;
}
//If read UID
if (!mfrc522.PICC_ReadCardSerial())
{
return;
}
String codLetto = ""; //clean the reading string
for (byte i = 0; i < mfrc522.uid.size; i++) { //save the code into the string
codLetto += String(mfrc522.uid.uidByte[i], HEX);
}
codLetto.toUpperCase(); //uppercase the string
Serial.println(codLetto); //print the code
if(codLetto == mioCod){ //if the code match with mioCod
Serial.println("Code recognized");
}
else{
Serial.println("Wrong code");
}
}
I want to write a personal data on my Card like:
"Name"
"Age"
"Region"
etc.
I wanna do this in this code:
https://imgur.com/BnlsKU3
https://imgur.com/4lNDqVX
https://imgur.com/LGCdXET
On this links are my screens to my code
Use this basic approach to adapt to your code. So far your arduino code is only reading information from the card or tag with pre-loaded data.
You can follow this to write data to your card
Pls: Follow your card documentation or datasheet on the amount of data you can write to it
#include <SPI.h>
#include <MFRC522.h>
/*Using Hardware SPI of Arduino */
/*MOSI (11), MISO (12) and SCK (13) are fixed */
/*You can configure SS and RST Pins*/
#define SS_PIN 10 /* Slave Select Pin */
#define RST_PIN 7 /* Reset Pin */
/* Create an instance of MFRC522 */
MFRC522 mfrc522(SS_PIN, RST_PIN);
/* Create an instance of MIFARE_Key */
MFRC522::MIFARE_Key key;
/* Set the block to which we want to write data */
/* Be aware of Sector Trailer Blocks */
int blockNum = 2;
/* Create an array of 16 Bytes and fill it with data */
/* This is the actual data which is going to be written into the card */
byte blockData [16] = {"data-to-write-to-card"};
/* Create another array to read data from Block */
/* Legthn of buffer should be 2 Bytes more than the size of Block (16 Bytes) */
byte bufferLen = 18;
byte readBlockData[18];
MFRC522::StatusCode status;
void setup()
{
/* Initialize serial communications with the PC */
Serial.begin(9600);
/* Initialize SPI bus */
SPI.begin();
/* Initialize MFRC522 Module */
mfrc522.PCD_Init();
Serial.println("Scan a MIFARE 1K Tag to write data...");
}
void loop()
{
/* Prepare the ksy for authentication */
/* All keys are set to FFFFFFFFFFFFh at chip delivery from the factory */
for (byte i = 0; i < 6; i++)
{
key.keyByte[i] = 0xFF;
}
/* Look for new cards */
/* Reset the loop if no new card is present on RC522 Reader */
if ( ! mfrc522.PICC_IsNewCardPresent())
{
return;
}
/* Select one of the cards */
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}
Serial.print("\n");
Serial.println("**Card Detected**");
/* Print UID of the Card */
Serial.print(F("Card UID:"));
for (byte i = 0; i < mfrc522.uid.size; i++)
{
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
}
Serial.print("\n");
/* Print type of card (for example, MIFARE 1K) */
Serial.print(F("PICC type: "));
MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
Serial.println(mfrc522.PICC_GetTypeName(piccType));
/* Call 'WriteDataToBlock' function, which will write data to the block */
Serial.print("\n");
Serial.println("Writing to Data Block...");
WriteDataToBlock(blockNum, blockData);
/* Read data from the same block */
Serial.print("\n");
Serial.println("Reading from Data Block...");
ReadDataFromBlock(blockNum, readBlockData);
/* If you want to print the full memory dump, uncomment the next line */
//mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
/* Print the data read from block */
Serial.print("\n");
Serial.print("Data in Block:");
Serial.print(blockNum);
Serial.print(" --> ");
for (int j=0 ; j<16 ; j++)
{
Serial.write(readBlockData[j]);
}
Serial.print("\n");
}
void WriteDataToBlock(int blockNum, byte blockData[])
{
/* Authenticating the desired data block for write access using Key A */
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, blockNum, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK)
{
Serial.print("Authentication failed for Write: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
else
{
Serial.println("Authentication success");
}
/* Write data to the block */
status = mfrc522.MIFARE_Write(blockNum, blockData, 16);
if (status != MFRC522::STATUS_OK)
{
Serial.print("Writing to Block failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
else
{
Serial.println("Data was written into Block successfully");
}
}
void ReadDataFromBlock(int blockNum, byte readBlockData[])
{
/* Authenticating the desired data block for Read access using Key A */
byte status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, blockNum, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK)
{
Serial.print("Authentication failed for Read: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
else
{
Serial.println("Authentication success");
}
/* Reading data from the Block */
status = mfrc522.MIFARE_Read(blockNum, readBlockData, &bufferLen);
if (status != MFRC522::STATUS_OK)
{
Serial.print("Reading failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
else
{
Serial.println("Block was read successfully");
}
}
I've taken my Arduino out of the box about an hour ago and I'm trying to get some bits of code working.
The code below is supposed to wait for an input from the Serial Monitor and set the LED connected at terminal 9 to the input value.
The Arduino reads the value the first time, but it always sets the LED to full brightness and never prints the "input invalid". The Arduino also doesn't read any value after the first one. What is the reason?
int A;
void setup()
{
Serial.begin(9600);
Serial.println("input value");
}
void loop()
{
A = 0; /* Reset A */
if (Serial.available() > 0) /* Wait for input */
{
A = Serial.read(); /* Read input */
if(A >= 0 && A <= 255) /* Check for valid input */
{
analogWrite(9, A); /* Set value of an LED */
}
else
{
Serial.println("input invalid");
}
}
delay(100); /* Wait 0.1 second */
}
According to the rest of code the Serial.parseInt() should be used instead of reading one character by Serial.read().
To check if my cc3000 Wifi Shield works, I download the Adafruit CC3000 Library and run the example in the library, which is 'buildtest'.
Here are the link of Adafruit and library.
https://learn.adafruit.com/adafruit-shield-compatibility/cc3000-wifi-shield
https://github.com/adafruit/Adafruit_CC3000_Library
But when I run 'buildtest', the only things I can see in Serial monitor are corrupt(or garbled) messages.
Here is the code of 'buildtest' in the library.
/***************************************************
This is an example for the Adafruit CC3000 Wifi Breakout & Shield
Designed specifically to work with the Adafruit WiFi products:
----> https://www.adafruit.com/products/1469
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Kevin Townsend & Limor Fried for Adafruit Industries.
BSD license, all text above must be included in any redistribution
****************************************************/
/*
This example does a full test of core connectivity:
* Initialization
* SSID Scan
* AP connection
* DHCP printout
* DNS lookup
* Ping
* Disconnect
It's a good idea to run this sketch when first setting up the
module.
*/
#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <string.h>
#include "utility/debug.h"
// These are the interrupt and control pins
#define ADAFRUIT_CC3000_IRQ 3 // MUST be an interrupt pin!
// These can be any two pins
#define ADAFRUIT_CC3000_VBAT 5
#define ADAFRUIT_CC3000_CS 10
// Use hardware SPI for the remaining pins
// On an UNO, SCK = 13, MISO = 12, and MOSI = 11
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,
SPI_CLOCK_DIVIDER); // you can change this clock speed but DI
#define WLAN_SSID "myNetwork" // cannot be longer than 32 characters!
#define WLAN_PASS "myPassword"
// Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
#define WLAN_SECURITY WLAN_SEC_WPA2
/**************************************************************************/
/*!
#brief Sets up the HW and the CC3000 module (called automatically
on startup)
*/
/**************************************************************************/
void setup(void)
{
Serial.begin(115200);
Serial.println(F("Hello, CC3000!\n"));
displayDriverMode();
Serial.print("Free RAM: "); Serial.println(getFreeRam(), DEC);
/* Initialise the module */
Serial.println(F("\nInitialising the CC3000 ..."));
if (!cc3000.begin())
{
Serial.println(F("Unable to initialise the CC3000! Check your wiring?"));
while(1);
}
/* Optional: Update the Mac Address to a known value */
/*
uint8_t macAddress[6] = { 0x08, 0x00, 0x28, 0x01, 0x79, 0xB7 };
if (!cc3000.setMacAddress(macAddress))
{
Serial.println(F("Failed trying to update the MAC address"));
while(1);
}
*/
uint16_t firmware = checkFirmwareVersion();
if (firmware < 0x113) {
Serial.println(F("Wrong firmware version!"));
for(;;);
}
displayMACAddress();
/* Optional: Get the SSID list (not available in 'tiny' mode) */
#ifndef CC3000_TINY_DRIVER
listSSIDResults();
#endif
/* Delete any old connection data on the module */
Serial.println(F("\nDeleting old connection profiles"));
if (!cc3000.deleteProfiles()) {
Serial.println(F("Failed!"));
while(1);
}
/* Optional: Set a static IP address instead of using DHCP.
Note that the setStaticIPAddress function will save its state
in the CC3000's internal non-volatile memory and the details
will be used the next time the CC3000 connects to a network.
This means you only need to call the function once and the
CC3000 will remember the connection details. To switch back
to using DHCP, call the setDHCP() function (again only needs
to be called once).
*/
/*
uint32_t ipAddress = cc3000.IP2U32(192, 168, 1, 19);
uint32_t netMask = cc3000.IP2U32(255, 255, 255, 0);
uint32_t defaultGateway = cc3000.IP2U32(192, 168, 1, 1);
uint32_t dns = cc3000.IP2U32(8, 8, 4, 4);
if (!cc3000.setStaticIPAddress(ipAddress, netMask, defaultGateway, dns)) {
Serial.println(F("Failed to set static IP!"));
while(1);
}
*/
/* Optional: Revert back from static IP addres to use DHCP.
See note for setStaticIPAddress above, this only needs to be
called once and will be remembered afterwards by the CC3000.
*/
/*
if (!cc3000.setDHCP()) {
Serial.println(F("Failed to set DHCP!"));
while(1);
}
*/
/* Attempt to connect to an access point */
char *ssid = WLAN_SSID; /* Max 32 chars */
Serial.print(F("\nAttempting to connect to ")); Serial.println(ssid);
/* NOTE: Secure connections are not available in 'Tiny' mode!
By default connectToAP will retry indefinitely, however you can pass an
optional maximum number of retries (greater than zero) as the fourth parameter.
ALSO NOTE: By default connectToAP will retry forever until it can connect to
the access point. This means if the access point doesn't exist the call
will _never_ return! You can however put in an optional maximum retry count
by passing a 4th parameter to the connectToAP function below. This should
be a number of retries to make before giving up, for example 5 would retry
5 times and then fail if a connection couldn't be made.
*/
if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
Serial.println(F("Failed!"));
while(1);
}
Serial.println(F("Connected!"));
/* Wait for DHCP to complete */
Serial.println(F("Request DHCP"));
while (!cc3000.checkDHCP())
{
delay(100); // ToDo: Insert a DHCP timeout!
}
/* Display the IP address DNS, Gateway, etc. */
while (! displayConnectionDetails()) {
delay(1000);
}
#ifndef CC3000_TINY_DRIVER
/* Try looking up www.adafruit.com */
uint32_t ip = 0;
Serial.print(F("www.adafruit.com -> "));
while (ip == 0) {
if (! cc3000.getHostByName("www.adafruit.com", &ip)) {
Serial.println(F("Couldn't resolve!"));
}
delay(500);
}
cc3000.printIPdotsRev(ip);
/* Do a quick ping test on adafruit.com */
Serial.print(F("\n\rPinging ")); cc3000.printIPdotsRev(ip); Serial.print("...");
uint8_t replies = cc3000.ping(ip, 5);
Serial.print(replies); Serial.println(F(" replies"));
if (replies)
Serial.println(F("Ping successful!"));
#endif
/* You need to make sure to clean up after yourself or the CC3000 can freak out */
/* the next time you try to connect ... */
Serial.println(F("\n\nClosing the connection"));
cc3000.disconnect();
}
void loop(void)
{
delay(1000);
}
/**************************************************************************/
/*!
#brief Displays the driver mode (tiny of normal), and the buffer
size if tiny mode is not being used
#note The buffer size and driver mode are defined in cc3000_common.h
*/
/**************************************************************************/
void displayDriverMode(void)
{
#ifdef CC3000_TINY_DRIVER
Serial.println(F("CC3000 is configure in 'Tiny' mode"));
#else
Serial.print(F("RX Buffer : "));
Serial.print(CC3000_RX_BUFFER_SIZE);
Serial.println(F(" bytes"));
Serial.print(F("TX Buffer : "));
Serial.print(CC3000_TX_BUFFER_SIZE);
Serial.println(F(" bytes"));
#endif
}
/**************************************************************************/
/*!
#brief Tries to read the CC3000's internal firmware patch ID
*/
/**************************************************************************/
uint16_t checkFirmwareVersion(void)
{
uint8_t major, minor;
uint16_t version;
#ifndef CC3000_TINY_DRIVER
if(!cc3000.getFirmwareVersion(&major, &minor))
{
Serial.println(F("Unable to retrieve the firmware version!\r\n"));
version = 0;
}
else
{
Serial.print(F("Firmware V. : "));
Serial.print(major); Serial.print(F(".")); Serial.println(minor);
version = major; version <<= 8; version |= minor;
}
#endif
return version;
}
/**************************************************************************/
/*!
#brief Tries to read the 6-byte MAC address of the CC3000 module
*/
/**************************************************************************/
void displayMACAddress(void)
{
uint8_t macAddress[6];
if(!cc3000.getMacAddress(macAddress))
{
Serial.println(F("Unable to retrieve MAC Address!\r\n"));
}
else
{
Serial.print(F("MAC Address : "));
cc3000.printHex((byte*)&macAddress, 6);
}
}
/**************************************************************************/
/*!
#brief Tries to read the IP address and other connection details
*/
/**************************************************************************/
bool displayConnectionDetails(void)
{
uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv;
if(!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv))
{
Serial.println(F("Unable to retrieve the IP Address!\r\n"));
return false;
}
else
{
Serial.print(F("\nIP Addr: ")); cc3000.printIPdotsRev(ipAddress);
Serial.print(F("\nNetmask: ")); cc3000.printIPdotsRev(netmask);
Serial.print(F("\nGateway: ")); cc3000.printIPdotsRev(gateway);
Serial.print(F("\nDHCPsrv: ")); cc3000.printIPdotsRev(dhcpserv);
Serial.print(F("\nDNSserv: ")); cc3000.printIPdotsRev(dnsserv);
Serial.println();
return true;
}
}
/**************************************************************************/
/*!
#brief Begins an SSID scan and prints out all the visible networks
*/
/**************************************************************************/
void listSSIDResults(void)
{
uint32_t index;
uint8_t valid, rssi, sec;
char ssidname[33];
if (!cc3000.startSSIDscan(&index)) {
Serial.println(F("SSID scan failed!"));
return;
}
Serial.print(F("Networks found: ")); Serial.println(index);
Serial.println(F("================================================"));
while (index) {
index--;
valid = cc3000.getNextSSID(&rssi, &sec, ssidname);
Serial.print(F("SSID Name : ")); Serial.print(ssidname);
Serial.println();
Serial.print(F("RSSI : "));
Serial.println(rssi);
Serial.print(F("Security Mode: "));
Serial.println(sec);
Serial.println();
}
Serial.println(F("================================================"));
cc3000.stopSSIDscan();
}
Sorry, I know this is very long code, but I can't understand why I get the corrupt message.
I don't think this problem is related to the setting of WIFI(#define WLAN SSID or #define WLAN PASS).
Because... if you look at the 'void setup' function, you can see
Serial.println(F("Hello, CC3000!\n"));
so I should see this message(Hello CC3000) no matter what.
Even if I set the my WLAN SSID and WLAN PASS, same corrupt message appears.
I can not check whether my cc3000 work well because of this corrupt message.
How can I solve this problem??
It usually means that you are using the wrong baudrate. In your program listing you have this line
“Serial.begin(115200);”
This sets the default baudrate.
In the Arduino software-> Tools->Serial Monitor-> bottom right corner you will see the baudrate window. This needs to match the baudrate in your program otherwise you get rubbish displayed.