Go Down

Topic: Get time with NTP over ethernet and send data to Exosite (Read 955 times) previous topic - next topic

Dear Forum users,

I'm working on a monitoring system(for my beehives) based on one Arduino Uno (with an ethernet shield) that takes data from his ADCs and over software serial from a Teensy2++ and then sends the data to Exosite.

Until this point everything worked and so I tried to add the time(synchronised over internet with NTP) in order to be able to reset some variables overnight, the problem is that now I get the time but I can't send data to Exosite.

Does someone know where the problem is?

Here is the code, I always only get to the point were the arduino has the data from the Teensy and then it usually resets:
Code: [Select]

//librerie
#include <SPI.h>
#include <SoftwareSerial.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <Exosite.h>
#include <avr-libc.h>
#include <Time.h>

// function that calculates the temperature
enum {
   T_KELVIN=0,
   T_CELSIUS,
   T_FAHRENHEIT
};
#define EPISCO_K164_10k 4300.0f,298.15f,10000.0f  // B,T0,R0
#define SEN00250_10k 3977.0f,298.15f,10000.0f // B,T0,R0
#define RESISTOR 9950.0f
float Temperature(int AnalogInputNumber,int OutputUnit,float B,float T0,float R0,float R_Balance)
{
   float R,T;

   R=1024.0f*R_Balance/float(AnalogInputNumber)-R_Balance;
   T=1.0f/(1.0f/T0+(1.0f/B)*log(R/R0));

   switch(OutputUnit) {
     case T_CELSIUS :
       T-=273.15f;
     break;
     case T_FAHRENHEIT :
       T=9.0f*(T-273.15f)/3.28f+32.0f;
     break;
     default:
     break;
   };

   return T;
}
// Indirizzo MAC della scheda Ethernet
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x11, 0x22 };

//Username di exosite
String cikData = "a0287bfc60666d2f90a5b87fe28b0db11952b969";      // <-- Fill in your CIK here! (https://portals.exosite.com -> Add Device)


unsigned int localPort = 8888;      // local port to listen for UDP packets

IPAddress timeServer(199,167,198,163); // pool.ntp.org NTP server
//IPAddress timeServer = "hera.limescope.net";

const int NTP_PACKET_SIZE= 48; // NTP time stamp is in the first 48 bytes of the message

byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets

// A UDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

int timeZoneHour = +2; // Enter Your Actual Time Zone EDT (-4)

long timeZoneOffset = (timeZoneHour * -1) * 60 * 60 ;
int NTP_Update_Interval = 60; // Number of secs before resync - should be longer just testing really

//array che prendono i dati
unsigned int bilancia[50];
unsigned int compensazione[50];
//totali bilancia
unsigned int bilanciat1;
unsigned int bilanciat2;
unsigned int bilanciat;
//totali compensazione
unsigned int compensazionet2;
unsigned int compensazionet1;
unsigned int compensazionet;

int cleaned;

int i;

//costanti per il conteggio
unsigned int ins;
unsigned int insHigh;
unsigned int insLow;
unsigned int outs;
unsigned int outsHigh;
unsigned int outsLow;



int AnalogInputNumber;

// global
Exosite exosite(&Ethernet, mac, cikData);
//pin della comunicazione con il beecounter
SoftwareSerial myserial(6, 7); // RX, TX




void setup()
{
  Serial.begin(9600);
  myserial.begin(9600);
  exosite.init();

// Serial.println("Starting");
  // start Ethernet and UDP
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    for(;;)
      ;
   }
  Udp.begin(localPort);
  Serial.print("Got IP:");
  Serial.println(Ethernet.localIP());
  setSyncProvider(getNTPTime);
  Serial.println("Looking for a time");
  Serial.println("Got a Time");
  //while(timeStatus()== timeNotSet)   
    // ;
  setSyncInterval(NTP_Update_Interval);
 

}

void loop()
{
  time_t t = now();
 
  printDigits(hour(t));
  Serial.print(":");
  printDigits(minute(t));
  Serial.println();
 
  String retVal;


//get the data from the ADCs

for(i=0; i<50; i++){
  bilancia[i]= analogRead(A0);
  compensazione[i]= analogRead(A1);
  bilanciat1= bilanciat1 + bilancia[i];
  compensazionet1= compensazionet1 + compensazione[i];
  delay(30);
}


  for(i=0; i<50; i++){
  bilancia[i]= 0;
  compensazione[i]= 0;
}


  for(i=0; i<50; i++){
  bilancia[i]= analogRead(A0);
  compensazione[i]= analogRead(A1);
  bilanciat2= bilanciat2 + bilancia[i];
  compensazionet2 = compensazionet2 + compensazione[i];
  delay(30);
}

for(i=0; i<50; i++){
  bilancia[i]= 0;
  compensazione[i]= 0;
}
Serial.println("Got the scale data");
//medie parziali e medie totali
bilanciat1 = bilanciat1/50;
bilanciat2 = bilanciat2/50;

compensazionet1 = compensazionet1/50;
compensazionet2 = compensazionet2/50;

bilanciat= (bilanciat1 + bilanciat2)/2;
compensazionet = (compensazionet1 + compensazionet2)/2;


//calculate temperature
  AnalogInputNumber = compensazionet;
  float Temperatura = 100*Temperature(compensazionet,T_CELSIUS,SEN00250_10k,RESISTOR);
Serial.println("Calculated the temperature");

//read data from the teensy over software serial
ins = 0;
outs = 0;
  while(ins == 0){
  myserial.listen();
  if (myserial.available() > 4){
    if (myserial.read() == 'I'){
     insHigh = myserial.read();
     insLow = myserial.read();
     outsHigh = myserial.read();
     outsLow = myserial.read();
     outs = outsLow + (outsHigh*256);
     ins = insLow + (insHigh*256);
     Serial.println("Getting beecounter data");

   }}}
     Serial.println("Got beecounter data");

//The software interrupts here and it resets

  //Send analog input value to Exosite, use the alias (resource name) "1"
  exosite.sendToCloud("Bilancia", bilanciat);
  exosite.sendToCloud("Compensazione", compensazionet);
  exosite.sendToCloud("Temperatura", Temperatura);
  exosite.sendToCloud("Ins", ins);
  exosite.sendToCloud("Outs", outs);
  //exosite.sendToCloud("Cleaned", cleaned);

  Serial.println("DATA SENT");

 

 
//send signal to the teensy to reset variable at midnight
if(hour() == 00){
   myserial.print('S');
   ins=0;
   }
//azzerare costanti bilancia
bilanciat1=0,
bilanciat2=0;
bilanciat=0;

compensazionet2=0;
compensazionet1=0;
compensazionet=0;

i = 0;


  delay(1000);
    }

//funzioni
void printDigits(int digits){
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

unsigned long getNTPTime()
{
  Serial.println("In getNTPTime");
  sendNTPpacket(timeServer);
  delay(1000);
  if ( Udp.parsePacket() ) { 
    Serial.println("Got Time Packet");
    Udp.read(packetBuffer,NTP_PACKET_SIZE);

    unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
    unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]); 
    unsigned long secsSince1900 = highWord << 16 | lowWord; 

    const unsigned long seventyYears = 2208988800UL + timeZoneOffset;     
    unsigned long epoch = secsSince1900 - seventyYears; 
 
    return epoch;   
  }
  Serial.println("No Time Packet Found");
  return 0;
}
// send an NTP request to the time server at the given address
unsigned long sendNTPpacket(IPAddress& address)
{
  // set all bytes in the buffer to 0
  memset(packetBuffer, 0, NTP_PACKET_SIZE);
  // Initialize values needed to form NTP request
  // (see URL above for details on the packets)
  packetBuffer[0] = 0b11100011;   // LI, Version, Mode
  packetBuffer[1] = 0;     // Stratum, or type of clock
  packetBuffer[2] = 6;     // Polling Interval
  packetBuffer[3] = 0xEC;  // Peer Clock Precision
  // 8 bytes of zero for Root Delay & Root Dispersion
  packetBuffer[12]  = 49;
  packetBuffer[13]  = 0x4E;
  packetBuffer[14]  = 49;
  packetBuffer[15]  = 52;

  // all NTP fields have been given values, now
  // you can send a packet requesting a timestamp:    
  Udp.beginPacket(address, 123); //NTP requests are to port 123
  Udp.write(packetBuffer,NTP_PACKET_SIZE);
  Udp.endPacket();
}


Thank you

Ciaociao

SurferTim

Normally a reset is caused by running out of SRAM. Try using the F() function on all your static string constants. Here is an example. Do the same to the rest.
Code: [Select]
    Serial.println(F("Failed to configure Ethernet using DHCP"));


Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy