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:
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.
}