Go Down

Topic: Variable conversion (Solved) (Read 14417 times) previous topic - next topic

Frederic_Plante

Jul 27, 2013, 05:48 am Last Edit: Jul 27, 2013, 07:24 am by Frederic_Plante Reason: 1
So im going true this process where I'm getting data from An ENC28J60 device with the Ethercard libraries using the udp listener, and I want then to send that data to an NRF24l01 device using MIRF. The data does get from the network and showed in the serial window no problem, And the sending process from the serial Window to the NRF device and get's to another NRF device and come out of the serial window of the receiving device. So every thing work well in theories, both device work in the same time on one Arduino no problem.

However, I have a small variable conversion problem, Here is the code:

Code: [Select]
#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>
#include <EtherCard.h>
#include <IPAddress.h>

#define STATIC 0

uint8_t buf[32];

static byte mymac[] = { 0x70,0x69,0x69,0x2D,0x30,0x31 };
byte Ethernet::buffer[500];



void setup(){

 Serial.begin(9600);

 Mirf.cePin = 10;
 Mirf.csnPin = 9;
 Mirf.spi = &MirfHardwareSpi;
 Mirf.init();
 Mirf.setRADDR((byte *)"aurx0");
 Mirf.payload = 32;
 Mirf.channel = 50;
 Mirf.config();
 
 Serial.println("NRF libraries initialised ... ");

 ether.begin(sizeof Ethernet::buffer, mymac);
 ether.dhcpSetup();
 ether.printIp("IP:  ", ether.myip);
 ether.printIp("GW:  ", ether.gwip);
 ether.printIp("DNS: ", ether.dnsip);
 ether.udpServerListenOnPort(&udpSerialPrint, 1337);
 ether.udpServerListenOnPort(&udpSerialPrint, 42);

 Serial.println("ENC libraries initialised ... ");

}

void loop(){
 
 if (Mirf.dataReady()){nrfEvent();}
 ether.packetLoop(ether.packetReceive());

}
 
void serialEvent() {

 while (Serial.available()) {
   
    buf[1] = (char)Serial.read();
   Mirf.setTADDR((byte *)"aurx1");
   Mirf.send((byte *) buf);
   while(Mirf.isSending()){  }
   
 }

}  

void nrfEvent() {
 
    Mirf.getData((byte *) buf);
    Serial.print ((char)buf[1]);

}  

void udpSerialPrint(word port, byte ip[4], const char *data, word len) {
 
 Mirf.setTADDR((byte *)"aurx1");
 Mirf.send((byte*)&data);                    /// <<<<<-----The problem is here
 while(Mirf.isSending()){  }
 Serial.print(data);
 
}


When I connect the computer by using the UDP Processing example, data goes to Arduino, come out no problem, And Arduino does send some stuff true the NRF Device, but Only bogus character, always the same, or space character come out the other end of the NRF. But it always send the right amount of character. If I simply send from the serial window  true the NRF, it will work.

Any one has a idea?
As we fight our way northward into the great unknown, only that one thing remains certain...

Frederic_Plante

#1
Jul 27, 2013, 06:21 am Last Edit: Jul 27, 2013, 07:25 am by Frederic_Plante Reason: 1
Maybe this can help,

In the receiver program, on the other side of the NRF connection, I've remove the (char) int front of the variable buf[1]  so it look like this:

Code: [Select]
Serial.print (buf[1]);

instead of:

Code: [Select]
Serial.print ((char)buf[1]);

And now only the number "1", instead of the space character, come out of the serial window.

It's not usign the same libraries as the sender, but since it's communicating on the same channel, and considering that the data goes true when I'm not trying to get it true the UDP link, it doesn't really matter.
   

Code: [Select]
#include <NRF24.h>
#include <SPI.h>

NRF24 nrf24(10, 9);
uint8_t buf[32];
uint8_t len = sizeof(buf);

void setup()
{
 Serial.begin(9600);
 while (!Serial)
   ;
 nrf24.init();
 nrf24.setChannel(50);
 nrf24.setThisAddress((uint8_t*)"aurx1", 5);
 nrf24.setPayloadSize(32);
 nrf24.setRF(NRF24::NRF24DataRate2Mbps, NRF24::NRF24TransmitPower0dBm);
 nrf24.spiWriteRegister(NRF24_REG_1D_FEATURE, NRF24_EN_DYN_ACK);
 nrf24.powerUpRx();
 Serial.println("initialised");
}

void loop(){
 
 nrfEvent();

}

void serialEvent() {

 while (Serial.available()) {
   
   buf[1] = (char)Serial.read();
   nrf24.setTransmitAddress((uint8_t*)"aurx0", 5);
   nrf24.powerUpTx();
   nrf24.send(buf, sizeof(buf));
   nrf24.waitPacketSent();
   nrf24.powerUpRx();
 
 }

}

void nrfEvent() {
 
 if (nrf24.recv(buf, &len)){
    uint8_t i;
    Serial.print (buf[1]);
   
 }

}
As we fight our way northward into the great unknown, only that one thing remains certain...

Frederic_Plante

#2
Jul 27, 2013, 06:51 am Last Edit: Jul 27, 2013, 07:23 am by Frederic_Plante Reason: 1
Here is the Awnser:

Code: [Select]
void udpSerialPrint(word port, byte ip[4], const char *data, word len) {
 
 buf[1] = (char)*data;                                   // <<--- Added
 Mirf.setTADDR((byte *)"aurx1");
 Mirf.send((byte *)buf);                          // <<--- Changed
 while(Mirf.isSending()){ ; }
 Serial.print(data);
 
}


Hope it will help lot of people ;)
As we fight our way northward into the great unknown, only that one thing remains certain...

Frederic_Plante

Now it's time to make it fully bidirectional.

I saw that lot of people look for the answer thank you all for trying.

Well it's late, Ill keep some fun for tomorrow.
As we fight our way northward into the great unknown, only that one thing remains certain...

Trixi

Hoi Fredric,

did you have some fun ?
And works it bidirectional ?

I am starting to connect myBoard to may WLAN, but i am srtuck in the details  :smiley-roll:

Doei
Trixi
Dum spiro, spero - if you find my posting helpful, please click my Karma :)

Frederic_Plante

Yeap I found work around. Maybe I can help, what is you dificulty?
As we fight our way northward into the great unknown, only that one thing remains certain...

Trixi

Mostly all  :)

Did you have a simple sample code to connect to a wlan and have i to use this WiFishield ?

Doei
Trixi
Dum spiro, spero - if you find my posting helpful, please click my Karma :)

Frederic_Plante

Are you using the official wifi shield? Cause if you are, you are gonna have lot less trouble then I did, but I won't have a direct code to propose, since i'm using some thing else.

And also, what do you plan to do exactly? a simple repeater?
As we fight our way northward into the great unknown, only that one thing remains certain...

Frederic_Plante

#8
Sep 10, 2013, 07:26 pm Last Edit: Sep 10, 2013, 07:34 pm by Frédéric_Plante Reason: 1
I got some thing that receive UDP and send TCP for practical reason maybe it can help but it's for ENC28J60 network board so it would need some adaptation.

It use Mirf instead of the NRF24 libraries cause Mirf is compatible at the level of the spi driver with the Ethercard Librairies.

So this is fully bi-directionnal. It listen on the Serial, NRF and the ENC devices and when it receive something from one it send it to the other.

Work super well. NRF use Pin 9 ans 10 and listen to chanel 50, ENC use Pin 8.

Here have fun:

Code: [Select]

// By Frédéric Plante
#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>
#include <EtherCard.h>
#include <IPAddress.h>

#define STATIC 0
#define FEED    "10002"
#define APIKEY  "bXkPFCiYm57f7flLyD86bm0HK3TXsfuQF-Jeyh3HeMg"

uint8_t buf[32];
static byte mymac[] = { 0x70,0x69,0x69,0x2D,0x30,0x31 };
byte Ethernet::buffer[500];
Stash stash;
String tcpsendstring ="";
unsigned long timer = 0;
char website[] PROGMEM = "FredTouch";

void setup(){

 Serial.begin(9600);

 Mirf.cePin = 10;
 Mirf.csnPin = 9;
 Mirf.spi = &MirfHardwareSpi;
 Mirf.init();
 Mirf.setRADDR((byte *)"aurx0");
 Mirf.payload = 32;
 Mirf.channel = 50;
 Mirf.config();
 Serial.println("NRF libraries initialised ... ");
 Serial.println();

 ether.begin(sizeof Ethernet::buffer, mymac);
 ether.dhcpSetup();
 ether.printIp("IP: ", ether.myip);
 ether.printIp("GW: ", ether.gwip);
 ether.printIp("DNS: ", ether.dnsip);
 Serial.println();
 Serial.println("ENC libraries initialised ... ");
 Serial.println();
 ether.dnsLookup(website);
 ether.hisport =10002;
 ether.printIp("Found Server: ", ether.hisip);
 Serial.println();
 Serial.println("Local serveur listening ");
 ether.udpServerListenOnPort(&udpSerialPrint, 1337);
 
}

void loop(){

 if (Mirf.dataReady()){nrfEvent();}
 ether.packetLoop(ether.packetReceive());

}

void serialEvent() {

 while (Serial.available()) {

   buf[1] = (char)Serial.read();
   Mirf.setTADDR((byte *)"aurx1");
   Mirf.send((byte *) buf);
   while(Mirf.isSending()){ }
   tcpsendstring=tcpsendstring +(char) buf[1];
 }
 TCPSEnd();
 
}

void nrfEvent() {

 Mirf.getData((byte *) buf);
 Serial.print ((char)buf[1]);
 tcpsendstring=tcpsendstring +(char) buf[1];
 if ((char) buf[1]== '\n'){TCPSEnd();}
}

void udpSerialPrint(word port, byte ip[4], const char *data, word len) {
 
 for (int nn =0; nn< len;nn++){
   
   buf[1] = (char)data[nn];
   Mirf.setTADDR((byte *)"aurx1");
   Mirf.send((byte*)buf);
   while(Mirf.isSending()){ ; }
 }
 Serial.print(data);

}

void TCPSEnd(){

byte sd = stash.create();
   
   stash.println((long) millis());
   stash.println(tcpsendstring);
   tcpsendstring="";
   stash.save();
   
   Stash::prepare(PSTR("PUT http://$F/v2/feeds/$F.csv HTTP/1.0" "\r\n"
                       "Host: $F" "\r\n"
                       "X-PachubeApiKey: $F" "\r\n"
                       "Content-Length: $D" "\r\n"
                       "\r\n"
                       "$H"),
           website, PSTR(FEED), website, PSTR(APIKEY), stash.size(), sd);

   ether.udpServerPauseListenOnPort(1337);
   ether.tcpSend();
   ether.udpServerResumeListenOnPort(1337);
}

As we fight our way northward into the great unknown, only that one thing remains certain...

Trixi

have no Wifi shield yet but a NRF24L01+ - Modul.
I want to transfer datas via the Wlan to a PC software - that transfer isnt a problem with Bluetooth, but i want to use the wlan instead.
But i have no experiences with that and reading the different threads and postings let me get more confused  :smiley-eek-blue:
Dum spiro, spero - if you find my posting helpful, please click my Karma :)

Frederic_Plante

Show me what you have done so far, and we will copy paste from my code.
As we fight our way northward into the great unknown, only that one thing remains certain...

Trixi

Here my code to connect via BlueTooth (with a SmartPhone and an App) and send and receive data (as bytes).

Code: [Select]

#include <SoftwareSerial.h>

   SoftwareSerial softSerial(8, 7); // RX, TX   Setup to use pin 10 and 11 of the Arduino to connect to the bluetooth module
   String command = ""; // Stores response of bluetooth device
                 
   void setup()  
   {
     // Open serial communications and wait for port to open:
     Serial.begin(9600);
    // Set the Arduino's serial port so we can use the Arduino Serial Monitor (top right 'spyglas' in the Arduino software) Then we can type stuff live and send it over bluetooth.    
   softSerial.begin(9600);
    // SoftwareSerial port is the one for sending and receiving over Bluetooth
   }

   void loop() // the start of the loop
   {
       // Read device output if available.  
       while(softSerial.available()) { // While there is more to be read, keep reading.
       command += (char)softSerial.read();
       delay(2000); // gives it a chance to get all the characters.
       }          
   
       if(command != ""){              // so if there is something in the string 'command' If command doesn't equal "" (if its not empty!) then print it to the
         Serial.println(command);  // Arduino's Serial Monitor and what's in 'command' is what has been received over the Bluetooth link.
       }                                                    

       if(command == "A"){  
        // the first thing that happens upon connect is that BTInterface sends the string 'btinterface' to let the device know its connected.
         softSerial.println("A");  // sending  'screen1' will cause BTInterface to show screen1
         Serial.println("ToggleButton ON");          
         delay(1000); // wait a second to give it a chance to get all the characters.
       }

       if(command == "a"){        
         softSerial.println("a");  
         Serial.println("ToggleButton OFF");  
       }
       // this is how easy it is to use, wait for a command like b1 (for button one) etc. then
       // act on that command and send something back over the bluetooth link.  Here if BTInterface sends 'ping' the Arduino sends back 'pong'
           
       command = ""; // Empty the command string so we can start again.
     
      // Read user input if available. This bit is for debugging, if you write anything into the Arduino Serial Monitor
      if (Serial.available()){          //  and press Return it will be sent over the Bluetooth link.
         softSerial.write(Serial.read());  // send whatever was written over the bluetooth link
      }            
           
   }// the end of the loop
Dum spiro, spero - if you find my posting helpful, please click my Karma :)

Frederic_Plante

Ok, mais là you are not gonna use the bluetooth any more or will you?
As we fight our way northward into the great unknown, only that one thing remains certain...

Trixi

Its not necessary then, but could be an additional feature (if possible)
Dum spiro, spero - if you find my posting helpful, please click my Karma :)

Frederic_Plante

Congrat.

Gimme a few hour, i'll check what you got so far and plug a few thing on it. Baby is crying I got to put priority to first plan, i'll be back
As we fight our way northward into the great unknown, only that one thing remains certain...

Go Up