// DS18B20 SENSOR #include #include #include #include #include #include #include #define SERVER_IP "192.168.0.100" // IP address of the Pixhawk #define SERVER_PORT 5000 // Port number on which Pixhawk is listening int temp_udp_socket; struct sockaddr_in temp_serverAddr; int temperature_init(); void temperature_spg() { if (temperature_init() == -1) printf("Initialization failed.\n"); FILE *tempfile = fopen("/sys/bus/w1/devices/28-0000027a4334/w1_slave", "r"); char temptext[100]; fgets(temptext, 100, tempfile); fclose(tempfile); char *tempdata = strtok(temptext, "\n"); tempdata = strtok(NULL, " "); tempdata = strtok(NULL, " "); tempdata = strtok(NULL, " "); tempdata = strtok(NULL, " "); tempdata = strtok(NULL, " "); tempdata = strtok(NULL, " "); tempdata = strtok(NULL, " "); tempdata = strtok(NULL, "="); float temperature = atof(tempdata + 2) / 1000.0; printf("%f\n", temperature); // Send temperature data to Pixhawk char temperatureStr[20]; // Is this good enough for ARK? sprintf(temperatureStr, "%.2f", temperature); if (send(temp_udp_socket, temperatureStr, strlen(temperatureStr), 0) == -1) perror("Error sending the data!"); // sleep(1); // Delay for 1 second // Will be done in AADL close(temp_udp_socket); // Close the socket } int temperature_init() { // Create socket if ((temp_udp_socket = socket(AF_INET, SOCK_DGRAM, 0)) == -1) // For TCP, SOCK_STREAM { perror("socket"); return -1; } // Set server address memset(&temp_serverAddr, 0, sizeof(temp_serverAddr)); // V1 temp_serverAddr.sin_family = AF_INET; temp_serverAddr.sin_port = htons(SERVER_PORT); // temp_serverAddr.sin_addr.s_addr = inet_addr(SERVER_IP); // For IPv4 // memset(temp_serverAddr.sin_zero, '\0', sizeof(temp_serverAddr.sin_zero)); // V2 if (inet_pton(AF_INET, SERVER_IP, &(temp_serverAddr.sin_addr)) <= 0) { perror("inet_pton"); return -1; } // Connect to the server if (connect(temp_udp_socket, (struct sockaddr *)&temp_serverAddr, sizeof(temp_serverAddr)) == -1) { perror("connect"); return -1; } return 0; }