Guest User

Untitled

a guest
Apr 14th, 2021
43
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "mgos.h"
  2. #include "mgos_timers.h"
  3. using namespace std;
  4.  
  5. struct mg_mgr mgr;
  6.  
  7.  
  8. static int sendCnt = 0;
  9. static void sending_timer(void *arg) {
  10.   LOG(LL_INFO, ("sending %d", sendCnt++));
  11.     static mg_connection* sendConnection1 =  mg_connect(mgos_get_mgr(), "udp://192.168.17.30:7777", NULL, NULL);
  12.     if(sendConnection1 != NULL) {
  13.       mg_printf(sendConnection1, "test test test test");
  14.       sendConnection1->flags |= MG_F_SEND_AND_CLOSE;
  15.       LOG(LL_INFO, ("sent %d", sendCnt));
  16.     } else {
  17.       LOG(LL_ERROR, ("%s", "Failed to establish udp connection"));
  18.     }
  19. }
  20.  
  21. static void app_net_ev_handler(int ev, void *evd, void *arg) {
  22.  
  23.    switch (ev) {
  24.     case MGOS_NET_EV_DISCONNECTED:
  25.       LOG(LL_INFO, ("%s", "Net disconnected"));
  26.       break;
  27.     case MGOS_NET_EV_CONNECTING:
  28.       LOG(LL_INFO, ("%s", "Net connecting..."));
  29.       break;
  30.     case MGOS_NET_EV_CONNECTED:
  31.       LOG(LL_INFO, ("%s", "Net connected"));
  32.       break;
  33.     case MGOS_NET_EV_IP_ACQUIRED:
  34.       LOG(LL_INFO, ("%s", "Net got IP address"));
  35.       mgos_set_timer(3000, MGOS_TIMER_REPEAT, sending_timer, NULL);  
  36.       break;
  37.   }
  38.  
  39.   (void) evd;
  40.   (void) arg;
  41. }
  42.  
  43. enum mgos_app_init_result mgos_app_init(void) {
  44.   LOG(LL_INFO, ("Initializing"));
  45.  
  46.   mg_mgr_init(&mgr, NULL);
  47.   mgos_event_add_group_handler(MGOS_EVENT_GRP_NET, app_net_ev_handler, NULL);
  48.  
  49.   LOG(LL_INFO, ("Initialization complete"));
  50.  
  51.   return MGOS_APP_INIT_SUCCESS;
  52. }
  53.  
  54.  
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.

×