コード例 #1
0
/**
 * @brief Write data to the cloud
 * Note: you have to register components prior.
 * E.g. using "iotkit-admin register <comp_name> <catalogid>" or via iotkit-agent
 */
void writeDataToCloud(string &component, string &data) {
    UdpClient client;
    stringstream ss;

    /* Create a udp connection for iotkit-agent to communicate over */
    if (client.isConnected() == false) {
        if (client.connectUdp(NODE, SERVICE) < 0) {
            cerr << "connection failed" << endl;
            exit(EXIT_FAILURE);
        }
    }

    /*
     * Create the string to send to the cloud
     * eg. {"n":"gpsv1","v":"45.550953, -122.968719"}
     */
    ss << "{\"n\":\"" << component << "\",\"v\":\"" << data << "\"}" << endl;
    cout << ss.str();

    if (client.writeData(ss) < 0)
        cerr << "Cannot write to cloud\n" << endl;

    ss.str("");
}
コード例 #2
0
int main()
{
    // check that we are running on Galileo or Edison
    mraa::Platform platform = mraa::getPlatformType();
    if ((platform != mraa::INTEL_GALILEO_GEN1) &&
            (platform != mraa::INTEL_GALILEO_GEN2) &&
            (platform != mraa::INTEL_EDISON_FAB_C)) {
        std::cerr << "Unsupported platform, exiting" << std::endl;
        return mraa::ERROR_INVALID_PLATFORM;
    }

    // UdpClient class is wrapper for sending UDP data to iotkit-agent
    UdpClient client;
    if (client.connectUdp(NODE, SERVICE) < 0) {
        std::cerr << "Connection to iotkit-agent failed, exiting" << std::endl;
        return mraa::ERROR_UNSPECIFIED;
    }

    // create an analog input object from MRAA using pin A0
    mraa::Aio* a_pin = new mraa::Aio(0);
    if (a_pin == NULL) {
        std::cerr << "Can't create mraa::Aio object, exiting" << std::endl;
        return mraa::ERROR_UNSPECIFIED;
    }

    // loop forever sending the input value every second
    for (;;) {
        uint16_t data = a_pin->read();
        std::stringstream ss;
        ss << "{\"n\":\"" << COMP_NAME << "\",\"v\":" << data << "}" << std::endl;
        client.writeData(ss);
        sleep(1);
    }

    return mraa::SUCCESS;
}