示例#1
0
// Connect to the MQTT broker.  Call this function whenever connection needs
// to be re-established.
// returns:  0 - everything OK
//          -1 - packet error
//          -2 - failed to get connack
int App_ConnectMqtt()
{  
    int packet_length;

    mqtt_init(&broker, WIO_CLIENTID);
    mqtt_init_auth(&broker, WIO_USERNAME, WIO_PASSWORD);
    mqtt_connect(&broker);
    // wait for CONNACK	
    packet_length = mqtt_read_packet(6000);

    if(packet_length < 0) 
    {
        DisplayLCD(LCD_LINE4, "MQTT packet error");
        return -1;
    }
    if(MQTTParseMessageType(rxm.message) != MQTT_MSG_CONNACK) 
    { 
        return -2;
    }  
    if(rxm.message[3] != 0x00)
    {
        return -2;
    }
    App_PrepareIncomingData();
    AtLibGs_FlushIncomingMessage();
    mqttConnected=1;
    return 0;
}
/**
 * Read message from network (non-blocking).
 * Memory management is handled by the callee.
 *
 * @param socket  the UDP socket
 * @param buf     destination of the received bytes
 * @param len     length of destination buffer
 * @param addr    the senders IP address (host byte order)
 * @param port    the senders UDP port (host byte order)
 * @return        the number of bytes received
 */
ssize_t nabto_read(nabto_socket_t socket,
                   uint8_t*       buf,
                   size_t         len,
                   uint32_t*      addr,
                   uint16_t*      port)
{
  ATLIBGS_UDPMessage at_msg;
  
  if (AtLibGs_WaitForUDPMessage(10) == ATLIBGS_MSG_ID_OK) {
    uint8_t temp_port[2];
    int temp_ip[4];
    
    AtLibGs_ParseUDPData(G_received, G_receivedCount, &at_msg);
    
    memcpy(buf, at_msg.message, at_msg.numBytes);
    
    // Convert IP
    sscanf(at_msg.ip, _F8_ "." _F8_ "." _F8_ "." _F8_, &temp_ip[0], &temp_ip[1], &temp_ip[2], &temp_ip[3]);
    uint32_t ip = (uint32_t)temp_ip[0] << 24 |
                  (uint32_t)temp_ip[1] << 16 |
                  (uint32_t)temp_ip[2] << 8  |
                  (uint32_t)temp_ip[3];
    
    *addr = ip;
    
    // Convert port, host order
    temp_port[0] = (uint8_t)(at_msg.port >> 8);
    temp_port[1] = (uint8_t)(at_msg.port);
    *port = (uint16_t)temp_port[0] << 8 | temp_port[1];

    len = at_msg.numBytes;
    
    // Clear the RX buffer for the next reception
    App_PrepareIncomingData();
    
    return len;
  }
/*---------------------------------------------------------------------------*/
void App_TCPClientDemo(void)
{
    ATLIBGS_MSG_ID_E rxMsgId = ATLIBGS_MSG_ID_NONE;
    static char content[512];
    uint8_t cid = 0;
    static int16_t G_adc_int[2] = { 0, 0 };
    static char G_temp_int[2] = { 0, 0 };
    uint8_t remoteTcpSrvIp[20];
    
    bool connected = false; // when connected to TCP server this is true
    uint32_t time = MSTimerGet();
    ATLIBGS_TCPMessage msg;
    ATLIBGS_NetworkStatus networkStatus;
    
    AtLibGs_GetNetworkStatus(&networkStatus);
    
    AppTCPSetIPMenu(&networkStatus);

    sprintf((char*)remoteTcpSrvIp, "%d.%d.%d.%d",
            networkStatus.addr.ipv4[0],
            networkStatus.addr.ipv4[1],
            networkStatus.addr.ipv4[2],
            G_nvsettings.webprov.tcpIPClientHostIP);
    
    App_PrepareIncomingData();
    while (1) {
        if (!AtLibGs_IsNodeAssociated()) {
            App_Connect(&G_nvsettings.webprov);
            connected = false;
        } else if (!connected) {
            DisplayLCD(LCD_LINE7, "Connecting");
            // Start a TCP client
            rxMsgId = AtLibGs_TCPClientStart((char *)remoteTcpSrvIp,
                    TCP_DEMO_REMOTE_TCP_SRVR_PORT, &cid);
            if (rxMsgId != ATLIBGS_MSG_ID_OK) {
                DisplayLCD(LCD_LINE7, "No Connect!");
                MSTimerDelay(2000);
                DisplayLCD(LCD_LINE7, "");
                continue;
            }
            if (cid == ATLIBGS_INVALID_CID) {
                DisplayLCD(LCD_LINE7, "No CID!");
                MSTimerDelay(2000);
                DisplayLCD(LCD_LINE7, "");
                continue;
            }
            DisplayLCD(LCD_LINE7, "");
            App_PrepareIncomingData();
            connected = true;
        } else {
            App_TemperatureReadingUpdate(G_temp_int, true);
            App_PotentiometerUpdate(G_adc_int, true);

            // Look to see if there is a message
            if ((G_receivedCount) || (AtLibGs_WaitForTCPMessage(250)
                    == ATLIBGS_MSG_ID_DATA_RX)) {
                // Got data!  Its sitting in G_received, but in a <CID> <data> format
                // We need to send it back
                AtLibGs_ParseTCPData(G_received, G_receivedCount, &msg);

                // Prepare for the next batch of incoming data
                App_PrepareIncomingData();

                // Copy the data out of the receive message (its sitting in G_recieved)
                memcpy(content, msg.message, msg.numBytes);

                // Now send this back over the TCP/IP connection
                rxMsgId = AtLibGs_SendTCPData(cid, (uint8_t *)content,
                        msg.numBytes);
                if (rxMsgId != ATLIBGS_MSG_ID_OK) {
                    DisplayLCD(LCD_LINE7, " Send Fail!");
                    MSTimerDelay(2000);
                    DisplayLCD(LCD_LINE7, "");
                    continue;
                }
            } else if (MSTimerDelta(time) >= TCP_DEMO_UPDATE_INTERVAL) {
                time = MSTimerGet();
                // send temp and ADC to last received TCP client every X seconds

                DisplayLCD(LCD_LINE7, " Sending");
                sprintf(content, "Temp: %d.%d, Pot: %d.%d%%\r\n", G_temp_int[0],
                        G_temp_int[1], G_adc_int[0], G_adc_int[1]);
                // send data to server
                rxMsgId = AtLibGs_SendTCPData(cid, (uint8_t *)content, strlen(
                        content));
                if (rxMsgId != ATLIBGS_MSG_ID_OK) {
                    DisplayLCD(LCD_LINE7, " Send Fail!");
                    MSTimerDelay(2000);
                    DisplayLCD(LCD_LINE7, "");
                    connected = false;
                    continue;
                } // end if
                DisplayLCD(LCD_LINE7, "");
            } // end else if
        } // end else
    } // end while
}