ESP8266 (Arduino IDE) and Thingspeak with Webserver for debugging | ThingSpeak Projects | Forum

Avatar

Please consider registering
Guest

sp_LogInOut Log In sp_Registration Register

Register | Lost password?
Advanced Search

— Forum Scope —






— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

sp_Feed sp_TopicIcon
ESP8266 (Arduino IDE) and Thingspeak with Webserver for debugging
No permission to create posts
July 4, 2016
10:13 pm
Avatar
dukbilt

Silver
Members
Forum Posts: 7
Member Since:
July 2, 2016
sp_UserOfflineSmall Offline

Hello everyone,

I wish to use an ESP8266 to send data to Thingspeak however I also want to use display information locally. Can anyone offer assistance on this? I can send data to Thingspeak, and I can display data using a webserver, however I can't work out how to combine both functions.

Any ideas?

Thanks,

Ewan

 

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

// WiFi Definitions
#define SSID "*****" // WiFi SSID
#define PSK "*****" // WiFi password

// Thingspeak definitions
#define SERVER "api.thingspeak.com" // Thingspeak server address
#define PORT 80 // Thingspeak port
#define API "*****" // Thingspeak 'Write' key for this channel

int i = 0;

ESP8266WebServer server(80);
WiFiClient client;

void handleRoot()
{
String message = "hello from esp8266!
";
message += i;
server.send(200, "text/plain", message);
i ++;
}

void setup(void)
{
WiFi.begin(SSID, PSK);

// Wait for connection
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
}

server.on("/", handleRoot);
server.begin();
}

void loop(void)
{
server.handleClient();

client.connect(SERVER, PORT);
String postStr = API;
postStr +="&field1=";
postStr += String(i);
postStr +="&field2=";
postStr += String(i);
postStr += "

";

client.print("POST /update HTTP/1.1
Host:");
client.print(SERVER);
client.print("Connection: close
");
client.print("X-THINGSPEAKAPIKEY: ");
client.print(API);
client.print("
Content-Type: application/x-www-form-urlencoded
");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("

");
client.print(postStr);
delay (10000);
}

July 4, 2016
10:31 pm
Avatar
rw950431

Top
Members
Forum Posts: 261
Member Since:
January 30, 2014
sp_UserOfflineSmall Offline

What do you mean by "display data using a webserver"?  Can you post the code that does that function?

I'm guessing you want to POST data to thingspeak as a client but also act as a server for local clients..

July 4, 2016
11:34 pm
Avatar
dukbilt

Silver
Members
Forum Posts: 7
Member Since:
July 2, 2016
sp_UserOfflineSmall Offline

Correct - ideally I want to be able to see what data is being sent to Thingspeak by accessing the ESP8266 webserver.

The code above is my (unworking) version that is based on my very simple webserver example. I tried to add my very basic Thingspeak functionality with my very basic webserver example, however it doesn't work.

One of the main reasons I want to do this is so I can debug easily without adding in a heap of serial print information. 

July 5, 2016
7:09 pm
Avatar
rw950431

Top
Members
Forum Posts: 261
Member Since:
January 30, 2014
sp_UserOfflineSmall Offline

Does it post data to Thingspeak properly if you remove the server.handleClient(); from the main loop?

Does it serve local clients if you comment out the Thingspeak update section in the main loop?

 

One of the issues you have is that the server will only check for incoming requests every 10 seconds so your local clients may timeout waiting.   You need to eliminate delay() so the clients get handled quickly.  See the Doing several things at once post on the arduino forum and the "Blink without delay" example in the Arduino IDE.

 

You should also be aware the 15 seconds is the min time between TS updates so at the moment every 2nd update would fail anyway.

July 6, 2016
5:23 pm
Avatar
dukbilt

Silver
Members
Forum Posts: 7
Member Since:
July 2, 2016
sp_UserOfflineSmall Offline

Hello everyone,

after much head scratching I have a functional solution (see below).

If anyone has suggestions on how to improve it, I'm all ears. I have extensively commented it to assist in other people re-using it.

Bye,

Ewan

 

/* 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 + "
" + 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;
}

 
July 6, 2016
9:38 pm
Avatar
rw950431

Top
Members
Forum Posts: 261
Member Since:
January 30, 2014
sp_UserOfflineSmall Offline

Its great that you got it to work.

One improvement might be to eliminate the delay() call so you can serve clients in a more timely fashion. (You obviously need to declare the variables and initialise them in setup()

 

void loop()
{

  now=millis();
  createData(); // Make the data message
  server.handleClient(); // Take care of any client that accesses the web server
  //once every 15 seconds send to Thingspeak

  if (now - last_mills > 15000) {

     sendData(); // send data to Thingspeak

     last_millis=now;

  }
 
}

July 7, 2016
12:09 am
Avatar
dukbilt

Silver
Members
Forum Posts: 7
Member Since:
July 2, 2016
sp_UserOfflineSmall Offline

Thanks for that - I have heard that using delays can cause issues with the ESP8266, so eliminating it will be useful.

Forum Timezone: America/New_York

Most Users Ever Online: 114

Currently Online: Hans, agrisensezm
37 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 261

Vinod: 196

piajola: 85

turgo: 70

vespapierre: 63

Adarsh_Murthy: 62

Member Stats:

Guest Posters: 1

Members: 5703

Moderators: 0

Admins: 2

Forum Stats:

Groups: 4

Forums: 17

Topics: 1313

Posts: 4565

Newest Members:

Archieter, JamesExedo, IrbosHich, brodi, kasko, franklin castro

Administrators: Hans: 387, lee: 457