Access device using web browser and also send data to Thingspeak

Hello everyone,

I want to send data to Thingspeak, and also connect to my Arduino-like device using a web browser. I am using an ESP8266 and I'm programming it with the Arduino IDE 1.6.9 with the ESP8266 add-in module.

In essence, I want to be able to access the ESP8266 via a web browser to not only see the data but to also use it for debugging (once it's programmed it gets connected outside and it's difficult to connect via a serial cable - so if I can get this functionality working it's something I expect to use for any other ESP8266 project).

Functional requirements:

  1. Every 60 seconds, check for data (to keep it simple, we'll just call it 'x')

  2. If a client connects to the local webserver, send them the value of 'x'

  3. Every 5 minutes, send the value of 'x' to Thingspeak

I have managed to write some code that reads and displays my data using a web server, and I have written some code that reads the data and sends it to Thingspeak. However, combining the two doesn't seem to work. The more I look at example code, the more confused I get - but at the same time I have a feeling that there is a much simpler way of doing things...

Any suggestions?

Ewan

but to also use it for debugging

You haven't a hope in hell of doing that. While you can program the ESP8266 to respond to basic "commands" to dump data, and you could devise commands to change the data, you can not set break points or change where the code is executing.

However, combining the two doesn't seem to work.

It is a lot more challenging to write code that handles both the server role AND the client role. If you have done that, you seem to have forgotten to post it and explain what it actually does and how that differs from what you expect.

Without that, we can't help adjust your expectations.

but at the same time I have a feeling that there is a much simpler way of doing things...

Well, of course there is. Get a real computer.

Any suggestions?

Buy low; sell high.

dukbilt:
In essence, I want to be able to access the ESP8266 via a web browser to not only see the data but to also use it for debugging (once it's programmed it gets connected outside and it's difficult to connect via a serial cable...

Read up on over-the-air programming. Is that what you want to do?

Well, I have something that works, and it seems to be simpler than most other client/server examples I have seen. I'd appreciate any suggestions on how to tidy things up, or other approaches that could simplify things further.

The debugging section is extremely limited at the moment, however in future projects I will have more information to display - this example just shows that the client and server functionality works together. I have commented as much as I understand so that hopefully it's crystal clear what's going on.

One thing I found that may have been an ongoing issue was that connecting to Thingspeak was successful only when the IP address was used - the name "api.thingspeak.com" and variations of that failed.

Hope this helps someone else,

Ewan (dukbilt)

/* Server and Client functionality for ESP8266

Sends data to Thingspeak and also shows sent data and response on a web page hosted on
the ESP8266 for local information to aid in debugging.

Version: 1.0 (07 Jul 16)
Hardware: ESP8266 module (specifically the Sparkfun ESP8266 Thing)
IDE: Arduino 1.6.9

Author: Ewan Regazzo */

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiClient.h>

// WiFi Definitions
#define SSID "myssid" // WiFi SSID
#define PSK "mypassword" // WiFi password

// Thingspeak definitions
#define SERVER "184.106.153.149" // Thingspeak server address (don't use 'api.thingspeak.com' - it doesn't work)
#define PORT 80 // Thingspeak port
#define API "myapiwritekey" // Thingspeak 'Write' key for this channel

/* NOTE 1 - The instance of ESP8266WebServer called 'server' uses port 81.
Port 81 must be used as Thingspeak uses port 80, so the web server by typing
XXX.XXX.XXX.XXX:81 into your browser window where XXX.XXX.XXX.XXX is the IP
address of the ESP8266 - ie, '192.168.0.5:81'
The '#include <ESP8266WebServer.h>' statement at the top of this code is
essential in allowing the 'server' instance to be created.*/

ESP8266WebServer server(81); // Create instance of ESP8266WebServer called 'server' using port 81 (see note 1 above)

WiFiClient client; // Create an instance of WiFiClient called 'client'

// Global variables (usable by all functions)
int x = 0; // Simple counter to show that something changes
String message; // The information needed to update Thingspeak is kept in a string called 'message'
String response; // Holds the response information from Thingspeak after data has been sent

// The 'setup' function is called at startup only and sets up the ESP8266
void setup()
{
WiFi.begin(SSID, PSK); // Begin the WiFi connection using SSID and PSK definitions

// Pause for 0.5 seconds while WiFi is NOT connected
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
}

// we've gotten to here so WiFi must now be connected

// Set up the web server
server.on("/", handleRoot); // If a client accesses the root page ('/') then process the 'handleRoot' function
server.begin(); // Start the server
}

// The main function that does everything.
void loop()
{
createData(); // Make the data message
server.handleClient(); // Take care of any client that accesses the web server
sendData(); // send data to Thingspeak
delay(5000); // Have a break
}

// This function takes care of a client accessing the root web server page
void handleRoot()
{
server.send(200, "text/plain", (message + "\n" + response)); // send a message to the web server
}

// This function sets the ESP8266 as a client to an external server (Thingspeak), sends data,
// and receives a response
void sendData()
{
if (client.connect(SERVER, PORT)) // If a successful connection occurs, then...
{
client.println(message); // ... send the information
}

if (client.connected()) // If we have connected...
{
response = client.read(); // ... read the response from the server

if (response == "48") // string value of "48" equals char '0' - which means data received ok
{
x ++; // increment x by 1 (just to show that something happened)
}
}

client.stop(); // We don't want the client to maintain a constant connection
}

// This function creates a simple GET message string to update Thingspeak
void createData()
{
message = "GET /update?api_key=";
message += API;
message += "&field1=";
message += x;
}

Works Ok. Only one thing:
"Port 81 must be used as Thingspeak uses port 80"

Port 80 is OK. You have 2 different servers