Libelium is offering new technical support channels, to contact with de Technical Support team to solve any issue our doubt please contact with your sales agent or visit our contact site



Post a new topicPost a reply Page 1 of 2   [ 14 posts ]
Go to page 1, 2  Next
Author Message
 Post subject: Receiving IR commands using 3G module
PostPosted: Sat Mar 04, 2017 9:19 pm 

Joined: Sat Mar 04, 2017 9:06 pm
Posts: 7
So I am following this tutorial https://www.cooking-hacks.com/documenta ... ir-remote/ to receive and send IR commands through the 3G/GPRS shield. I am using this code which is in the link:
/*
* IR Remote Module
*
* Copyright (C) Libelium Comunicaciones Distribuidas S.L.
* http://www.libelium.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* a
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*
* Version: 1.0
* Design: David Gascón
* Implementation: Luis Martin
*/

// We need to use the 'raw' pin reading methods
// because timing is very important here and the digitalRead()
// procedure is slower!

#define IRpin_PIN PIND
#define IRpin 2

// The maximum pulse we'll listen for - 65 milliseconds is a long time
#define MAXPULSE 65000

// Timing resolution
#define RESOLUTION 20

int IRledPin = 3; // IR emitter LED connected to digital pin 3
int orangeLedPin = 7; // Orange LED connected to digital pin 7
int buttonPin1 = 4; // "Send" push-button connected to digital pin 4
int blueLedPin = 6; // Blue LED connected to digital pin 6
int buttonPin2 = 5; // "Receive" push-button connected to digital pin 5

int buttonState1 = 0;
int buttonState2 = 0;

int8_t answer;
int onModulePin= 8;

// Store up to 100 pulse pairs
uint16_t pulses[100][2]; // Pair is high and low pulse
uint8_t currentpulse = 0; // Index for pulses we're storing
uint8_t sendpulse = 0; // Index for send pulses

void setup(void) {
// Initialize the Orange LED pin as an output
pinMode(orangeLedPin, OUTPUT);
// Initialize the "Send" push-button pin as an input
pinMode(buttonPin1, INPUT);
// Initialize the Orange LED pin as an output
pinMode(blueLedPin, OUTPUT);
// Initialize the "Send" push-button pin as an input
pinMode(buttonPin2, INPUT);
// Set uart baudrate
Serial.begin(115200);
pinMode(onModulePin, OUTPUT);
power_on();
delay(5000);
while( (sendATcommand("AT+CREG?", "+CREG: 0,1", 1000) ||
sendATcommand("AT+CREG?", "+CREG: 0,5", 1000)) == 0 );
sendATcommand("AT+CLIP=0", "OK", 1000);
}

void loop(void) {
// Read the state of the "Send" push-button value
buttonState1 = digitalRead(buttonPin1);
// Read the state of the "Receive" push-button value
buttonState2 = digitalRead(buttonPin2);

answer = sendATcommand("", "RING", 1000);

if ((buttonState1 == HIGH) || (answer == 1)) {
Serial.println("Sending IR signal");
digitalWrite(orangeLedPin, HIGH);
delay(200);
digitalWrite(orangeLedPin, LOW);
delay(200);

SendIRCode();
//delay(15); // wait 15 milliseconds before sending it again
//SendIRCode(); // repeat IR code if it is neccesary

delay(5000); // wait 5 seconds to resend the code
}

if ((buttonState2==HIGH) || (currentpulse != 0)){

if (buttonState2==HIGH){
Serial.println("Ready to decode IR!");
digitalWrite(blueLedPin, HIGH);
delay(200);
digitalWrite(blueLedPin, LOW);
delay(200);
}

uint16_t highpulse, lowpulse; // Temporary storage timing
highpulse = lowpulse = 0; // Start out with no pulse length

while (IRpin_PIN & (1 << IRpin)) {
// count off another few microseconds
highpulse++;
delayMicroseconds(RESOLUTION);

// If the pulse is too long, we 'timed out' - either nothing
// was received or the code is finished, so print what
// we've grabbed so far, and then reset
if ((highpulse >= MAXPULSE) && (currentpulse != 0)) {
// printpulses();
sendpulse=currentpulse;
currentpulse=0;
return;
}
}
// we didn't time out so lets stash the reading
pulses[currentpulse][0] = highpulse;

// same as above
while (! (IRpin_PIN & _BV(IRpin))) {
// pin is still LOW
lowpulse++;
delayMicroseconds(RESOLUTION);
if ((lowpulse >= MAXPULSE) && (currentpulse != 0)) {
// printpulses();
sendpulse=currentpulse;
currentpulse=0;
return;
}
}
pulses[currentpulse][1] = lowpulse;

// we read one high-low pulse successfully, continue!
currentpulse++;

}
}

void printpulses(void) {
Serial.println("\n\r\n\rReceived:");
for (uint8_t i = 0; i < currentpulse; i++) {

if (i != 0){
Serial.print("delayMicroseconds(");
Serial.print(pulses[i][0] * RESOLUTION);
Serial.println(");");
}
Serial.print("pulseIR(");
Serial.print(pulses[i][1] * RESOLUTION);
Serial.println(");");
}
delay(1000);
digitalWrite(orangeLedPin, HIGH);
delay(200);
digitalWrite(orangeLedPin, LOW);
delay(200);

/* // Print it in a 'array' format
Serial.println("int IRsignal[] = {");
Serial.println("// ON, OFF (in 10's of microseconds)");
for (uint8_t i = 0; i < currentpulse-1; i++) {
Serial.print("\t"); // tab
Serial.print(pulses[i][1] * RESOLUTION / 10, DEC);
Serial.print(", ");
Serial.print(pulses[i+1][0] * RESOLUTION / 10, DEC);
Serial.println(",");
}
Serial.print("\t"); // tab
Serial.print(pulses[currentpulse-1][1] * RESOLUTION / 10, DEC);
Serial.print(", 0};");
*/
}

// This procedure sends a 38KHz pulse to the IRledPin for a certain # of microseconds.
void pulseIR(long microsecs) {

cli(); // Turn off any background interrupts

while (microsecs > 0) {
// 38 kHz is about 13 microseconds high and 13 microseconds low
digitalWrite(IRledPin, HIGH); // 3 microseconds
delayMicroseconds(10); /* hang out for 10 microseconds,
you can also change this to 9 if its not working */
digitalWrite(IRledPin, LOW); // 3 microseconds
delayMicroseconds(10); /* hang out for 10 microseconds,
you can also change this to 9 if its not working */

// So 26 microseconds altogether
microsecs -= 26;
}

sei(); // Turn them back on
}

void SendIRCode() {

for (uint8_t i = 0; i < sendpulse; i++) {

if (i != 0){
delayMicroseconds(pulses[i][0] * RESOLUTION);
}

pulseIR(pulses[i][1] * RESOLUTION);
}
}

void power_on(){

uint8_t answer=0;

// checks if the module is started
answer = sendATcommand("AT", "OK", 2000);
if (answer == 0)
{
// power on pulse
digitalWrite(onModulePin,HIGH);
delay(3000);
digitalWrite(onModulePin,LOW);

// waits for an answer from the module
while(answer == 0){ // Send AT every two seconds and wait for the answer
answer = sendATcommand("AT", "OK", 2000);
}
}

}

int8_t sendATcommand(char* ATcommand, char* expected_answer, unsigned int timeout){

uint8_t x=0, answer=0;
char response[100];
unsigned long previous;

memset(response, '\0', 100); // Initialice the string

delay(100);

while( Serial.available() > 0) Serial.read(); // Clean the input buffer

if (ATcommand[0] != '\0')
{
Serial.println(ATcommand); // Send the AT command
}


x = 0;
previous = millis();

// this loop waits for the answer
do{
if(Serial.available() != 0){ /* if there are data in the UART input buffer,
reads it and checks for the asnwer */
response[x] = Serial.read();
x++;
if (strstr(response, expected_answer) != NULL)
// check if the desired answer (OK) is in the response of the module
{
answer = 1;
}
}
}while((answer == 0) && ((millis() - previous) < timeout));
// Waits for the asnwer with time out

return answer;
}

But for some reason, the code gets stuck after I press the receive button in the IR shield. And once I remove the 3G shield the IRpin_PIN changes value and the code continues but its useless since the 3G shield is removed. This is where the code gets stuck: while (IRpin_PIN & (1 << IRpin)). There is also a note that I need to connect the digital pin 8 in Arduino to the pin 2 in the 3G shield but I see no difference when I do this.
Can someone help me, please? I have been trying to fix this issue for weeks.


Top
 Profile  
 
 Post subject: Re: Receiving IR commands using 3G module
PostPosted: Tue Mar 07, 2017 8:45 am 

Joined: Mon Sep 28, 2009 12:06 pm
Posts: 11366
Hi rei-valera,

Can you upload some photos showing the connections when you do this:

Code:
NOTE: You have to connect the 3G/GPRS pin 2 to the Arduino UNO digital pin 8 using one small wire. Arduino UNO use this pin to switch on the 3G/GPRS module.


Thanks

Regards


Top
 Profile  
 
 Post subject: Re: Receiving IR commands using 3G module
PostPosted: Wed Mar 08, 2017 4:58 pm 

Joined: Sat Mar 04, 2017 9:06 pm
Posts: 7
Thnk you for your reply! Here is a picture Image


Top
 Profile  
 
 Post subject: Re: Receiving IR commands using 3G module
PostPosted: Thu Mar 09, 2017 8:44 am 

Joined: Mon Sep 28, 2009 12:06 pm
Posts: 11366
Hi rei-valera,

With that hardware, you are connecting the pin 2 of the 3G with the pin2 of the Arduino and de pin 8 of the arduino, making a shortcircuit between them.

You need to bend the pin 2 of the 3G shield to avoid that.

Regards


Top
 Profile  
 
 Post subject: Re: Receiving IR commands using 3G module
PostPosted: Fri Mar 10, 2017 3:51 am 

Joined: Sat Mar 04, 2017 9:06 pm
Posts: 7
Thanks a lot!! It fixed my problem.


Top
 Profile  
 
 Post subject: Re: Receiving IR commands using 3G module
PostPosted: Fri Mar 10, 2017 8:17 am 

Joined: Mon Sep 28, 2009 12:06 pm
Posts: 11366
Hi rei-valera,

You are welcome, good luck with your project!

Regards


Top
 Profile  
 
 Post subject: Re: Receiving IR commands using 3G module
PostPosted: Sat Mar 11, 2017 10:01 pm 

Joined: Sat Mar 04, 2017 9:06 pm
Posts: 7
Hello again!

Even though now I am able to receive IR commands it seems like the 3G shield made the process very slow. When I try to receive commands the commands I receive are very few thus, when I send the IR command it doesn't work. I am attaching some pictures that show the commands I receive when I don't use the 3G shield and a picture of the commands I receive when the 3G shield is attached.
this is what I receive without the 3G shield
Image
this is what I receive with the 3G shield
Image


Top
 Profile  
 
 Post subject: Re: Receiving IR commands using 3G module
PostPosted: Mon Mar 13, 2017 8:34 am 

Joined: Mon Sep 28, 2009 12:06 pm
Posts: 11366
Hi rei-valera,

It seems like you are using the GPRS sim900 code. Can you try with the 3G code?

Regards


Top
 Profile  
 
 Post subject: Re: Receiving IR commands using 3G module
PostPosted: Mon Mar 13, 2017 7:18 pm 

Joined: Sat Mar 04, 2017 9:06 pm
Posts: 7
But the 3G code does not receive IR commands. It just opens a TCP connection and it listens for messages. Once it reads a message it sends some IR commands that are entered manually. I need to record commands from a remote and resend them. And I need the 3G module because I want to control the Arduino from an Android app.

Regards,
Rei


Top
 Profile  
 
 Post subject: Re: Receiving IR commands using 3G module
PostPosted: Wed Mar 15, 2017 9:00 am 

Joined: Mon Sep 28, 2009 12:06 pm
Posts: 11366
Hi rei-valera,

Please try to close the serial communication using serial.end while your are reeceiving IR data.

Regards.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post a new topicPost a reply Page 1 of 2   [ 14 posts ]
Go to page 1, 2  Next


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:


Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Libelium theme based on 610nm Style by Daniel St. Jules of http://www.gamexe.net


© Libelium Comunicaciones Distribuidas S.L. | Terms of use.