ello, I have a question/problem about my code.
software:
a simple Labview program to control, read/write UDP signal to the Ethernet shield VMA04, with my labtop.
hardware:
- Arduino mega 2560,
- Velleman Ethernet shield VMA04
- Adafru.it barometric sensor MPL3115A2-IC2
- labtop with fixed IP address 192.168.2.50 (no routers or switches between shield and labtop )
Problem:
my code works fine or 1.5 hours sometimes 2 hours and than the connection is death.
I try to ping it but the message is “Destination host unreachable” or “request timed out”.
most of the time the connection is automatically alive again in about 30 minutes sometimes after 2 hours.
and with a hard reset it works within 1 second.
so I was thinking about a buffer overflow, but I am not skilled enough to see where the problem is.
Can somebody show me to my coding errors or point me in the right direction to start looking
thanks in you in advance.
code:
/*
- UIPEthernet UdpServer example.
- UIPEthernet is a TCP/IP stack that can be used with a enc28j60 based
- Ethernet-shield.
- UIPEthernet uses the fine uIP stack by Adam Dunkels adam@sics.se
-
-----------------
- This UdpServer example sets up a udp-server at 192.168.0.6 on port 5000.
- send packet via upd to test
- Copyright © 2013 by Norbert Truchsess (norbert.truchsess@t-online.de)
- connect pin 10 and 53
*/
#include <SPI.h>
#include <UIPEthernet.h>
#include <Wire.h>
#include <Adafruit_MPL3115A2.h>
Adafruit_MPL3115A2 baro = Adafruit_MPL3115A2();
EthernetUDP udp;
char* data;
int someInt;
char* write_to_labview_lamp_state;
char* write_to_labview_sensor_state = “0”;
int write_to_labview_temp;
int long write_to_labview_press;
const unsigned long interval_cycles = 1000UL; // Time periods of in milliseconds (1000 milliseconds to a second).
// Time variable and constants are always unsigned long
unsigned long previous_time = 0; // Variable holding the timer value so far. One for each “Timer”
int DI_01 = 42; // the number of the pushbutton pin
int DI_02 = 43; // the number of the pushbutton pin
int DI_03 = 44; // the number of the pushbutton pin
int DI_04 = 45; // the number of the pushbutton pin
int DO_04 = 22; //4 relay module D4
int DO_03 = 23; //4 relay module D3
int DO_02 = 24; //4 relay module D2
int DO_01 = 25; //4 relay module D1
// Variables will change:
int DI_01_State = LOW; // the current state of the output pin
int DI_02_State = LOW; // the current state of the output pin
int DI_03_State = LOW; // the current state of the output pin
int DI_04_State = LOW; // the current state of the output pin
void setup() {
Serial.begin(9600);
pinMode(DI_01, INPUT);
pinMode(DI_02, INPUT);
pinMode(DI_03, INPUT);
pinMode(DI_04, INPUT);
pinMode(DO_01, OUTPUT);
pinMode(DO_02, OUTPUT);
pinMode(DO_03, OUTPUT);
pinMode(DO_04, OUTPUT);
// connect pin 10 and 53 to each other to get ethernet shield (velleman VMA04) woriking
uint8_t mac[6] = {0x04,0x02,0x02,0x03,0x04,0x05}; //mac address
Ethernet.begin(mac,IPAddress(192,168,2,60)); //ip address
udp.begin(5000); // port address
Serial.println(“Initialize”);
//Serial.println(success ? “success” : “failed”);
}
void loop() {
//check for new udp-packet:
int size = udp.parsePacket();
if (( (millis () - previous_time) >= interval_cycles ) &&(size > 0)){
char* msg = (char*)malloc(size+1);
int len = udp.read(msg,size+1);
msg[len]=0;
data = msg; //
int someInt = atoi(data) ;
free(msg);
udp.flush();
udp.beginPacket(udp.remoteIP(),udp.remotePort());
Serial.println("Begin_Packet");
// Serial.println(success ? "success" : "failed");
Serial.print("Data_from_Labview = ");
Serial.println (someInt);
//all lamps on
if ( someInt == 1){
digitalWrite (DO_01,LOW);
delay (1000);
digitalWrite (DO_02,LOW);
delay (1000);
digitalWrite (DO_03,LOW);
delay (1000);
digitalWrite (DO_04,LOW);
write_to_labview_lamp_state = “All lamps ON”;
msg = 0;
}
//all lamps off
if ( someInt == 2){
digitalWrite (DO_04,HIGH);
delay (1000);
digitalWrite (DO_03,HIGH);
delay (1000);
digitalWrite (DO_02,HIGH);
delay (1000);
digitalWrite (DO_01,HIGH);
write_to_labview_lamp_state = “All lamps OFF”;
msg = 0;
}
//received sensor signal and reset
if ( someInt == 10){
write_to_labview_sensor_state = “0”;
}
//elektrical cabinet is to hot
if (DI_04_State = digitalRead(DI_04)){
write_to_labview_sensor_state = “Elektrical cabinet is to hot”;
}
//Pir censor 01 is active
if (DI_03_State = digitalRead(DI_03)){
write_to_labview_sensor_state = “PIR sensor_01 is active”;
}
//Pir censor 02 is on
if (DI_02_State = digitalRead(DI_02)){
write_to_labview_sensor_state = “Sensor fence_01 is active”;
}
//Censor 03 is on
if (DI_01_State = digitalRead(DI_01)){
write_to_labview_sensor_state = “PIR sensor_02 is active”;
}
if (!baro.begin()) {
Serial.println(“Couldnt find sensor”);
delay(1000);
return;
}
float pascals = baro.getPressure();
// Our weather page presents pressure in Inches (Hg)
// Use http://www.onlineconversion.com/pressure.htm for other units
//Serial.print(pascals/3377); Serial.println(" Inches (Hg)");
Serial.print("Pressure ");
Serial.print(pascals0.01);
Serial.println(" mbar");
write_to_labview_press = (pascals0.01*100) ;
//float altm = baro.getAltitude();
//Serial.print(altm); Serial.println(" meters");
float tempC = baro.getTemperature();
Serial.print("Temp_01 “);
Serial.print(tempC);
Serial.println(” C");
write_to_labview_temp = (tempC100);
//success = write_to_labview ;
udp.print("Lamp_state = ");
udp.println(write_to_labview_lamp_state);
udp.print("Sensor_state = ");
udp.println(write_to_labview_sensor_state);
udp.print("Temp_01 = ");
udp.println(write_to_labview_temp);
udp.print("Pressure = ");
udp.println(write_to_labview_press);
//finish reading this packet:
//udp.flush();
//udp.endPacket();
udp.endPacket();
Serial.println(“endPacket”);
//Serial.println(success ? “success” : “failed”);
udp.stop();
//restart with new connection to receive packets from other clients
Serial.print("Restart connection: ");
Serial.println (udp.begin(5000) ? "Success" : "Failed");
previous_time = millis();
}
}