Hi there,
i've just posted in the german subforum, but i haven't found a solution yet - so i thought, i should write another post in the english forum's.
I've programmed ATTiny84-Based Sensor-Nodes, that send their data via 433 Mhz-Transmitter to an "Arduino Ethernet Base-Station". The Ardu writes this data via Ethernet to a PHP-Webscript, which saves them to a MySQL-Database.
I programmed 2 Sketches for testing: One, that read's the 433-packets and print them on Serial Monitor, and a second, that transmits a demo-string to the webscript. Both works fine - but if i try to put them together, the arduino seems to hang up.
I've optimized my code, because i thought it could be a ram-issue; but i think the left ram of 1,1 kb should be enough to transmit the data via GET to my WebScript.
My Problem:
The Arduino Ethernet receive a data-packet. This Data will be converted vom Char to String, and passed to my connect()-function, which writes it to the Webscript. In Serial Monitor, i see the last output of the actual free ram (1107), and then nothing happens anymore.
The Webscript and the function to write it to there seems to be ok; because if i commet out some VirtualWire-functions, the data is written correctly to my database.
Here's my code; sorry for the german comments:
#include <VirtualWire.h>
#include <SPI.h>
#include <Ethernet.h>
// PIN, an den der 433 Mhz Empfänger angeschlossen ist
int RX_Pin = 8;
int TX_Pin = 9; // Einen leeren PIN angeben, da die VirtualWire-Lib sonst einen Pin belegt, der für den Ethernet-Chip genutzt wird
int PTT_Pin = 7; //
byte mac[] = {
0x00, 0xAB, 0xCB, 0xCD, 0xDE, 0x02 };
byte ip[] = { 192,168,2,177 };
byte subnet[] = { 255,255,255,0 };
byte gateway[] = { 192,168,2,1 };
byte dns1[] = { 192,168,2,1 };
char server[] = "www.3bm.de";
EthernetClient client;
void setup() {
Serial.begin(9600);
Serial.print(F("Setup LAN ... "));
// give the Ethernet shield a second to initialize:
delay(1000);
Ethernet.begin(mac, ip, dns1, gateway, subnet);
Serial.println("ok");
// VirtualWire initialisieren
vw_setup(2000); // Bits per sec
vw_set_ptt_pin(PTT_Pin);
vw_set_rx_pin(RX_Pin); // We will be receiving on pin 4 i.e the RX pin from the module connects to this pin.
vw_set_tx_pin(TX_Pin);
vw_rx_start(); // Start the receiver
}
void loop(){
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
// Daten-Variable, die die empfangenen Daten hält
char SensorDataTemp[VW_MAX_MESSAGE_LEN+1];
// Daten-Variable, zum Übergeben der Daten an die WebRequest-Funktion
String SensorData;
// Empfang von Daten prüfen
if (vw_get_message(buf, &buflen)) {
int i;
// Datagramm zusammensetzen
for (i = 0; i < buflen; i++) {
// Serial.print((char)buf[i]); // the received data is stored in buffer
SensorDataTemp[i] = (char)buf[i];
}
// Char-Variable terminieren
SensorDataTemp[VW_MAX_MESSAGE_LEN] = '\0';
// Char to String-Konvertierung zur Übergabe an die connect()-Funktion
SensorData = SensorDataTemp;
// Console-Output zum Debuggen
Serial.print(F("Daten empfangen: "));
Serial.print(SensorData);
Serial.println();
Serial.println(freeMemory());
Serial.println(F("Daten schicken..."));
connect(SensorData);
Serial.println(F("Daten geschickt!"));
}
delay(30000);
}
void connect(String payload){
Serial.println(freeMemory());
if (client.connect(server, 80)) {
Serial.print(F("Anfrage senden an "));
Serial.print(server);
client.print(F("GET /xxx/index.php?data="));
client.print(payload);
client.print(F("&key=xxx HTTP/1.0"));
client.println(F("HOST: 3bm.de"));
client.println();
client.stop();
Serial.println();
Serial.println(F("Daten uebermittelt."));
Serial.println();
}
else {
// kf you didn't get a connection to the server:
Serial.println(F("!!! Verbindung fehlgeschlagen"));
}
}
int freeMemory()
{
extern int __heap_start, *__brkval;
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}
The problem occures here:
void connect(String payload){
Serial.println(freeMemory());
if (client.connect(server, 80)) {
The last serial message i receive is the freeMemory() of 1107; then nothing happens. It seems, that the arduino is hang up as he try to client.connect() the server.
The PINs for TX and PTT are not connected to arduino; i've just added them, because the virtual wire-lib initialize them with pins, that are relevant for the ethernet-chip-communication (10-13).
Has someone any idea, how i get around this issue?