WIZwiki-W7500_ADC + Flot Exemple interactivity + THING SPEAK
Dependencies: SDFileSystem STATIC_COLORS WIZnetInterface mbed
test durée de fonctionnement :
172 458 secondes ===> environs 48h
Revision 6:d68dd410b141, committed 2016-07-11
- Comitter:
- Fo170
- Date:
- Mon Jul 11 17:12:01 2016 +0000
- Parent:
- 5:d5a3be58b76a
- Child:
- 7:c2acbe486cf2
- Commit message:
- adding THING SPEAK
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Sun Jul 10 00:08:42 2016 +0000
+++ b/main.cpp Mon Jul 11 17:12:01 2016 +0000
@@ -1,9 +1,19 @@
#include "mbed.h"
#include "EthernetInterface.h"
#include "SDFileSystem.h"
+#include <math.h>
#include <stdio.h>
#include <string.h>
+#define NB_SAMPLES 10
+unsigned long int Sample = 0;
+float adc_samples[NB_SAMPLES];// = { 0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0,1.01,1.02,1.03,1.04 };
+float time_samples[NB_SAMPLES];// = { -0.1,2,3,4,5,6,7,8,9,10,11,12,13,14 };
+float x_min = 0.0, x_max = 0.0;
+float y_min = 0.0, y_max = 0.0;
+float Seconds = 0.0;
+float meas = 0.0;
+
#define USE_DHCP 1
/*
MAC Address Details ( http://www.macvendorlookup.com/ )
@@ -41,6 +51,106 @@
TCPSocketServer server;
TCPSocketConnection client;
+//------ THING_SPEAK ------------------
+
+#define THRESHOLD_V 0.01
+float adc_v_moy = 0.0;
+float adc_v_moy_old = 0.0;
+int ret;
+bool status;
+float calcule_update_time;
+#define UPDATE_TIME_THING_SPEAK 15.0
+
+char paq_en[64];
+#define THING_SPEAK_IP_STR "184.106.153.149" /* thingspeak.com IP Address */
+#define THING_SPEAK_IP_PORT 80 /* port number */
+#define THING_SPEAK_KEY_STR "PD6JY2N94E3N0UNO" /* API key */
+#define THING_SPEAK_CHANNEL 132986 /* channel ID */
+#define THING_SPEAK_LABEL_STR "field1"
+char * str0 = "POST /update HTTP/1.1\n";
+char * str1 = "Host: api.thingspeak.com\n";
+char * str2 = "Connection: close\n";
+char * str3 = "X-THINGSPEAKAPIKEY: ";
+char * str4 = "Content-Type: application/x-www-form-urlencoded\n";
+char * write_key = "PD6JY2N94E3N0UNO";
+char * str5 = "Content-Length: ";
+TCPSocketConnection client_th;
+
+void thingspeak_update(void)
+{
+ int attempt = 0;
+ int length;
+ char buffer_th[64];
+ char data_entry[64];
+ float adc_v;
+ // récupération du dernier samples de la liste et convertion en volts
+ adc_v = adc_samples[NB_SAMPLES-1] * 0.001;
+ // moyenne sur 10 valeurs
+ adc_v_moy = 0.1 * (9.0 * adc_v_moy + adc_v);
+ // active l'envoi sur THING SPEAK si l'évolution moyenne est supérieure au seuil désiré
+ if((abs(adc_v_moy - adc_v_moy_old) >= THRESHOLD_V))
+ {
+ while(attempt < 4)
+ {
+ uart.printf("\n\rWaiting for connection to ThingSpeak server...\n\r");
+ ret = client_th.connect(THING_SPEAK_IP_STR,THING_SPEAK_IP_PORT);
+
+ if(!ret)
+ {
+ uart.printf("Connected to ThingSpeak server\n\r");
+ }
+ else
+ {
+ uart.printf("Connection attempt to ThingSpeak server failed\n\r");
+ attempt++;
+ }
+
+ if(client_th.is_connected() ) // try to send data
+ {
+ // METHODE POST
+
+ // update function
+ // POST /update HTTP/1.1\n
+ client_th.send(str0,strlen(str0));
+ uart.printf("%s >%d\n\r",str0,strlen(str0));
+ // Host: api.thingspeak.com\n
+ client_th.send(str1,strlen(str1));
+ uart.printf("%s >%d\n\r",str1,strlen(str1));
+ // Connection: close\n
+ client_th.send(str2,strlen(str2));
+ uart.printf("%s >%d\n\r",str2,strlen(str2));
+ // X-THINGSPEAKAPIKEY: PD6JY2N94E3N0UNO
+ sprintf(buffer_th,"%s %s\n",str3,write_key);
+ client_th.send(buffer_th,strlen(buffer_th));
+ uart.printf("%s >%d",buffer_th,strlen(buffer_th));
+ // Content-Type: application/x-www-form-urlencoded\n
+ client_th.send(str4,strlen(str4));
+ uart.printf("%s >%d",str4,strlen(str4));
+
+ sprintf(data_entry,"field1=%f\n",adc_v_moy ); //adc moyenne en volts
+ length=strlen(data_entry);
+ // Content-Length:
+ sprintf(buffer_th,"Content-Length: %d\n\n",length);
+ uart.printf("%s >%d\n\r",buffer_th,strlen(buffer_th));
+
+ uart.printf("%s >%d\n\r",data_entry,strlen(data_entry));
+ client_th.send(buffer_th,strlen(buffer_th));
+ client_th.send(data_entry,strlen(data_entry));
+
+ // METHODE GET
+ // http://api.thingspeak.com/update?api_key=PD6JY2N94E3N0UNO&field1=value
+
+ attempt = 4;
+ }
+ }
+
+ adc_v_moy_old = adc_v_moy;
+ }
+}
+
+//------ THING_SPEAK ------------------
+
+
char buffer[HTTPD_MAX_REQ_LENGTH+1];
char httpHeader[HTTPD_MAX_HDR_LENGTH+1];
char fileName[HTTPD_MAX_FNAME_LENGTH+1];
@@ -66,15 +176,6 @@
int refresh = 1; // 1 second refresh
-#define NB_SAMPLES 10
-unsigned long int Sample = 0;
-float adc_samples[NB_SAMPLES];// = { 0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0,1.01,1.02,1.03,1.04 };
-float time_samples[NB_SAMPLES];// = { -0.1,2,3,4,5,6,7,8,9,10,11,12,13,14 };
-float x_min = 0.0, x_max = 0.0;
-float y_min = 0.0, y_max = 0.0;
-float Seconds = 0.0;
-float meas = 0.0;
-
//------------
#define __Time_tic_in_Second__ 0.1
@@ -123,7 +224,7 @@
{
led_r = !led_r;
}
-
+
void printf_send_client(const char *str_c)
{
char http_send[HTTPD_MAX_REQ_LENGTH+1];
@@ -154,6 +255,7 @@
printf_send_client("</SCRIPT>\r\n");
}
+
void ETAT(void)
{
int i;
@@ -251,40 +353,35 @@
}
//--------------------------------------------
-
-int main(void)
+void Serial_Interface_init(void)
{
- // initialisation des variables
- int i;
-
- for(i = 0 ; i < NB_SAMPLES ; i++)
- {
- time_samples[i] = 0;
- adc_samples[i] = 0.0;
- }
- // Init the ticker with the address of the function (add_one_second) to be attached and the interval (1000 ms)
- time_tic.attach(&add_one_tic, __Time_tic_in_Second__);
- //--------------
- ledTick.attach(&ledTickfunc,0.5);
- // Serial Interface eth;
+ // Serial Interface eth;
// Serial port configuration (valeurs par defaut) : 9600 baud, 8-bit data, no parity, stop bit
uart.baud(9600);
uart.format(8, SerialBase::None, 1);
uart.printf("Initializing\n");
- wait(1.0);
-// Check File System
- uart.printf("Checking File System\n");
- DIR *d = opendir("/sd/");
- if(d != NULL)
- {
- uart.printf("SD Card Present\n");
- }
- else
- {
- uart.printf("SD Card Root Directory Not Found\n");
- }
- wait(1.0);
-// EthernetInterface eth;
+ wait(1.0);
+}
+
+void Check_File_System(void)
+{
+ // Check File System
+ uart.printf("Checking File System\n");
+ DIR *d = opendir("/sd/");
+ if(d != NULL)
+ {
+ uart.printf("SD Card Present\n");
+ }
+ else
+ {
+ uart.printf("SD Card Root Directory Not Found\n");
+ }
+ wait(1.0);
+}
+
+void ethernet_init(void)
+{
+ // EthernetInterface eth;
uart.printf("Initializing Ethernet\n");
#if USE_DHCP
//eth.init Use DHCP
@@ -301,13 +398,11 @@
if(eth.link() == true)
{
uart.printf("- Ethernet PHY Link - Done\r\n");
- //led_r = LED_ON;
COLOR(_RED_);
}
else
{
uart.printf("- Ethernet PHY Link - Fail\r\n");
- //led_r = LED_OFF;
COLOR(_BLACK_);
}
wait(1.0);
@@ -320,13 +415,11 @@
{
uart.printf("IP: %s, MASK: %s, GW: %s\r\n",
eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway());
- // led_b = LED_ON, led_g = LED_ON;
COLOR(_CYAN_);
}
else
{
uart.printf("Error ethernet.connect() - ret = %d\r\n", ret);
- //led_b = LED_OFF;
COLOR(_BLUE_);
exit(0);
}
@@ -334,17 +427,41 @@
else
{
uart.printf("Error ethernet.init() - ret = %d\r\n", ret);
- //led_b = LED_OFF;
COLOR(_BLACK_);
exit(0);
}
wait(1.0);
-// TCPSocketServer server;
+ // TCPSocketServer server;
server.bind(HTTPD_SERVER_PORT);
server.listen();
- uart.printf("Server Listening\n");
+ uart.printf("Server Listening\n");
+}
+int main(void)
+{
+ // initialisation des variables
+ int i;
+
+ for(i = 0 ; i < NB_SAMPLES ; i++)
+ {
+ time_samples[i] = 0;
+ adc_samples[i] = 0.0;
+ }
+
+ adc_v_moy = ain0.read()*3.3;
+ adc_v_moy_old = adc_v_moy;
+ //------------------
+ Serial_Interface_init();
+
+ // Init the ticker with the address of the function (add_one_second) to be attached and the interval (1000 ms)
+ time_tic.attach(&add_one_tic, __Time_tic_in_Second__);
+ //--------------
+ ledTick.attach(&ledTickfunc,0.5);
+ //---------------
+ Check_File_System();
+ ethernet_init();
+
while(true)
{
uart.printf("\nWait for new connection...\r\n");
@@ -385,11 +502,14 @@
*eou = 0;
//get_file(uristr);
ETAT();
+ thingspeak_update();
}
}
}
//led_g = LED_OFF;
COLOR(_BLACK_);
client.close();
+ client_th.close();
+ //
}
}
FOURNET Olivier