Jump to content
Sign in to follow this  
Bernard

TM4C129 +nRF24l01 web server problem

Recommended Posts

Hi,

 

I would like to centralize several RF sensors on TM4C129 and make a web server,  so I write this simple sketch ( may be too simple)

 

But, Serial.println("inbuf");  doesn't work and client.print(inbuf); works once.

/* Tiva launchpad connected TM4C129
   nRF24L01 RX
   SPI module 1
   26/06/2014
*/
#include <Ethernet.h>
#include <Enrf24.h>
#include <nRF24L01.h>
#include <string.h>
#include <SPI.h>

byte mac[] = { 0x00, 0x1A, 0xB6, 0x02, 0xB1, 0x27 };
byte ip[] = { 192,168,0, 13 };

EthernetServer server(80);

Enrf24 radio(PE_0,PE_2,PE_1);  // CE, CSN, IRQ + SCK/MOSI/MISO ==> PB_5/PE_4/PE_5

const uint8_t rxaddr[] = { 0xF0,0xF0,0xF0,0xF0,0xE1 };

unsigned long prev_time;

void setup()
{
  Serial.begin(9600);
  Serial.flush();
  Ethernet.begin(mac, ip);
  server.begin();
  SPI.setModule(1);
  SPI.begin();
  SPI.setDataMode(SPI_MODE0);
  SPI.setBitOrder(1);
  
  radio.begin();  // Defaults 1Mbps, channel 0, max TX power
  dump_radio_status_to_serialport(radio.radioState());  
  radio.setRXaddress((void*)rxaddr);  
  radio.enableRX();  // Start listening
  delay(100);
}

//**************************** Main loop *************************************** 

void loop() {
  char inbuf[33]; 
  
  delta_set();
  Serial.println("Loop begin"); // debug
  
  while (!radio.available(true)&& delta_get()<1000);
   
  if (radio.read(inbuf)) {
      Serial.println("inbuf");    
  }  
 
 //Listen to client (browser WEB)

  delay(100);
  
  EthernetClient client = server.available();
  if (client) {
    
    boolean currentLineIsBlank = true;

    while (client.connected()) {
      if (client.available()) {
                      // send a standard http response header
                    client.println("HTTP/1.1 200 OK");
                    client.println("Content-Type: text/html");
                    client.println("Connection: close");
                    client.println();
                    client.println("<!DOCTYPE html>");
                    client.println("<html>");
                    client.println("<head>");
                    client.println("<title>Domotic</title>");
                    client.println("<meta http-equiv=\"refresh\" content=\"30\" >");
                    client.println("</head><body bgcolor=\"#000000\" text=\"#ffffff\" link=\"#0000ff\" vlink=\"#0000ff\" alink=\"#ff0000\">");
                    client.print("<h1>TM4C129 web server</h1><h2>Temperature</h2>");
                    client.print("Humidity and temperature = ");
                    client.print(inbuf);  
                    client.print("<br>");
                    client.println("(update 30s)");
                    client.println("<br>");                    
                    client.println("</html>");
                    client.stop();
                } 
    }
  } 
 Serial.println("End loop"); // debug
 
}

//************************** End main loop  ***************************************** 



void delta_set() {
  prev_time = millis();
}

// TimeOut  
unsigned long delta_get() {
  unsigned long time;
  unsigned long delta;

  time = millis();
  if (time < prev_time) { // TimeOut
    delta = 0xffffffff - prev_time + time + 1;
  } 
  else {
    delta = time - prev_time;
  }
  return delta;
}

void dump_radio_status_to_serialport(uint8_t status)
{
  Serial.print("Enrf24 radio transceiver status: ");
  switch (status) {
    case ENRF24_STATE_NOTPRESENT:
      Serial.println("NO TRANSCEIVER PRESENT");
      break;

    case ENRF24_STATE_DEEPSLEEP:
      Serial.println("DEEP SLEEP <1uA power consumption");
      break;

    case ENRF24_STATE_IDLE:
      Serial.println("IDLE module powered up w/ oscillators running");
      break;

    case ENRF24_STATE_PTX:
      Serial.println("Actively Transmitting");
      break;

    case ENRF24_STATE_PRX:
      Serial.println("Receive Mode");
      break;

    default:
      Serial.println("UNKNOWN STATUS CODE");
  }
}

Any help would be appreciated.

 

Salutations.

Bernard

Share this post


Link to post
Share on other sites

Hi,

 

Sorry for my previous post, better to  forget it .

 

Here is another more clear I hope in order to explain my problem

/*
TM4C129
SPI module 1
*/

#include <Ethernet.h>
#include <Enrf24.h>
#include <nRF24L01.h>
#include <SPI.h>
Enrf24 radio(PE_0,PE_2,PE_1);  // CE, CSN, IRQ + SCK/MOSI/MISO ==> PB_5/PE_4/PE_5
const uint8_t rxaddr[] = { 0xF0,0xF0,0xF0,0xF0,0xE1 };
unsigned long prev_time;
byte mac[] = { 0x00, 0x1A, 0xB6, 0x02, 0xB1, 0x27 };  
IPAddress ip(192,168,0, 13);
EthernetServer server(80); 

void setup(){
  Serial.begin(115200);
  Serial.flush();
  Ethernet.begin(mac, ip);
  server.begin();
  SPI.setModule(1);
  SPI.begin();
  SPI.setDataMode(SPI_MODE0);
  SPI.setBitOrder(1);
  
  radio.begin();  // Defaults 1Mbps, channel 0, max TX power
  dump_radio_status_to_serialport(radio.radioState());  
  radio.setRXaddress((void*)rxaddr);  
  radio.enableRX();  // Start listening
  delay(200);
}  

void loop(){
  char inbuf[48];  
  delta_set();
  Serial.println("Loop begin"); 
  
  while (!radio.available(true)); //&& delta_get()<1000);
   Serial.println("before if");
  if (radio.read(inbuf)) {
      Serial.println(inbuf); 
      Serial.println("if");  
  } 
  
  listenForEthernetClients();
  Serial.println("End Loop"); 
}

void listenForEthernetClients() {
  
  EthernetClient client = server.available();
  if (client) {
    Serial.println("Got a client");
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {   

        char c = client.read();   
        if (c == '\n' && currentLineIsBlank) {
          client.println();
          client.print("degreesC,25"); // just for test
          client.print(", ");
          client.print("Humidity,60"); // idem
          client.print(", ");
          client.print("millibar,1018"); // idem
          break;
        }
        if (c == '\n') {   

          currentLineIsBlank = true;
        }
        else if (c != '\r') {   

          currentLineIsBlank = false;
        }
      }
    }
    delay(1);   


    client.stop();   

  }
}

void dump_radio_status_to_serialport(uint8_t status)
{
  Serial.print("Enrf24 radio transceiver status: ");
  switch (status) {
    case ENRF24_STATE_NOTPRESENT:
      Serial.println("NO TRANSCEIVER PRESENT");
      break;

    case ENRF24_STATE_DEEPSLEEP:
      Serial.println("DEEP SLEEP <1uA power consumption");
      break;

    case ENRF24_STATE_IDLE:
      Serial.println("IDLE module powered up w/ oscillators running");
      break;

    case ENRF24_STATE_PTX:
      Serial.println("Actively Transmitting");
      break;

    case ENRF24_STATE_PRX:
      Serial.println("Receive Mode");
      break;

    default:
      Serial.println("UNKNOWN STATUS CODE");
  }
}
void delta_set() {
  prev_time = millis();
}

// TimeOut  
unsigned long delta_get() {
  unsigned long time;
  unsigned long delta;

  time = millis();
  if (time < prev_time) { // TimeOut
    delta = 0xffffffff - prev_time + time + 1;
  } 
  else {
    delta = time - prev_time;
  }
  return delta;
}

nRF24L01 alone works ok but when I add the Ethernet part I can't get received values.

The loop freeze on while (!radio.available(true)); I don't know why  and I am a bit lost.

Thanks for help.

 

Best regards

 

Bernard

Share this post


Link to post
Share on other sites

@spirillis : thanks for replying.

 

Yes Internet part works when I comment out Enrf24-related items.

 

When I comment out    //Ethernet.begin(mac, ip);    //server.begin(); radio is ok and  I get DHT22 values.

 

Another clue, Resetting the TM4C129 board several times .. sometimes I cans see DHT22 values :

 

copy of terminal with loop debug :

Enrf24 radio transceiver status: DEEP SLEEP <1uA power consumption

Loop begin <---- nada

Enrf24 radio transceiver status: DEEP SLEEP <1uA power consumption

Loop begin <---- nada

Enrf24 radio transceiver status: DEEP SLEEP <1uA power consumption

Loop begin

41.5---25.2 <----- good values    miracle !!! 

if

End Loop

Loop begin

salutations

Edited by brownfox

Share this post


Link to post
Share on other sites

I think it's true, although I added a 10uF cap,  sometimes the program freeze when  server get a client and need more power.

I will try a 47uF.

 

To be continued ...

 

Salutations

Share this post


Link to post
Share on other sites

The server/client freezing is likely due to an issue I found in the demo sketch that you (and I, and most people I suspect) are pulling the ethernet server stuff out of.

The while loop ("while (client.connected())") can only exit if the client sends data. If the client connects but never sends data (FireFox on Windows does this sometimes) it will hang there forever. beyond sending data, it has to send the correct data to make this line return true: "if (c == '\n' && currentLineIsBlank) {", as the break; for the while loop is in there.

 

My soluition was to start a timer when a client connects, and if the client is connected for more than one second, it calls "break;" to exit the while loop.

In future energia versions this will be baked into at least one of the ethernet sketches.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×