#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
//needed for library
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
//WiFiManager
//Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wifiManager;
//reset saved settings
//wifiManager.resetSettings();
//set custom ip for portal
//wifiManager.setAPStaticIPConfig(IPAddress(10,0,1,1), IPAddress(10,0,1,1), IPAddress(255,255,255,0));
//fetches ssid and pass from eeprom and tries to connect
//if it does not connect it starts an access point with the specified name
//here "AutoConnectAP"
//and goes into a blocking loop awaiting configuration
wifiManager.autoConnect("AutoConnectAP");
//or use this for auto generated name ESP + ChipID
//wifiManager.autoConnect();
pinMode(2,INPUT);
char apiKey[20] = "";
WiFiClient client;
char defaultHost[100] = "184.106.153.149"; //Thing Speak IP address (sometime the web address causes issues with ESP's :/
long itt = 500;
//if you get here you have connected to the WiFi
Serial.println("connected...yeey :)");
}
void loop() {
delay(5000);
pinMode(2,INPUT);
const int waterInPin = 2; // Analog input pin that the potentiometer is attached to
int waterSensorInValue;//reading our water lever sensor
int waterSensorOutValue;//conversion of water sensor value
// put your main code here, to run repeatedly:
waterSensorInValue = analogRead(waterInPin);
waterSensorOutValue = map(waterSensorInValue,0,1024,0,225);
Serial.print("WaterOutValue = ");
Serial.print(waterSensorOutValue );
Serial.print("WaterInValue = ");
Serial.print(waterSensorInValue );
delay(18000);
WiFiManager wifiManager;
char apiKey[20] = "";
WiFiClient client;
char defaultHost[100] = "184.106.153.149"; //Thing Speak IP address (sometime the web address causes issues with ESP's :/
long itt = waterSensorOutValue;
//if you get here you have connected to the WiFi
WiFiManagerParameter customHostServer("defaultHost", "Host Server", defaultHost, 100);
WiFiManagerParameter customAPIKey("apiKey", "Host API Key", apiKey, 20);
wifiManager.addParameter(&customHostServer);
wifiManager.addParameter(&customAPIKey);
if (client.connect(defaultHost,80))
{ // "184.106.153.149" or api.thingspeak.com
itt++; //Replace with a sensor reading or something useful
String postStr = apiKey;
postStr +="&field1=";
postStr += String(itt);
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: "+String("XXXXXXXXXXXXXXXXXX")+"\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);
Serial.println("% send to Thingspeak");
}
client.stop();
Serial.println("Waiting…");
}
Does anyone have any suggestions?