Showing posts with label processing data. Show all posts
Showing posts with label processing data. Show all posts

Friday, March 20, 2015

Publish data from your ESP8266 to thingspeak.com

On this posts ( post#1, post#2) I've presented how to create a channel, fields, API KEYS on thingspeak.com

Now will publish some data from ESP8266 to thingspeak.com. Can be any kind of data, from
temperature, humidity, voltage you name it. As long as it has a value, can be published.

Add following  lines after the include statements. (Can use the blinky code as skeleton).

#include "espconn.h" 
LOCAL struct espconn *pCon = NULL;
LOCAL my_count = 0;
LOCAL your_value = 33;




Add the following code in the user_init() function.

const char ssid[32] = "my_home_ssid";
const char password[32] = "my_home_password";

struct station_config stationConf;

wifi_set_opmode( STATION_MODE );
os_memcpy(&stationConf.ssid, ssid, 32);
os_memcpy(&stationConf.password, password, 32);
wifi_station_set_config(&stationConf); 
wifi_station_connect();
//connect to the previous pCon created structure
int ret = 0;
ret = espconn_connect(pCon);

if(ret == 0) 
      INFO("espconn_connect OK!\r\n");
else
{
      //INFO("espconn_connect FAILED!\r\n");  
      char *fail;
      os_sprintf(fail, "%d \r\n", ret);
      //INFO(fail);
      //clean up allocated memory
      if(pCon->proto.tcp)
          os_free(pCon->proto.tcp);
      os_free(pCon);
      pCon = NULL;
} 

Add this code in a timer function like in this example:

pCon = (struct espconn *)os_zalloc(sizeof(struct espconn));
if (pCon == NULL)
{
        os_printf("pCon ALLOCATION FAIL\r\n");
        return;
}

pCon->type = ESPCONN_TCP;
pCon->state = ESPCONN_NONE;
    
pCon->proto.tcp = (esp_tcp *)os_zalloc(sizeof(esp_tcp));
pCon->proto.tcp->local_port = espconn_port();
//set up the server remote port
pCon->proto.tcp->remote_port = 80;

//set up the remote IP
uint32_t ip = ipaddr_addr("184.106.153.149"); //IP address for thingspeak.com
os_memcpy(pCon->proto.tcp->remote_ip, &ip, 4);

//set up the local IP
struct ip_info ipconfig;
wifi_get_ip_info(STATION_IF, &ipconfig);
os_memcpy(pCon->proto.tcp->local_ip, &ipconfig.ip, 4);

//register publish_thingspeak_connect_cb that will be called when the
//connection with the thingspeak is done. In this call back function
// will actualy do the data sending
espconn_regist_connectcb(pCon, publish_thingspeak_connect_cb);



where the callback function is:


static void ICACHE_FLASH_ATTR publish_thingspeak_connect_cb(void *arg)
{
  //INFO("========>publish_thingspeak_connect_cb\r\n");
  my_count++;

  struct espconn *pespconn = (struct espconn *)arg;

  char payload[128];

  char field1[10];
  char field2[10];

  os_sprintf(field1, "%d", your_value);
  os_sprintf(field2, "%d", my_count);

  os_sprintf(payload, "GET /update?  api_key=YOUR_THINGSPEAK_API_KEY&field1=%s&field2=%s\r\n", field1, field2);

  espconn_sent(pespconn, payload, strlen(payload));
  
}

and need to be added before the user_main function.

Change my_count and your_value with your data. 

Notes:

1. The time between to posts on thingspeak.com must be greater then 15 seconds.
2. Up to 8 fields can be used to post data on thingspeak.com





Thursday, March 5, 2015

Usefull tools #1






Ok, we have the board, we have the environment but we need to see the values from the sensors as a nice graph. What we should do ?

For storing the values I recommend and I use thingspeak.com.

Make an account on thingspeak.com and create a new channel.



Complete the fields for the newly created channel, a channel name, a description, metadata,
tags, Make Public check box.



On this step important fields are ones labeled Field 1 to Field 8. You can use them later 
to insert values into the thingspeak database. So complete the labels that your values will have.




Save your channel so you can use it later. You can come back later and modify those values.



API KEY

In order to upload values to thingspeak.com from the ESP8266, match must be done between your account and your ESP8266. This is done by the API KEY which can be found in the API KEYS menu.

Keep this KEY secret and don't post it on the web as I've seen it in a lot of github code. Every one that know you API KEY can alter your values and you will bang your head on the tables to see why your temperature is 200 degrees.  



Don't bother to use the API KEY above, I'll delete the channel later.


Testing

It's time to test the channel and the api key.

For now just add the the link changing the API_KEY with your own value in a browser.

http://api.thingspeak.com/update?key=[API_KEY]&field1=22&field2=55

which in my case is 

http://api.thingspeak.com/update?key=UD05NUCXSTSJ39W&field1=22&field2=55

Don't panic if your browser will show you just a blank page with one value "1", is the id
for your first record inserted into the database.





If you are going back the the channel page, you will see the values for field1 (temperature) and field2(humidity). In this way you can update up to 8 values in one shot.

Temperature:



Humidity:



Note: Keep at least 15 seconds between two writes !