예제 #1
0
int main()
{
	const char* file_path = "../example.json";

	long length = get_file_length(file_path);
	if (length == -1)
	{
		exit(1);
	}

	char* buffer = (char*) malloc(length);

	int ret_val = read_json_file_into_string(file_path, length, buffer);
	if (ret_val == -1)
	{
		exit(1);
	}

	JSON_Obj* base_obj = parse_JSON(buffer, length);

	printf("Items after parse:\n");
	printf("==================\n\n");
	for (int i = 0; i < base_obj->item_count; i++)
	{
		printf("Item %d name: %s\n", (i + 1), base_obj->items[i]->name);
	}

	return 0;
}
예제 #2
0
int main() {
    int i;
    HTS221 hts221;
    pc.baud(115200);
    
    void hts221_init(void);

    // Set LED to RED until init finishes
    SetLedColor(0x1);

    pc.printf(BLU "Hello World from AT&T Shape!\r\n\n\r");
    pc.printf(GRN "Initialize the HTS221\n\r");

    i = hts221.begin();  
    if( i ) 
        pc.printf(BLU "HTS221 Detected! (0x%02X)\n\r",i);
    else
        pc.printf(RED "HTS221 NOT DETECTED!!\n\r");

    printf("Temp  is: %0.2f F \n\r",CTOF(hts221.readTemperature()));
    printf("Humid is: %02d %%\n\r",hts221.readHumidity());
    
    sensors_init();
    read_sensors();

    // Initialize the modem
    printf(GRN "Modem initializing... will take up to 60 seconds" DEF "\r\n");
    do {
        i=mdm_init();
        if (!i) {
            pc.printf(RED "Modem initialization failed!" DEF "\n");
        }
    } while (!i);
    
    //Software init
    software_init_mdm();
 
    // Resolve URL to IP address to connect to
    resolve_mdm();

    //Create a 1ms timer tick function:
    OneMsTicker.attach(OneMsFunction, 0.001f) ;

    iTimer1Interval_ms = SENSOR_UPDATE_INTERVAL_MS;

    // Open the socket (connect to the server)
    sockopen_mdm();

    // Set LED BLUE for partial init
    SetLedColor(0x4);

    // Send and receive data perpetually
    while(1) {
        static unsigned ledOnce = 0;
        if  (bTimerExpiredFlag)
        {
            bTimerExpiredFlag = false;
            sprintf(SENSOR_DATA.Temperature, "%0.2f", CTOF(hts221.readTemperature()));
            sprintf(SENSOR_DATA.Humidity, "%02d", hts221.readHumidity());
            read_sensors(); //read available external sensors from a PMOD and the on-board motion sensor
            char modem_string[512];
            GenerateModemString(&modem_string[0]);
            printf(BLU "Sending to modem : %s" DEF "\n", modem_string); 
            sockwrite_mdm(modem_string);
            sockread_mdm(&MySocketData, 1024, 20);
            
            // If any non-zero response from server, make it GREEN one-time
            //  then the actual FLOW responses will set the color.
            if ((!ledOnce) && (MySocketData.length() > 0))
            {
                ledOnce = 1;
                SetLedColor(0x2);
            }
            
            printf(BLU "Read back : %s" DEF "\n", &MySocketData[0]);
            char * myJsonResponse;
            if (extract_JSON(&MySocketData[0], &myJsonResponse[0]))
            {
                printf(GRN "JSON : %s" DEF "\n", &myJsonResponse[0]);
                parse_JSON(&myJsonResponse[0]);
            }
            else
            {
                printf(RED "JSON : %s" DEF "\n", &myJsonResponse[0]); //most likely an incomplete JSON string
                parse_JSON(&myJsonResponse[0]); //This is risky, as the string may be corrupted
            }
        } //bTimerExpiredFlag
    } //forever loop
}