Boas, eu estou a usar um wemos ligado ao moedeiro ch923 .
eu quero que envie os pulsos da moeda para o thingspeak sempre que é inserido uma moeda.
não estou a conseguir enviar para o thingspeak os pulsos que da a moeda quando é inserida
Codigo:
#include <ESP8266WiFi.h>
#define tempo 15 // envia para o Thingspeak passado 15 segundos
#define COIN_PIN D2
volatile int pulses = 0;
volatile long timeLastPulse = 0;
const char* server = “api.thingspeak.com”;
String apiKey = “88ZI7CYAIMP4XCI9”;
const char* nome_rede = “MEO-CASA”;
const char* password_rede = “123456789”;
void setup() {
Serial.begin(115200);
FazConexaoWiFi();
Serial.println(“Ready”);
pinMode(COIN_PIN, INPUT_PULLUP);
attachInterrupt(COIN_PIN, coinISR, FALLING); // COIN wire connected to D2;
}
// executed for every pulse;
void coinISR()
{
pulses++;
timeLastPulse = millis();
}
void loop ()
{ long timeFromLastPulse = millis() - timeLastPulse;
if (pulses >0 && timeFromLastPulse > 595)
{
// sequence of pulses stopped; determine the coin type;
if (pulses == 1)
{
Serial.println(“1 pulses”);
}
else if (pulses == 2)
{
Serial.println(“2 pulses”);
}
else if (pulses == 4)
{
Serial.println(“4 pulses”);
}
else
{
Serial.print(“Unknown coin: “);
Serial.print(pulses);
Serial.println(” pulses”);
}
}
pulses=2;
Serial.println(pulses);
enviopulso(pulses);
int contador = tempo;
while(contador–)
delay(200);
}
void FazConexaoWiFi()
{Serial.println(“Conectando-se à rede WiFi…”);
WiFi.begin(nome_rede, password_rede);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("");
Serial.println(“Connectado”);
Serial.println(“IP OBTIDO”);
Serial.println(WiFi.localIP());
}//end
void enviopulso(int pulses)
{
WiFiClient client;
if (client.connect(server, 80)) { // use ip 184.106.153.149 or api.thingspeak.com
Serial.println(“Informação enviada para o Thingspeak”);
String postStr = apiKey;
postStr += “&field1=”;
postStr += String(pulses);
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);
delay(1000);
}
}