Hi I'm working on a code which freezes randomly.. Pls help me?
Thanks
https://cdn.instructables.com/FPR/ZG1M/JF8IV0I1/FPRZG1MJF8IV0I1.LARGE.jpg
#include <UIPEthernet.h> // Used for Ethernet
#include <string.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include "DigitalIO.h"
#undef SOFT_SPI_MISO_PIN
#undef SOFT_SPI_MOSI_PIN
#undef SOFT_SPI_SCK_PIN
#define SOFT_SPI_SCK_PIN 7
#define SOFT_SPI_MOSI_PIN 8
#define SOFT_SPI_MISO_PIN 9
RF24 radio(5, 6); // CE, CSN
const byte address[6] = "00001";
struct RFdataPacket {
int temp, hum, air;
char sensorId;
};
RFdataPacket dataReceived = { 0, 0, 0, 0 };
byte mac[] = { 0x54, 0x34, 0x41, 0x30, 0x30, 0x31 };
byte ip[] = {10, 152, 16, 166};
byte subnet[] = {255, 255, 248, 0};
byte gateway[] = {10, 152, 16, 1};
byte dnss[] = {10, 150, 0, 5};
int ledout = 0;
EthernetClient client;
char server[] = "10.152.16.142"; // IP Adres (or name) of server to dump data to
void setup() {
Ethernet.begin(mac, ip, dnss, gateway, subnet);
delay(1000);
radio.begin();
radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_MAX);
radio.startListening();
}
void loop() {
if (radio.available())
{
radio.read(&dataReceived, sizeof(RFdataPacket));
int s = dataReceived.sensorId;
int t = dataReceived.temp;
int a = dataReceived.air;
int h = dataReceived.hum;;
String responseFromPHPServer = "";
if (client.connect(server, 80))
{
client.flush();
client.print( "GET /airwatcher/sensorupdate.php?");
client.print("id=");
client.print(s);
client.print("&temp=");
client.print(t);
client.print("&hum=");
client.print(h);
client.print("&air=");
client.print(a);
client.println( " HTTP/1.1");
client.print( "Host: " );
client.println(server);
client.println( "Connection: close" );
client.println();
client.println();
delay(2000);
client.stop();
}
}
}
Take a look at the read function in the RF24 library you have installed. In the version I was looking at, the payload_size overwrites the buffer size passed in if it's less than 32 (which yours is).
I suspect you're getting buffer overrun on occasion.
That String that does nothing can go too. If you cut the code down to show this as a reduced sketch that illustrates the problem and the String does get used in the full version, the String itself may be your issue.
I reduced the packet size and started to test it... I hope that it will work
#include <UIPEthernet.h> // Used for Ethernet
#include <string.h>
#include <RF24.h>
#include "DigitalIO.h"
#undef SOFT_SPI_MISO_PIN
#undef SOFT_SPI_MOSI_PIN
#undef SOFT_SPI_SCK_PIN
#define SOFT_SPI_SCK_PIN 7
#define SOFT_SPI_MOSI_PIN 8
#define SOFT_SPI_MISO_PIN 9
RF24 radio(5, 6); // CE, CSN
const byte address[6] = "00001";
byte data[4];
byte mac[] = { 0x54, 0x34, 0x41, 0x30, 0x30, 0x31 };
byte ip[] = {10,152,16,166};
byte subnet[] = {255,255,248,0};
byte gateway[] = {10,152,16,1};
byte dnss[] = {10,150,0,5};
EthernetClient client;
char server[] = "10.152.16.142"; // IP Adres (or name) of server to dump data to
unsigned long previousMillis = 0;
const long interval = 10000; // 15 mins. 900000;
void setup() {
Serial.begin(9600);
delay(2000);
Ethernet.begin(mac, ip, dnss, gateway, subnet);
Serial.print("Local IP:");
Serial.println(Ethernet.localIP());
radio.begin();
radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_MAX);
radio.startListening();
Serial.println("Started");
}
void loop() {
if (radio.available())
{
Serial.println("Data Recieved....");
radio.read(&data, 4);
int s = data[0];
int t = data[1];
int a = data[2];
int h = data[3];
Serial.println("Sensor ID : ");
Serial.println(s);
Serial.println("Temp : ");
Serial.println(t);
Serial.println("Air : ");
Serial.println(a);
Serial.println("Humidity");
Serial.println(h);
boolean insertedToPHPServer = false;
while (!insertedToPHPServer)
{
String responseFromPHPServer = "";
if (client.connect(server, 80)) {
//Serial.println("-----> Connected");
client.flush();
client.print( "GET /airwatcher/sensorupdate.php?");
client.print("id=");
client.print(s);
client.print("&temp=");
client.print(t);
client.print("&hum=");
client.print(h);
client.print("&air=");
client.print(a);
client.println( " HTTP/1.1");
client.print( "Host: " );
client.println(server);
client.println( "Connection: close" );
client.println();
client.println();
while (client.connected() || client.available())
{
char c = client.read();
Serial.print(c);
if (c == 'Q')
{
insertedToPHPServer = true;
}
}
client.stop();
}
}
}
}