Gbox 4.20
Grow box automation and monitoring - <a href='https://sites.google.com/site/growboxguy/'>https://sites.google.com/site/growboxguy/</a>
 
Loading...
Searching...
No Matches
demo.ino
Go to the documentation of this file.
1
6#include <espduino.h>
7#include <rest.h>
8
9ESP esp(&Serial, 4);
10
11REST rest(&esp);
12
13boolean wifiConnected = false;
14
15void wifiCb(void* response)
16{
17 uint32_t status;
18 RESPONSE res(response);
19
20 if(res.getArgc() == 1) {
21 res.popArgs((uint8_t*)&status, 4);
22 if(status == STATION_GOT_IP) {
23 Serial.println("WIFI CONNECTED");
24
25 wifiConnected = true;
26 } else {
27 wifiConnected = false;
28 }
29 }
30}
31
32void setup() {
33 Serial.begin(115200);
34 delay(10);
35 Serial.println("ARDUINO: starting");
36 esp.enable();
37 delay(10);
38 esp.reset();
39 delay(10);
40 while(!esp.ready()) {
41 Serial.println("ARDUINO: ESP not ready...");
42 delay(1000);
43 }
44
45 Serial.println("ARDUINO: setup rest client");
46 if(!rest.begin("api.thingspeak.com")) {
47 Serial.println("ARDUINO: failed to setup rest client");
48 while(1);
49 }
50
51 /*setup wifi*/
52 Serial.println("ARDUINO: setup wifi");
53 esp.wifiCb.attach(&wifiCb);
54
55 esp.wifiConnect("","");
56 Serial.println("ARDUINO: system started");
57}
58
59void printChar(char c) {
60 if (c < ' ' || c >= '~') {
61 Serial.print("\\x");
62 uint8_t c1 = (uint8_t)c >> 4;
63 Serial.print((char)(c1 >= 10 ? 'A'+c1-10 : '0' + c1));
64 uint8_t c2 = (uint8_t)c & 0xf;
65 Serial.print((char)(c2 >= 10 ? 'A'+c2-10 : '0' + c2));
66 } else {
67 Serial.print(c);
68 }
69}
70
71uint32_t timer = 0;
72
73void loop() {
74 char response[266];
75 esp.process();
76 if(wifiConnected) {
77 char buff[64];
78 uint32_t t0 = millis();
79 sprintf(buff, "/update?api_key=MAY03AKJDMPP4Y4I&field1=%ld", timer);
80 Serial.print("ARDUINO: send get ");
81 Serial.println(buff);
82 rest.get((const char*)buff);
83
84 uint16_t err = rest.getResponse(response, 266);
85 if(err == HTTP_STATUS_OK){
86 Serial.println("ARDUINO: GET successful");
87 Serial.print("ARDUINO: got <<");
88 int len = strlen(response);
89 for (int i=0; i<len; i++) printChar(response[i]);
90 Serial.println(">>");
91 } else if (err == 0) {
92 Serial.println("ARDUINO: GET timed out");
93 } else {
94 Serial.print("ARDUINO: GET failed: ");
95 Serial.println(err);
96 }
97 timer = millis() - t0;
98 Serial.print("ARDUINO: took ");
99 Serial.print(timer);
100 Serial.println("ms");
101 delay(3000);
102 }
103 delay(100);
104}
@ HTTP_STATUS_OK
@ STATION_GOT_IP
Definition ELClient.h:51
uint32_t timer
Definition demo.ino:71
REST rest & esp
Definition demo.ino:11
void wifiCb(void *response)
Definition demo.ino:15
void setup()
Definition demo.ino:32
ESP esp & Serial
Definition demo.ino:9
boolean wifiConnected
Definition demo.ino:13
void printChar(char c)
Definition demo.ino:59
void loop()
Definition demo.ino:73