ESP32 wind Speed (Anemometer) thingspeak+fix IP wifi

// create 04-dec-2019

#include<WiFi.h>
#include “ThingSpeak.h”

//—————- Fill in your credentails thingspeak 2019 ———————
char ssid[] = “XXXX”; // your network SSID (name)
char pass[] = “xxxxxx”; // your network password
unsigned long myChannelNumber = xxxxxx; // Replace the 0 with your channel number
const char * myWriteAPIKey = “xxxxxxxxxxxx”; // Paste your ThingSpeak Write API Key between the quotes
String myStatus = “”;
//——————————————————————

WiFiClient client;
int number = 0;
//Static IP address configuration
IPAddress staticIP(192, 168, xx,xx); //ESP static IP address
IPAddress gateway(192, 168, xx, 1); //IP Address of your WiFi Router (Gateway)
IPAddress subnet(255, 255, 255, 0); //Subnet mask
IPAddress primaryDNS(8, 8, 8, 8); //optional
IPAddress secondaryDNS(8, 8, 4, 4); //optionalDNS

//wind speed parameter
const int m_time = 5; //Measure time in Seconds
int wind_ct = 0;
float windkm = 0.0;
float windms = 0.0;
unsigned long ttime = 0;

void setup()
{
Serial.begin(115200);
// Configures static IP address
if (!WiFi.config(staticIP, gateway, subnet, primaryDNS, secondaryDNS)) {
Serial.println(“STA Failed to configure”);
}

// connect wifi
Connect_to_Wifi();

// time parameter
ttime = millis();

WiFi.mode(WIFI_STA);
ThingSpeak.begin(client); // Initialize ThingSpeak

}

void loop()
{

meassure();

Serial.print(“ความเร็วลม: “);
Serial.print(windkm); //Speed in Km/h
Serial.print(” km/h – “);
Serial.print(windms); //Speed in m/s
Serial.println(” m/s”);

// 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.
// set the fields with the values
ThingSpeak.setField(1, windkm);
ThingSpeak.setField(2, windms);

// set the status
ThingSpeak.setStatus(myStatus);

//int x = ThingSpeak.writeField(myChannelNumber, 1, windkm, myWriteAPIKey);
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
// Check the return code
if(x == 200) {
Serial.println(“ThingSpeak Channel update successful.”);
}
else{
Serial.println(“Problem updating channel. HTTP error code ” + String(x));
}

number++;
if(number > 99){
number = 0;
}

delay(20000); // Wait 20 seconds before sending a new value
delay(20000);

}

void countWind() {
wind_ct ++;
}

void meassure() {
wind_ct = 0;
ttime = millis();
attachInterrupt(16, countWind, RISING);
delay(1000 * m_time);
detachInterrupt(1);
windkm = (float)wind_ct / (float)m_time * 2.4;
windms = windkm/3.6;
}

void Connect_to_Wifi()
{

// We start by connecting to a WiFi network
Serial.println(“Connecting to “);
Serial.println(ssid);
WiFi.begin(ssid, pass);
//Serial.println();
Serial.println();
Serial.print(“Wait for WiFi… “);

while (WiFi.status() != WL_CONNECTED) {
Serial.print(“.”);
delay(500);
}

Serial.println(“”);
Serial.println(“WiFi connected”);
Serial.print(“IP address: “);
Serial.println(WiFi.localIP());
}

source:https://github.com/mathworks/thingspeak-arduino