main_demo.c 2.47 KB
#include "libwismart.h"
#include "libwismart_irqs.h" /* implement irq handlers */
#include "lwip/inet.h"
#include "globals.h"
#include "httpClient.h"
#include "callbacks.h"
//#include "buffer.h"
#include "json.h"
#include "ntp.h"
#include "i2c.h"

#define WIFI_MODE				WIFI_MODE_CLIENT
#define NETWORK_SSID			"linksys"
#define NETWORK_KEY             ""
#define WPA_USER				"smartcities"
#define WPA_PASS				"superpass"

uint8_t connected=0;
uint8_t timeout=0;
uint8_t retries=0;

void initLibwismart(void)
{
	wismart_hwif_t hwif = libwismart_GetDefaultHWIF();
	libwismart_Init(hwif);
}

int main(void)
{
	struct wpa_param wpa;
	wpa.eap_method = WISMART_EAP_METHOD_TTLS;
	wpa.u.ttls.identity=WPA_USER;
	wpa.u.ttls.password=WPA_PASS;
	wpa.u.ttls.ca_cert=NULL;
	initLibwismart();
	libwismart_PowerSave_Enable();
	libwismart_PowerSave_HigherProfile(TRUE);
	libwismart_RegisterDhcpCB(dhcp_connect_result_cb);	
	libwismart_WiFiInit();
	libwismart_WiFiConnectEnterprise(NETWORK_SSID, &wpa, wifi_connect_result_cb);

	printf("WiSmart initialized\r\n");

	while(!connected)
	{chThdSleepMilliseconds(500);}

	uint16_t received_data;
	char answer[10];

	I2C_init();
	printf("I2C Initialized\r\n");

	I2C_start(I2C1,0x39 << 1, I2C_Direction_Transmitter);
	I2C_write(I2C1, 0x0C);
	I2C_stop(I2C1);
	printf("I2C Temperature measurment request sent\r\n");

  	I2C_start(I2C1, 0x39<<1, I2C_Direction_Receiver);
  	printf("I2C Read request sent\r\n");
 	received_data = I2C_read_ack(I2C1);
 	received_data = received_data << 8;
 	received_data = received_data | I2C_read_ack(I2C1);
	printf("I2C data received\r\n");

	sprintf(answer,"%d",received_data);
	printf("Data: %s\r\n",answer);

	char **buffer;
	buffer = chHeapAlloc(NULL,sizeof (char *) * 1);
	
	Date time=getDate(getSecsSince1900());

	libwismart_EnableBsdSocketAPI();

	buffer[0] = timestamp_data(answer,time);

	printf("Init JSON statement\r\n");
	char* statement = prepare_json_observation_statement(buffer,1);
	chHeapFree(buffer[0]);
	printf("Statement: ");
	printf("%s\r\n",statement);
	uint8_t result = send_json(statement, strlen(statement), "bmp", "085");
	if(result == JSON_POST_OK)
	{
		printf("JSON_POST_OK\r\n");
	}
	else if(result == JSON_COMM_ERROR)
	{
		printf("JSON_COMM_ERROR\r\n");
	}
	else if(result == JSON_OTHER_ERROR)
	{
		printf("JSON_OTHER_ERROR\r\n");
	}
	else
	{
		printf("WTF I don't even\r\n");
	}
		
	for(;;)
	{
		chThdSleepMilliseconds(500);
	}
}