Advertisement
Guest User

photon_udp_test

a guest
Jul 31st, 2015
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.93 KB | None | 0 0
  1. // UDP Port used for two way communication
  2. unsigned int localPort = 8888;
  3.  
  4. UDP udp;
  5.  
  6. const size_t maxPacketSize = 256;
  7. uint8_t packetBuffer[maxPacketSize];
  8. IPAddress ip(192,168,178,20); // ip of the computer running photontest.py
  9.  
  10. uint32_t last;
  11. uint32_t count = 0;
  12. const uint32_t interval = 20000;
  13.  
  14. void send_count(IPAddress& ip, int port){
  15.   udp.beginPacket(ip, port);
  16.   String countstr(count, DEC);
  17.   countstr.getBytes(&packetBuffer[0], maxPacketSize);
  18.   size_t count_length = countstr.length();
  19.   packetBuffer[count_length] = ' ';
  20.   udp.write(&packetBuffer[0], count_length+1);
  21.   udp.endPacket();
  22. }
  23.  
  24. void setup() {
  25.     Serial.begin(9600);
  26.     udp.begin(localPort);
  27.     last = millis();
  28. }
  29.  
  30. void loop() {
  31.   auto now = millis();
  32.   // not wrap around safe but that only happens every 50 days
  33.   // and this is a simple hack
  34.   if(now - last >= interval) {
  35.     last = now;
  36.     send_count(ip, 8888);
  37.     count++;
  38.   }
  39.  
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement