162 lines
3.6 KiB
C
162 lines
3.6 KiB
C
#include <arpa/inet.h>
|
|
#include <cjson/cJSON.h>
|
|
#include <sqlite3.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
|
|
#define PORT 9000
|
|
#define BUFFER_SIZE 1024
|
|
|
|
typedef struct ESPData {
|
|
double celsius;
|
|
double fahrenheit;
|
|
double humidity;
|
|
} ESPData;
|
|
|
|
int parseJSON(char buffer[BUFFER_SIZE], ESPData *espData);
|
|
sqlite3 *initDB();
|
|
int storeData(sqlite3 *db, ESPData espData);
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
sqlite3 *db = initDB();
|
|
if (!db) {
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
int serverSocket;
|
|
struct sockaddr_in serverAddr;
|
|
|
|
if ((serverSocket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
|
perror("socket failed");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
serverAddr.sin_family = AF_INET;
|
|
serverAddr.sin_addr.s_addr = INADDR_ANY;
|
|
serverAddr.sin_port = htons(PORT);
|
|
|
|
if (bind(serverSocket, (struct sockaddr *)&serverAddr, sizeof serverAddr) <
|
|
0) {
|
|
perror("bind failed");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
if (listen(serverSocket, 5) < 0) {
|
|
perror("listen failed");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
printf("Server listening on port %d\n", PORT);
|
|
while (1) {
|
|
|
|
struct sockaddr_in clientAddr;
|
|
socklen_t clientAddr_len = sizeof clientAddr;
|
|
int *clientSocket = malloc(sizeof(int));
|
|
|
|
if ((*clientSocket = accept(serverSocket, (struct sockaddr *)&clientAddr,
|
|
&clientAddr_len)) < 0) {
|
|
perror("accept failed");
|
|
continue;
|
|
}
|
|
|
|
printf("Connected\n");
|
|
|
|
// Receive from Client
|
|
char buf[BUFFER_SIZE];
|
|
int bytes = recv(*clientSocket, buf, sizeof buf, 0);
|
|
if (bytes <= 0) {
|
|
perror("failed to recv from client");
|
|
continue;
|
|
}
|
|
printf("raw data: %s\n", buf);
|
|
|
|
// Parse JSON
|
|
ESPData espData;
|
|
if (parseJSON(buf, &espData) == 0) {
|
|
printf("Fahrenheit: %.2f, Celsius: %.2f, Humidity: %.2f\n",
|
|
espData.fahrenheit, espData.celsius, espData.humidity);
|
|
|
|
storeData(db, espData);
|
|
}
|
|
|
|
close(*clientSocket);
|
|
printf("disconnected client.\n");
|
|
}
|
|
|
|
close(serverSocket);
|
|
return 0;
|
|
}
|
|
|
|
int parseJSON(char buffer[BUFFER_SIZE], ESPData *espData) {
|
|
|
|
// char *data = strstr(buffer, "\r\n\r\n");
|
|
// if (data) {
|
|
// data += 4;
|
|
// } else {
|
|
// return -1;
|
|
// }
|
|
|
|
const cJSON *fahr;
|
|
const cJSON *cel;
|
|
const cJSON *hum;
|
|
cJSON *jsonData = cJSON_Parse(buffer);
|
|
|
|
fahr = cJSON_GetObjectItem(jsonData, "fahrenheit");
|
|
if (!fahr || !cJSON_IsNumber(fahr)) {
|
|
printf("Error: failed to parse fahrenheit\n");
|
|
cJSON_Delete(jsonData);
|
|
return -1;
|
|
}
|
|
|
|
cel = cJSON_GetObjectItem(jsonData, "celsius");
|
|
if (!cel || !cJSON_IsNumber(cel)) {
|
|
printf("Error: failed to parse celsius\n");
|
|
cJSON_Delete(jsonData);
|
|
return -1;
|
|
}
|
|
|
|
hum = cJSON_GetObjectItem(jsonData, "humidity");
|
|
if (!hum || !cJSON_IsNumber(hum)) {
|
|
printf("Error: failed to parse humidity\n");
|
|
cJSON_Delete(jsonData);
|
|
return -1;
|
|
}
|
|
|
|
espData->fahrenheit = fahr->valuedouble;
|
|
espData->celsius = cel->valuedouble;
|
|
espData->humidity = hum->valuedouble;
|
|
|
|
cJSON_Delete(jsonData);
|
|
return 0;
|
|
}
|
|
|
|
sqlite3 *initDB() {
|
|
sqlite3 *db = NULL;
|
|
int res = sqlite3_open("stats.db", &db);
|
|
if (res != SQLITE_OK) {
|
|
fprintf(stderr, "Failed to open the db: %s", sqlite3_errmsg(db));
|
|
}
|
|
|
|
return db;
|
|
}
|
|
int storeData(sqlite3 *db, ESPData espData) {
|
|
|
|
char sql[256];
|
|
sprintf(sql,
|
|
"insert into Stats(Id, Fahrenheit, Celsius, Humidity) values(NULL, "
|
|
"'%f', '%f', '%f');",
|
|
espData.fahrenheit, espData.celsius, espData.humidity);
|
|
|
|
char *errMsg;
|
|
int res = sqlite3_exec(db, sql, 0, 0, &errMsg);
|
|
if (res != SQLITE_OK) {
|
|
fprintf(stderr, "Could not insert into DB: %s", errMsg);
|
|
sqlite3_free(errMsg);
|
|
return -1;
|
|
}
|
|
return 0;
|
|
}
|