Go Down

Topic: Temp Sensor does not get reasonable result (Read 1 time) previous topic - next topic

robtillaart

Q:

Udp.sendPacket(udp_content, remoteIP, remotePort ); ?? How does sendPacket know the length of the packet to be send?
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

fruit112358


Q:

Udp.sendPacket(udp_content, remoteIP, remotePort ); ?? How does sendPacket know the length of the packet to be send?


According to my understanding, for every UDP packet in IP v4, the size of data in one packet is always 160 bits. In other words, no matter how real size of data is, the method always send 160 bits data every time.

fruit112358


This:
Code: [Select]
sensorValue_Light = analogRead(sensorPin_Light);
sensorValue_Temp = analogRead(sensorPin_Temp);


may be problematic. The analog pins share a single A2D converter. If you try to read from multiple pins in rapid succession, the later results tend to be inaccurate. Recent posts have suggested a couple of remedies: use a small delay between readings or take two readings for each input and ignore the first. In your case, just commenting out the analogRead for the light should show you if that is the issue.


I have tried both ways: only read temperature and read twice when there is light sensor. fortunately, both of them works well for me. Thank you very much. :)

robtillaart


Quote
According to my understanding, for every UDP packet in IP v4, the size of data in one packet is always 160 bits. In other words, no matter how real size of data is, the method always send 160 bits data every time.


Dived into the library myself, and the function is overloaded. for a byte string you need to supply the length and for a char array it sends until it meets a '\0' char.

Code: [Select]

/* Send packet contained in buf of length len to peer at specified ip, and port */
/* Use this function to transmit binary data that might contain 0x00 bytes*/
/* This function returns sent data size for success else -1. */
uint16_t UdpClass::sendPacket(uint8_t * buf, uint16_t len,  uint8_t * ip, uint16_t port){
  return sendto(_sock,(const uint8_t *)buf,len,ip,port);
}

/* Send  zero-terminated string str as packet to peer at specified ip, and port */
/* This function returns sent data size for success else -1. */
uint16_t UdpClass::sendPacket(const char str[], uint8_t * ip, uint16_t port){
  // compute strlen
  const char *s;
  for(s = str; *s; ++s);
  uint16_t len = (s-str);
  // send packet
  return sendto(_sock,(const uint8_t *)str,len,ip,port);
}
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

uink

You might also consider using a resistor

Go Up