source: rtos_arduino/trunk/examples/IotText/sample4/rca_app.cpp@ 256

Last change on this file since 256 was 256, checked in by ertl-honda, 8 years ago

プログラムの見直し

File size: 2.5 KB
Line 
1#include "rca.h"
2
3#include "ESP8266.h"
4#include "../../examples_gdef.h"
5
6#define IFTTT_KEY ""
7#define EVENT_NAME ""
8
9#define HOST_NAME "maker.ifttt.com"
10#define HOST_PORT (80)
11
12#define WiFi wifi
13#define TOUCH_SW 2
14
15ESP8266 WiFi;
16
17void setup()
18{
19 int ret;
20
21 Serial.begin(115200);
22 Serial.print("IFTTT demo");
23
24 // Connect to WiFi access point.
25 Serial.println(); Serial.println();
26 Serial.print("Connecting to ");
27 Serial.println(STA_SSID);
28
29 ret = WiFi.begin(Serial5, 115200);
30
31 if(ret == 1) {
32 Serial.print("Cannot communicate with ESP8266.");
33 while(1);
34 } else if(ret == 2) {
35 Serial.println("FW Version mismatch.");
36 Serial.print("FW Version:");
37 Serial.println(WiFi.getVersion().c_str());
38 Serial.print("Supported FW Version:");
39 Serial.println(ESP8266_SUPPORT_VERSION);
40 while(1);
41 } else {
42 Serial.print("begin ok\r\n");
43 }
44
45 Serial.print("FW Version:");
46 Serial.println(WiFi.getVersion().c_str());
47
48 if (WiFi.setOprToStation()) {
49 Serial.print("to station ok\r\n");
50 } else {
51 Serial.print("to station err\r\n");
52 }
53
54 if (WiFi.joinAP(STA_SSID, STA_PASSWORD)) {
55 Serial.print("Join AP success\r\n");
56 Serial.print("IP: ");
57 Serial.println(WiFi.getLocalIP().c_str());
58 } else {
59 Serial.print("Join AP failure\r\n");
60 }
61
62 if (WiFi.stopServer()) {
63 Serial.print("Stop server ok\r\n");
64 } else {
65 Serial.print("Stop server err\r\n");
66 }
67
68 if (WiFi.disableMUX()) {
69 Serial.print("single ok\r\n");
70 } else {
71 Serial.print("single err\r\n");
72 }
73
74 Serial.println("setup end\r\n");
75}
76
77char buffer[1024];
78const char request1[] = "GET /trigger/" EVENT_NAME "/with/key/" IFTTT_KEY "/";
79const char request2[] = " HTTP/1.1\r\nHost: " HOST_NAME "\r\nConnection: close\r\n\r\n";
80
81void loop()
82{
83 static int value1 = 0, value2 = 1, value3 = 2;
84 int sw_push = 0;
85 static int sw_st = 1;
86
87 switch(sw_st) {
88 case 0:
89 if(digitalRead(TOUCH_SW)) {
90 sw_push = 1;
91 sw_st = 1;
92 }
93 break;
94 default:
95 if(!digitalRead(TOUCH_SW)) {
96 sw_st = 0;
97 }
98 break;
99 }
100
101 if(sw_push) {
102 value1++; value2++; value3++;
103
104 Serial.print("Send Touch Event and Extra Data ");
105 Serial.print(value1); Serial.print(", ");
106 Serial.print(value2); Serial.print(", ");
107 Serial.println(value3);
108
109 if (!WiFi.createTCP(HOST_NAME, HOST_PORT)) {
110 Serial.println("create tcp err");
111 return;
112 }
113
114 sprintf(buffer, "%s?value1=%d&value2=%d&value3=%d%s", request1, value1, value2, value3, request2);
115
116 WiFi.send((const uint8_t*)buffer, strlen(buffer));
117
118 uint32_t len = WiFi.recv((uint8_t*)buffer, sizeof(buffer), 10000);
119
120 if (!WiFi.releaseTCP()) {
121 Serial.println("release tcp err");
122 }
123 }
124}
Note: See TracBrowser for help on using the repository browser.