Need help debugging code for WiFi usage

Program works fine for displaying results on old monitor. But when I added code for WiFi got a host of compiling error messages. Hardware uses an Arduino Uno and ESP8266-01S WiFi module.

I used the following source as a template for my WiFi code:

http://www.arduinesp.com/thingspeak

I even copied their code verbatim to a new sketch, and got similar error messages during compilation.

My sketch code follows.

/*
  H2O_Flo_WiFi
 
 Measures the flow rate of water flowing through a 3/4-inch PVC pipe using a DIGITEN Hall effect sensor and displaying results on thingspeak.com using an ESP8266-01S
 Sensor date wire connected to Uno pin D2
 The sensor output is in the form of the duration in milliseconds of a square wave pulse and the relationship to the water flowrate is:
 
 F = 4.8 * Q  ( in liters per minute)
 
 */

// Libraries
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <ESP8266WiFi.h>

// WiFi SSD and Password

const char* ssid;
ssid = “tracy”;
const char* password;
password = “Mange161”;

const char* server = "api.thingspeak.com";
//Initialize the client library
WiFiClient client;

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

const int sensdatapin = 2;

String APIKey;
APIKey = "0ZUS9CDG08ZU6VJ2";

unsigned long myChannelNumber;
myChannelNumber = 255517;

void setup()
{
  unsigned long duration;
  float dursec;
  float pulsefreq;
  float flolit;
  float flolitr;
  float flogal;
  float flocuft;

  pinMode(sensdatapin, INPUT);

  Serial.begin(9600);

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  //initialize with the I2C addr 0x3C (128x64)

  // init done

  // Show image buffer on the display hardware.
  // Since the buffer is intialized with an Adafruit splashscreen
  // internally, this will display the splashscreen.
  display.display();
  delay(2000);

  display.clearDisplay();
  // clear the display before start

  // text display tests
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.println("Hello, world!");
  display.display();
  delay(2000);
  display.clearDisplay();

  WiFi.begin(ssid, password); 

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");

}

void loop()
{

  const unsigned long duration = pulseIn(sensdatapin, HIGH);
  //Pulse duration in microseconds

  float dursec = duration / 1000000.0;
  //Pulse duration in seconds

  float pulsefreq = 10.0;

  if ( duration  != 0 )
  {
    pulsefreq = 1.0 / dursec;
  }
  else
  {
    pulsefreq = 0.0;
  }

  //Pulse frequency in Hz

  float flolit = (pulsefreq  / 4.8);
  float flolitr = 60.0 * flolit;
  //Flow rate in Liters/hour

  float flogal = flolitr * .26417;
  //Flow rate in Gallons/hour
  float flocuft = flolitr * .035315;
  //Flow rate in cu ft/hour

  display.setCursor(22, 20);
  //x,y coordinates
  display.setTextSize(1);
  //size of the text
  display.setTextColor(WHITE);
  display.println(flogal);
  display.display();
  display.setCursor(85, 20);
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.println("Gal/hr");
  //print "Gal/hr" in oled
  display.display();
  delay(1000);
  display.clearDisplay();

  if (client.connect(server,80)) {  //   "184.106.153.149" or api.thingspeak.com
    String postStr = apiKey;
    postStr +="&field1=";
    postStr += String(flogal);
    postStr += "\r\n\r\n";

    client.print("POST /update HTTP/1.1\n"); 
    client.print("Host: api.thingspeak.com\n"); 
    client.print("Connection: close\n"); 
    client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n"); 
    client.print("Content-Type: application/x-www-form-urlencoded\n"); 
    client.print("Content-Length: "); 
    client.print(postStr.length()); 
    client.print("\n\n"); 
    client.print(postStr);
  }

  // Write to ThingSpeak. There are up to 8 fields in a channel, allowing you to store up to 8 different
  // pieces of information in a channel.  Here, we write to field 1.

  ThingSpeak.writeField(myChannelNumber, 1, flogal, APIKey);

  delay(20000);
  // ThingSpeak will only accept updates every 15 seconds.

}

johndey:
But when I added code for WiFi got a host of compiling error messages.

Then why don't you post those error messages?

The code is meant to run on the ESP8266, not on the Arduino UNO, that's why it won't compile.

Pieter

Well that answers the compile error question. In the example, the WiFi module is able to accept data directly from the sensor. For my project, I must process the sensor output using the Uno. I need to find a different template to use.

johndey:
For my project, I must process the sensor output using the Uno.

Why?
The ESP8266 is many times faster than the Uno, and it has much more memory.
You can check out my Beginner's guide to the ESP8266 for more information and how to program it.

If you really want to use the Uno, you could look into AT commands (I'm not a big fan), or you could write your own ESP8266 program that accepts sensor data over Serial.

Pieter

I'm going to take your advice, Pieter, and not use the Uno. The result will be a much cleaner system. Any advice regarding a 110v - 5v external power source?

johndey:
Any advice regarding a 110v - 5v external power source?

A 5V phone charger will probably do the trick.

I found a device that will snap on to a MB102 breadboard. Input is standard 12v plug that is compatible with the Uno, and output is 5v (to one breadboard bus) and 3.3v (to the other bus).

Cost is USD3.59 including shipping (from US and to US).

I personally like to use my lab power supply for experimenting and prototyping, because it has current limiting (so I don't blow up anything when accidentally shorting something out :slight_smile: ). But a breadboard power module should also work just fine, and it's very handy to have laying around.