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
thingspeak.ino
Go to the documentation of this file.
1
8#include <ELClient.h>
9#include <ELClientRest.h>
10
11//###########################################################
12// For ARDUINO UNO WIFI with I2C to serial chip!
13//###########################################################
14// Serial port to ESP8266
15// #include <SC16IS750.h>
16// SC16IS750 i2cuart = SC16IS750(SC16IS750_PROTOCOL_I2C,SC16IS750_ADDRESS_AA);
17
18// Initialize a connection to esp-link using the I2Cuart chip of the Arduino Uno WiFi board for
19// SLIP messages.
20// ELClient esp(&i2cuart);
21
22//###########################################################
23// For boards using the hardware serial port!
24//###########################################################
25// Initialize a connection to esp-link using the normal hardware serial port both for
26// SLIP and for debug messages.
28
29// Initialize a REST client on the connection to esp-link
31
32boolean wifiConnected = false;
33
34// Callback made from esp-link to notify of wifi status changes
35// Here we print something out and set a global flag
36void wifiCb(void *response) {
37 ELClientResponse *res = (ELClientResponse*)response;
38 if (res->argc() == 1) {
39 uint8_t status;
40 res->popArg(&status, 1);
41
42 if(status == STATION_GOT_IP) {
43 Serial.println("WIFI CONNECTED");
44 wifiConnected = true;
45 } else {
46 Serial.print("WIFI NOT READY: ");
47 Serial.println(status);
48 wifiConnected = false;
49 }
50 }
51}
52
53void setup() {
54 Serial.begin(9600);
55//###########################################################
56// For ARDUINO UNO WIFI with I2C to serial chip!
57//###########################################################
58 // i2cuart.begin(9600);
59
60 Serial.println("");
61 Serial.println("EL-Client starting!");
62
63 // Sync-up with esp-link, this is required at the start of any sketch and initializes the
64 // callbacks to the wifi status change callback. The callback gets called with the initial
65 // status right after Sync() below completes.
66 esp.wifiCb.attach(wifiCb); // wifi status change callback, optional (delete if not desired)
67 bool ok;
68 do {
69 ok = esp.Sync(); // sync up with esp-link, blocks for up to 2 seconds
70 if (!ok) {
71 Serial.print("\nEL-Client sync failed! err: ");
72 Serial.println(ok);
73 }
74 } while(!ok);
75 Serial.println("EL-Client synced!");
76
77 // Wait for WiFi to be connected.
78Serial.println("esp.GetWifiStatus()");
79 esp.GetWifiStatus();
80 ELClientPacket *packet;
81 Serial.println("Waiting for WiFi ");
82 if ((packet=esp.WaitReturn()) != NULL) {
83 Serial.print(".");
84 Serial.println(packet->value);
85 }
86 Serial.println("");
87
88 // Set up the REST client to talk to api.thingspeak.com, this doesn't connect to that server,
89 // it just sets-up stuff on the esp-link side
90 // int err = rest.begin("api.thingspeak.com");
91 int err = rest.begin("184.106.153.149");
92 if (err != 0) {
93 Serial.print("REST begin failed: ");
94 Serial.println(err);
95 while(1) ;
96 }
97 Serial.println("EL-REST ready");
98}
99
100float solarValue = 99.5;
101// Change to your own Thingspeak API key
102char *api_key = "K9LDRXS7BXSN8X1J";
103// expand buffer size to your needs
104#define BUFLEN 266
105
106void loop() {
107 // process any callbacks coming from esp_link
108 esp.Process();
109
110 // if we're connected make an REST request
111 if(wifiConnected) {
112
113 // Generate a fake value starting from 100 going up to 300
114 solarValue = solarValue + 0.5;
115 if (solarValue == 300) {
116 solarValue = 100;
117 }
118 String solarValString = String(solarValue);
119 const char *solarValChar = solarValString.c_str();
120
121 // Reserve a buffer for sending the data
122 char path_data[BUFLEN];
123 // Copy the path and API key into the buffer
124 sprintf(path_data, "%s", "/update?api_key=");
125 sprintf(path_data + strlen(path_data), "%s", api_key);
126
127 // Copy the field number and value into the buffer
128 // If you have more than one field to update,
129 // repeat and change field1 to field2, field3, ...
130 sprintf(path_data + strlen(path_data), "%s", "&field1=");
131 sprintf(path_data + strlen(path_data), "%s", solarValChar);
132
133 // Send PUT request to thingspeak.com
134 rest.post(path_data,"");
135
136 // Reserve a buffer for the response from Thingspeak
137 char response[BUFLEN];
138 // Clear the buffer
139 memset(response, 0, BUFLEN);
140 // Wait for response from Thingspeak
141 uint16_t code = rest.waitResponse(response, BUFLEN-1);
142 // Check the response from Thingspeak
143 if(code == HTTP_STATUS_OK){
144 Serial.println("Thingspeak: POST successful:");
145 Serial.print("Response: ");
146 Serial.println(response);
147 } else {
148 Serial.print("Thingspeak: POST failed with error ");
149 Serial.println(code);
150 Serial.print("Response: ");
151 Serial.println(response);
152 }
153 // Send next data in 20 seconds
154 delay(20000);
155 }
156}
Definitions for ELClientRes.
@ HTTP_STATUS_OK
Definitions for ELClient.
@ STATION_GOT_IP
Definition ELClient.h:51
int16_t popArg(void *data, uint16_t maxLen)
Extract an argument from the response packet.
uint32_t value
ELClientRest rest & esp
void wifiCb(void *response)
void setup()
boolean wifiConnected
ELClient esp & Serial
#define BUFLEN
float solarValue
char * api_key
void loop()