Hi! Recently I am doing a Arduino project and I have finished some sensor part like water meter and I want to send data to Thingspeak so I choose esp8266ex as wifi module and I try some code however It doesn't work
#include<SoftwareSerial.h>
SoftwareSerial espSerial = SoftwareSerial(2,3);
String apiKey = "FLD165PBJ09Y7Q55";
String ssid="iPhone"; // Wifi network SSID
String password ="12345678";
boolean DEBUG=true;
byte statusLed = 13;
byte sensorInterrupt = 0;
int sensor_pin=A0; // variable for sensor
float sample=0;
float calibrationFactor = 4.5;
volatile byte pulseCount;
float flowRate;
unsigned int flowMilliLitres;
unsigned long totalMilliLitres;
unsigned long oldTime
void showResponse(int waitTime){
long t=millis();
char c;
while (t+waitTime>millis()){
if (espSerial.available()){
c=espSerial.read();
if (DEBUG) Serial.print(c);
}
}
}
//========================================================================
boolean thingSpeakWrite(float value1){
String cmd = "AT+CIPSTART="TCP",""; // TCP connection
cmd += "184.106.153.149"; // api.thingspeak.com
cmd += "",80";
espSerial.println(cmd);
if (DEBUG) Serial.println(cmd);
if(espSerial.find("Error")){
if (DEBUG) Serial.println("AT+CIPSTART error");
return false;
}
String getStr = "GET /update?api_key="; // prepare GET string
getStr += apiKey;
getStr +="&field1=";
getStr += String(value1);
// getStr +="&field3=";
// getStr += String(value3);
// ...
getStr += "\r\n\r\n";
// send data length
cmd = "AT+CIPSEND=";
cmd += String(getStr.length());
espSerial.println(cmd);
if (DEBUG) Serial.println(cmd);
delay(100);
if(espSerial.find(">")){
espSerial.print(getStr);
if (DEBUG) Serial.print(getStr);
}
else{
espSerial.println("AT+CIPCLOSE");
// alert user
if (DEBUG) Serial.println("AT+CIPCLOSE");
return false;
}
return true;
}
void setup(){
DEBUG=true;
Serial.begin(9600);
espSerial.begin(115200);
espSerial.println("AT+CWMODE=1"); // set esp8266 as client
showResponse(1000);
espSerial.println("AT+CWJAP=""+iPhone+"",""+12345678+"""); // set your home router SSID and password
showResponse(5000);
pinMode(statusLed, OUTPUT);
digitalWrite(statusLed, HIGH); // Optional Internal Pull-Up
// enable debug serial
// enable software serial
pulseCount = 0.0;
flowRate = 0.0;
flowMilliLitres = 0.0;
totalMilliLitres = 0.0;
oldTime = 0;
attachInterrupt(sensorInterrupt, pulseCount, FALLING);
if (DEBUG)
Serial.println("Setup completed");
}
void loop(){
if((millis() - oldTime) > 100){
flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor/100;
oldTime = millis();
float t = flowRate;
thingSpeakWrite(t);
// Write values to thingspeak
}
// blink LED on board
digitalWrite(statusLed, HIGH);
delay(200);
digitalWrite(statusLed, LOW);
pulseCount = 0;
attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
}
void pulseCounter()
{
pulseCount++;
}
please check it thanks a million