Arduino Forum

Using Arduino => Programming Questions => Topic started by: Frederic_Plante on Jul 27, 2013, 05:48 am

Title: Variable conversion (Solved)
Post by: Frederic_Plante on Jul 27, 2013, 05:48 am
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?
Title: Re: Variable conversion
Post by: Frederic_Plante on Jul 27, 2013, 06:21 am
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]);
   
 }

}
Title: Re: Variable conversion
Post by: Frederic_Plante on Jul 27, 2013, 06:51 am
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 ;)
Title: Re: Variable conversion (Solved)
Post by: Frederic_Plante on Jul 27, 2013, 06:56 am
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.
Title: Re: Variable conversion (Solved)
Post by: Trixi on Sep 10, 2013, 05:42 pm
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
Title: Re: Variable conversion (Solved)
Post by: Frederic_Plante on Sep 10, 2013, 07:12 pm
Yeap I found work around. Maybe I can help, what is you dificulty?
Title: Re: Variable conversion (Solved)
Post by: Trixi on Sep 10, 2013, 07:19 pm
Mostly all  :)

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

Doei
Trixi
Title: Re: Variable conversion (Solved)
Post by: Frederic_Plante on Sep 10, 2013, 07:22 pm
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?
Title: Re: Variable conversion (Solved)
Post by: Frederic_Plante on Sep 10, 2013, 07:26 pm
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);
}

Title: Re: Variable conversion (Solved)
Post by: Trixi on Sep 10, 2013, 07:27 pm
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:
Title: Re: Variable conversion (Solved)
Post by: Frederic_Plante on Sep 10, 2013, 08:30 pm
Show me what you have done so far, and we will copy paste from my code.
Title: Re: Variable conversion (Solved)
Post by: Trixi on Sep 10, 2013, 08:45 pm
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
Title: Re: Variable conversion (Solved)
Post by: Frederic_Plante on Sep 10, 2013, 08:49 pm
Ok, mais là you are not gonna use the bluetooth any more or will you?
Title: Re: Variable conversion (Solved)
Post by: Trixi on Sep 10, 2013, 08:50 pm
Its not necessary then, but could be an additional feature (if possible)
Title: Re: Variable conversion (Solved)
Post by: Frederic_Plante on Sep 10, 2013, 09:20 pm
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
Title: Re: Variable conversion (Solved)
Post by: Trixi on Sep 10, 2013, 09:33 pm
No prb of course - Babies rulez  :D (thank god my kids are adults now  8))
Title: Re: Variable conversion (Solved)
Post by: Frederic_Plante on Sep 10, 2013, 09:38 pm
Here, this is the wifi server  that can be found at: http://arduino.cc/en/Tutorial/WiFiChatServer (http://arduino.cc/en/Tutorial/WiFiChatServer)

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

char ssid[] = "yourNetwork"; //  your network SSID (name)
char pass[] = "secretPassword";    // your network password (use for WPA, or use as key for WEP)

int keyIndex = 0;            // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;

WiFiServer server(23);

boolean alreadyConnected = false; // whether or not the client was connected previously

void setup() {
 //Initialize serial and wait for port to open:
 Serial.begin(9600);
 while (!Serial) {
   ; // wait for serial port to connect. Needed for Leonardo only
 }
 
 // check for the presence of the shield:
 if (WiFi.status() == WL_NO_SHIELD) {
   Serial.println("WiFi shield not present");
   // don't continue:
   while(true);
 }
 
 // attempt to connect to Wifi network:
 while ( status != WL_CONNECTED) {
   Serial.print("Attempting to connect to SSID: ");
   Serial.println(ssid);
   // Connect to WPA/WPA2 network. Change this line if using open or WEP network:    
   status = WiFi.begin(ssid, pass);

   // wait 10 seconds for connection:
   delay(10000);
 }
 // start the server:
 server.begin();
 // you're connected now, so print out the status:
 printWifiStatus();
}


void loop() {
 // wait for a new client:
 WiFiClient client = server.available();


 // when the client sends the first byte, say hello:
 if (client) {
   if (!alreadyConnected) {
     // clead out the input buffer:
     client.flush();    
     Serial.println("We have a new client");
     client.println("Hello, client!");
     alreadyConnected = true;
   }

   if (client.available() > 0) {
     // read the bytes incoming from the client:
     char thisChar = client.read();
     // echo the bytes back to the client:
     server.write(thisChar);
     // echo the bytes to the server as well:
     Serial.write(thisChar);
   }
 }
}


void printWifiStatus() {
 // print the SSID of the network you're attached to:
 Serial.print("SSID: ");
 Serial.println(WiFi.SSID());

 // print your WiFi shield's IP address:
 IPAddress ip = WiFi.localIP();
 Serial.print("IP Address: ");
 Serial.println(ip);

 // print the received signal strength:
 long rssi = WiFi.RSSI();
 Serial.print("signal strength (RSSI):");
 Serial.print(rssi);
 Serial.println(" dBm");
}



Try it, tel me if it work and how mutch memory it use. Then post it back with the modification that you made to make it work. From there ill add some stuff to make it talk to the NFR module.
Title: Re: Variable conversion (Solved)
Post by: Trixi on Sep 10, 2013, 09:44 pm
Thx, i will try it -but how to connect the NFR to the SPI ?
Like described in the MIRF-Demo ?
Title: Re: Variable conversion (Solved)
Post by: Frederic_Plante on Sep 10, 2013, 09:47 pm
Don't NRF just now, one step at the time, for now Try the wifi chat server.

Cause I know the NRF part will work right away so dont worry about that just yet. Let's get starting by the unknown part.
Title: Re: Variable conversion (Solved)
Post by: Frederic_Plante on Sep 10, 2013, 09:49 pm
Your working on Uno and this WIFI shield right? http://arduino.cc/en/Main/ArduinoWiFiShield (http://arduino.cc/en/Main/ArduinoWiFiShield)
Title: Re: Variable conversion (Solved)
Post by: Trixi on Sep 10, 2013, 09:52 pm
No, that was my intention at all -to get connected to my WLAN only with the NFR24L01+  ;)
Title: Re: Variable conversion (Solved)
Post by: Frederic_Plante on Sep 10, 2013, 09:53 pm
If it's the case you will set the NRF device on pin 11, 12, 13 for the SPI. And you will have to:

Code: [Select]

Mirf.cePin = 9;
Mirf.csnPin = 8;


Since your WIFI shieild is using pin 10 as you can see here:

(http://arduino.cc/en/uploads/Main/ArduinoWiFiShieldPinUseDescribed_2.jpg)
Title: Re: Variable conversion (Solved)
Post by: Frederic_Plante on Sep 10, 2013, 10:00 pm
So basicaly like the picture at the bottom:
Title: Re: Variable conversion (Solved)
Post by: Trixi on Sep 10, 2013, 10:02 pm
No, that was my intention at all -to get connected to my WLAN only with the NFR24L01+ and without the WiFi shield  ;)
Title: Re: Variable conversion (Solved)
Post by: Frederic_Plante on Sep 10, 2013, 10:03 pm
Ok so how do you connect to your WIFI Lan if not with the WIFI shield? Do you connect using a RJ-45 cable?
Title: Re: Variable conversion (Solved)
Post by: Frederic_Plante on Sep 10, 2013, 10:05 pm
Oh you want to connect to your WIFI router with the NRF device? Forget about that. You would have to find the full WIFI connect procédure and even the i'm not sure the chanel match
Title: Re: Variable conversion (Solved)
Post by: Trixi on Sep 10, 2013, 10:05 pm
Oh, i thought i was possible following this example:
http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo (http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo)
Title: Re: Variable conversion (Solved)
Post by: Frederic_Plante on Sep 10, 2013, 10:10 pm
Yeah I see what you mean, but no. you can make a network, like WIFI, but not WIFI as such. it's more complicated then just a frequency match.

Even if you manage to fit the full procédure, I don't think there will be any mémory left to work with in you Arduino after that.
Title: Re: Variable conversion (Solved)
Post by: Trixi on Sep 10, 2013, 10:13 pm
=(
i dont want to buy the expensive WiFi Shield, so i searched for alternatives and thought, this could work.

So thanks a lot for your help (your Karma got a "hit")  :)
Title: Re: Variable conversion (Solved)
Post by: Frederic_Plante on Sep 10, 2013, 10:40 pm
Without the will to make a publicity for a particular product or place, have you consider product like those:

http://www.aliexpress.com/item/2-4G-Wireless-Module-TLG10UA03-Embedded-Uart-Wifi-Module-UART-WiFi-Network-Server-Client-IEEE802-11b/988481350.html (http://www.aliexpress.com/item/2-4G-Wireless-Module-TLG10UA03-Embedded-Uart-Wifi-Module-UART-WiFi-Network-Server-Client-IEEE802-11b/988481350.html)

It's not to expensive, it's got ip stacking built in and all
Title: Re: Variable conversion (Solved)
Post by: Trixi on Sep 10, 2013, 10:59 pm
Yes, i found it here and i  was happy (a few seconds....).
http://www.ebay.de/itm/UART-WiFi-Server-Client-Module-Kits-Arduino-Compatible-/130542318415?pt=LH_DefaultDomain_0&hash=item1e64edb34f

Then i read further and saw, that this also uses the WiFi Shield...but has this shield some special parts on it for WiFi connection or leads it simply out the depending pins ?
Title: Re: Variable conversion (Solved)
Post by: Frederic_Plante on Sep 10, 2013, 11:04 pm
This is just like your BlueTooth device in the way that it use a Serial port. You will use a virtual Serial port in the case of a Uno or one of the 3 hardware Serial port in the case of a Méga or Due.

It does not use the Wifi Shield at all, it's a module. It could be link to your computer with the wire you see beside the device to the USB port, but it can also be connected directly to your Arduino, or other micro-controler, on the Serial port.

It's a neet device. I like it.
Title: Re: Variable conversion (Solved)
Post by: Trixi on Sep 10, 2013, 11:11 pm
Aha, OK - now i understand that part.
USB to Serial and USB to TTL - sticks i have several here, so i think i order that piece from your other link...only 16 bucks (thats about 12 EUR for me) + some shipping seems OK then....
Title: Re: Variable conversion (Solved)
Post by: Frederic_Plante on Sep 10, 2013, 11:13 pm
Nah is't 16$ shipping included ;)

Here's the pin out just incase you ever need it

And you can just use one of you WiFi antenna from a old WiFI PCI card that hang out in you closet. It's cut for 2,4ghz and it's all it need.
Title: Re: Variable conversion (Solved)
Post by: Trixi on Sep 11, 2013, 01:59 pm
Hoi Frédéric,

have a look on this thread:
http://forum.arduino.cc/index.php?topic=187274.0 (http://forum.arduino.cc/index.php?topic=187274.0)
Title: Re: Variable conversion (Solved)
Post by: Frederic_Plante on Sep 11, 2013, 04:13 pm
Yep it does just like the sample code i put on, probably more developed, it's a nrf enc gateway. It's just like WIFI, but it's not.