Go Down

Topic: Get time with NTP over ethernet and send data to Exosite (Read 873 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]

#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 {
#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;


   switch(OutputUnit) {
     case T_CELSIUS :
     case T_FAHRENHEIT :

   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.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:
  Serial.print("Got IP:");
  Serial.println("Looking for a time");
  Serial.println("Got a Time");
  //while(timeStatus()== timeNotSet)   
    // ;


void loop()
  time_t t = now();
  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];

  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];

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){
  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){
//azzerare costanti bilancia


i = 0;


void printDigits(int digits){
  if(digits < 10)

unsigned long getNTPTime()
  Serial.println("In getNTPTime");
  if ( Udp.parsePacket() ) { 
    Serial.println("Got Time Packet");

    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

Thank you



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