Variable conversion (Solved)

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:

#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?

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:

Serial.print (buf[1]);

instead of:

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.

#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]);
    
  }

}

Here is the Awnser:

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 :wink:

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.

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 :roll_eyes:

Doei
Trixi

Yeap I found work around. Maybe I can help, what is you dificulty?

Mostly all :slight_smile:

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

Doei
Trixi

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?

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:

// 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);
}

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 :fearful:

Show me what you have done so far, and we will copy paste from my code.

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

#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

Ok, mais là you are not gonna use the bluetooth any more or will you?

Its not necessary then, but could be an additional feature (if possible)

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

No prb of course - Babies rulez :smiley: (thank god my kids are adults now 8))

Here, this is the wifi server that can be found at: http://arduino.cc/en/Tutorial/WiFiChatServer

#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.

Thx, i will try it -but how to connect the NFR to the SPI ?
Like described in the MIRF-Demo ?

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.

Your working on Uno and this WIFI shield right? http://arduino.cc/en/Main/ArduinoWiFiShield