ESP8266 to Thingspeak

Caveat here I am not a programmer in any fashion.

I wanted to start to grow veg and being not the responsible person to remember to water and look after plants I decided to make things simple for me. Also as I have a strange obsession to see graphs I wanted to be able to log all sensors and see them in a pretty graph (No reason why, just like graphs).

With this is mind I thought I would start in testing how to log to a web provider like www.thingspeak.com

After looking into many tutorials and not liking seeing the libraries for the ESP8266 I thought I would go through the pain in hacking them all to make it work for what I want it to do. Below is the code I eventually came up with.

This code is just a simple connect to services and create some random numbers that posts to the website in two fields. It can be easily adapted and refined (call all fields in a single function once you know how many you need).

Hope this helps and I will try and answer questions if I can.

This script will create two random numbers and post each number in Field 1 and Field 2 then wait 2 mins. The free version needs to wait 15 seconds before you can post so hence the Delay between calling the other sendData command in Void Loop.

All your code will be in [VOID LOOP] and then just amend the sendData as needed but make sure you create enough Fields in ThingSpeak

I hope this can help someone.

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#define WIFI_SSID "Your SSID" //Set your Wifi name
#define WIFI_PASS "Your Password" //Set your Wifi Password
#define SERIAL_BAUDRATE 9600
#define TSKEY "Your Creation API Key" // Thingspeak Key
#define IP "184.106.153.149" // thingspeak.com
int Random = 0;
long randNumber;
int brightness;
int Field;

void setup() {
Serial.begin(SERIAL_BAUDRATE);
wifiSetup();
}

void loop() {
randNumber = random(10, 20);
Field = 1;
sendData (String(Field), String(randNumber));
randNumber = random(10, 20);
delay(30000);
Field = 2;
sendData (String(Field), String(randNumber));
delay(120000);
}

void wifiSetup() {

// Set WIFI module to STA mode
WiFi.mode(WIFI_STA);

// Connect
Serial.printf("[WIFI] Connecting to %s ", WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASS);

// Wait
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(100);
}
Serial.println();

// Connected!
Serial.printf("[WIFI] STATION Mode, SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
}

void sendData(String Field, String tBrightness){
HTTPClient http;
String cmd = "http://api.thingspeak.com/update?key=";
cmd += TSKEY;
cmd += "&field";
cmd += Field;
cmd += "=";
cmd += tBrightness;
http.begin(cmd);
Serial.print ("Sending: ");
Serial.println (cmd);
int httpCode = http.POST(cmd); //Send the request
String payload = http.getString(); //Get the response payload
Serial.println (payload);
delay(2000);
if(Serial.find("Error")){
return;
}
}

If your a beginner program, why don't you use the libraries that are out there. I don't know what you mean by not liking them if your not an experienced programmer. As long as they work for you why not use them as is.

I would offer that a simple relay and a pump would be much better suited to keeping the plants alive than a txt message that they need water while you are on your way to work.

the total cost for a relay and pump is less than $10 (maybe half of that)

but by all means, do also to the thingspeak.
probably also monitor the water level in the tank and also send a reminder at 6:00 AM every day
just to give the plants a chance to get some water well before they wilt.

do both !

noweare:
If your a beginner program, why don't you use the libraries that are out there. I don't know what you mean by not liking them if your not an experienced programmer. As long as they work for you why not use them as is.

Probably worded it wrong. The libraries were for Ethernet and not WiFi plus the examples were not for the ESP8266. I had to change the code then the post http libraries were different too but yes I do agree with your comment. I just wanted to refine this for what I am using.

dave-in-nj:
I would offer that a simple relay and a pump would be much better suited to keeping the plants alive than a txt message that they need water while you are on your way to work.

the total cost for a relay and pump is less than $10 (maybe half of that)

but by all means, do also to the thingspeak.
probably also monitor the water level in the tank and also send a reminder at 6:00 AM every day
just to give the plants a chance to get some water well before they wilt.

do both !

You're right but if there is a way of overly complicate things............. I will find it. :slight_smile:

What is thingspeak actually used for in this project? Collecting/saving the data to make pretty graphs later on?
Or are you trying to control things over the internet? (I'm not clear...sorry)

Maybe eliminate the Thingspeak stuff.. and use a local web server? (Like WAMP or something?)

You get the php side of things as well as a (MySQL) database to log your data and make pretty graphs from.