Guest User

Untitled

a guest
Apr 14th, 2021
38
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. static mg_connection* sendConnection;
  2. static bool sendConnectionReady;
  3.  
  4. // this sends packet every 1 second (see timer below)
  5. static void sendPacket(void *arg) {
  6.   if(sendConnectionReady && sendConnection != NULL) {
  7.     mg_printf(sendConnection, "blabla\n");
  8.   }
  9. }
  10.  
  11. // this handles udp socket events
  12. static void sendConnectionHandler(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
  13.    switch(ev){
  14.      case MG_EV_CONNECT:
  15.        sendConnectionReady = true;
  16.        break;
  17.      case MG_EV_CLOSE:
  18.        sendConnectionReady = false;
  19.        break;
  20.  
  21.    }
  22. }
  23.  
  24. // this handles network events, creates udp socket AFTER device gets IP address from dhcp (dunno how it works with static ip, will see)
  25. static void app_net_ev_handler(int ev, void *evd, void *arg) {
  26.    switch (ev) {
  27.     case MGOS_NET_EV_DISCONNECTED:
  28.       sendConnection = NULL;
  29.       break;
  30.     case MGOS_NET_EV_IP_ACQUIRED:
  31.       sendConnection =  mg_connect(mgos_get_mgr(), "udp://192.168.17.30:7777", sendConnectionHandler, NULL);
  32.       break;
  33.   }
  34.  
  35.   (void) evd;
  36.   (void) arg;
  37. }
  38.  
  39.  
  40.  
  41. enum mgos_app_init_result mgos_app_init(void) {
  42.  
  43.     ...
  44.     mgos_event_add_group_handler(MGOS_EVENT_GRP_NET, app_net_ev_handler, NULL);
  45.     mgos_set_timer(1000, MGOS_TIMER_REPEAT | MGOS_TIMER_RUN_NOW, sendPacket, NULL);  
  46.     ...
  47.  
  48. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×