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

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