Skip to content

Commit aa50df0

Browse files
committed
add thingspeak example, using 512bytes buffer
1 parent 3a55952 commit aa50df0

File tree

10 files changed

+108
-8
lines changed

10 files changed

+108
-8
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ esp8266/tools/esptool.py -p COM1 write_flash 0x00000 esp8266/debug/0x00000.bin 0
5555

5656
**4. Import arduino library and run example:**
5757

58+
Example read DHT11 and send to [thingspeak.com](http://thingspeak.com)
59+
=========
60+
- Using DHT11 library from: [https://github.com/RobTillaart/Arduino](https://github.com/RobTillaart/Arduino)
61+
62+
![](images/thingspeak.png)
5863

5964
Example for MQTT client
6065
=======

esp8266/debug/0x00000.bin

112 Bytes
Binary file not shown.

esp8266/debug/0x40000.bin

128 Bytes
Binary file not shown.

esp8266/release/0x00000.bin

0 Bytes
Binary file not shown.

esp8266/release/0x40000.bin

-32 Bytes
Binary file not shown.

espduino/espduino.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,12 @@ void ESP::protoCompletedCb(void)
106106
}
107107
}
108108

109-
void ESP::wifiConnect(String ssid, String password)
109+
void ESP::wifiConnect(const char* ssid, const char* password)
110110
{
111111
uint16_t crc;
112112
crc = request(CMD_WIFI_CONNECT, (uint32_t)&wifiCb, 0, 2);
113-
crc = request(crc,(uint8_t*)ssid.c_str(), ssid.length());
114-
crc = request(crc,(uint8_t*)password.c_str(), password.length());
113+
crc = request(crc,(uint8_t*)ssid, strlen(ssid));
114+
crc = request(crc,(uint8_t*)password, strlen(password));
115115
request(crc);
116116
}
117117

@@ -138,7 +138,6 @@ uint16_t ESP::request(uint16_t crc_in, uint8_t* data, uint16_t len)
138138
uint16_t pad_len = len;
139139
while(pad_len % 4 != 0)
140140
pad_len++;
141-
142141
_serial->write((uint8_t*)&pad_len, 2);
143142
crc_in = crc16_data((uint8_t*)&pad_len, 2, crc_in);
144143
while(len --){

espduino/espduino.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class ESP
9494
uint16_t return_cmd;
9595
boolean is_return;
9696

97-
void wifiConnect(String ssid, String password);
97+
void wifiConnect(const char* ssid, const char* password);
9898
void process();
9999
uint16_t request(uint16_t cmd, uint32_t callback, uint32_t _return, uint16_t argc);
100100
uint16_t request(uint16_t crc_in, uint8_t* data, uint16_t len);
@@ -110,7 +110,7 @@ class ESP
110110

111111
boolean _debugEn;
112112
PROTO _proto;
113-
uint8_t _protoBuf[1024];
113+
uint8_t _protoBuf[512];
114114
int _chip_pd;
115115

116116

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/**
2+
* \file
3+
* ESP8266 RESTful Bridge example
4+
* \author
5+
* Tuan PM <[email protected]>
6+
*/
7+
8+
#include <SoftwareSerial.h>
9+
#include <espduino.h>
10+
#include <rest.h>
11+
#include <dht.h>
12+
13+
dht DHT;
14+
15+
16+
SoftwareSerial debugPort(2, 3); // RX, TX
17+
18+
ESP esp(&Serial, &debugPort, 4);
19+
20+
REST rest(&esp);
21+
22+
boolean wifiConnected = false;
23+
24+
void wifiCb(void* response)
25+
{
26+
uint32_t status;
27+
RESPONSE res(response);
28+
29+
if(res.getArgc() == 1) {
30+
res.popArgs((uint8_t*)&status, 4);
31+
if(status == STATION_GOT_IP) {
32+
debugPort.println("WIFI CONNECTED");
33+
34+
wifiConnected = true;
35+
} else {
36+
wifiConnected = false;
37+
}
38+
39+
}
40+
}
41+
42+
void setup() {
43+
Serial.begin(19200);
44+
debugPort.begin(19200);
45+
esp.enable();
46+
delay(500);
47+
esp.reset();
48+
delay(500);
49+
while(!esp.ready());
50+
51+
debugPort.println("ARDUINO: setup rest client");
52+
if(!rest.begin("api.thingspeak.com")) {
53+
debugPort.println("ARDUINO: failed to setup rest client");
54+
while(1);
55+
}
56+
57+
/*setup wifi*/
58+
debugPort.println("ARDUINO: setup wifi");
59+
esp.wifiCb.attach(&wifiCb);
60+
61+
esp.wifiConnect("DVES_HOME","wifipassword");
62+
debugPort.println("ARDUINO: system started");
63+
}
64+
65+
void loop() {
66+
char response[266];
67+
esp.process();
68+
if(wifiConnected) {
69+
int chk = DHT.read11(7);
70+
if(chk == DHTLIB_OK){
71+
char buff[64];
72+
char str_hum[6], str_temp[6];
73+
dtostrf(DHT.humidity, 4, 2, str_hum);
74+
dtostrf(DHT.temperature, 4, 2, str_temp);
75+
sprintf(buff, "/update?api_key=MAY03AKJDMPP4Y4I&field1=%s&field2=%s", str_hum, str_temp);
76+
debugPort.println(buff);
77+
rest.get((const char*)buff);
78+
debugPort.println("ARDUINO: send get");
79+
80+
if(rest.getResponse(response, 266) == HTTP_STATUS_OK){
81+
debugPort.println("ARDUINO: GET successful");
82+
debugPort.println(response);
83+
}
84+
delay(30000);
85+
86+
} else {
87+
debugPort.print("error,\r\n");
88+
}
89+
90+
91+
}
92+
93+
94+
}

espduino/rest.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ void REST::restCallback(void *resp)
1919

2020
boolean REST::begin(const char* host, uint16_t port, boolean security)
2121
{
22-
22+
uint8_t sec = 0;
23+
if(security)
24+
sec = 1;
2325
restCb.attach(this, &REST::restCallback);
2426

2527
uint16_t crc = esp->request(CMD_REST_SETUP, (uint32_t)&restCb, 1, 3);
2628
crc = esp->request(crc,(uint8_t*)host, strlen(host));
2729
crc = esp->request(crc,(uint8_t*)&port, 2);
28-
crc = esp->request(crc,(uint8_t*)&security, 1);
30+
crc = esp->request(crc,(uint8_t*)&sec, 1);
2931
esp->request(crc);
3032

3133
if(esp->waitReturn(timeout) && esp->return_value != 0){

images/thingspeak.png

12.7 KB
Loading

0 commit comments

Comments
 (0)