Eis os código completo:
Boa noite,
Desculpem o meu "abuso", mas estou desesperado para colocar o ESP01S a enviar dados para o ThinkSpeak.
Eis a programação, que já funcionou, como podem ver através do link:
AEPB - Estação Meteorológica - ThingSpeak IoT] (AEPB - Estação Meteorológica - ThingSpeak IoT
Programação:
#include <WiFiEsp.h>
#include <WiFiEspClient.h>
#include <WiFiEspServer.h>
#include <WiFiEspUdp.h>
#include <Adafruit_BME280.h>
#include <Wire.h>
#include <math.h>
#define LIGHT_SENSOR A0//Grove - Light Sensor is connected to A0 of Arduino
const int thresholdvalue=10; //The treshold for which the LED should turn on. Setting it lower will make it go on at more light, higher for more darkness
float Rsensor; //Resistance of sensor in K
#define SEALEVELPRESSURE_HPA (1013.25)
/* defines - wi-fi /
#define SSID_REDE "Vodafone-34C817" / coloque aqui o nome da rede que se deseja conectar /
#define SENHA_REDE "BY5xnMJDM5" / coloque aqui a senha da rede que se deseja conectar /
#define INTERVALO_ENVIO_THINGSPEAK 30000 / intervalo entre envios de dados ao ThingSpeak (em ms) */
Adafruit_BME280 bme;
/* constantes e variáveis globais /
char endereco_api_thingspeak[] = "api.thingspeak.com";
String chave_escrita_thingspeak = "L9F8BLYVWJTSM9JH"; / Coloque aqui sua chave de escrita do seu canal */
unsigned long last_connection_time;
WiFiEspClient client;
//DHT dht(DHTPIN, DHTTYPE);
/* prototypes */
void envia_informacoes_thingspeak(String string_dados);
void init_wifi(void);
void conecta_wifi(void);
void verifica_conexao_wifi(void);
/*
- Implementações
*/
/* Função: envia informações ao ThingSpeak
- Parâmetros: String com a informação a ser enviada
- Retorno: nenhum
/
void envia_informacoes_thingspeak(String string_dados)
{
if (client.connect(endereco_api_thingspeak, 80))
{
/ faz a requisição HTTP ao ThingSpeak */
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+chave_escrita_thingspeak+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(string_dados.length());
client.print("\n\n");
client.print(string_dados);
last_connection_time = millis();
Serial.println("- Informações enviadas ao ThingSpeak!");
}
}
/* Função: inicializa wi-fi
- Parametros: nenhum
- Retorno: nenhum
*/
void init_wifi(void)
{
Serial.println("------WI-FI -----");
Serial.println("Conectando-se a rede: ");
Serial.println(SSID_REDE);
Serial.println("\nAguarde...");conecta_wifi();
}
/* Função: conecta-se a rede wi-fi
- Parametros: nenhum
- Retorno: nenhum
/
void conecta_wifi(void)
{
/ Se ja estiver conectado, nada é feito. /
if (WiFi.status() == WL_CONNECTED)
{
return;
}/ refaz a conexão */
WiFi.begin(SSID_REDE, SENHA_REDE);while (WiFi.status() != WL_CONNECTED)
{
delay(100);
}Serial.println("Conectado com sucesso a rede wi-fi \n");
Serial.println(SSID_REDE);
}
/* Função: verifica se a conexao wi-fi está ativa
- (e, em caso negativo, refaz a conexao)
- Parametros: nenhum
- Retorno: nenhum
*/
void verifica_conexao_wifi(void)
{
conecta_wifi();
}
void setup() {
{
Serial.begin(115200);
last_connection_time = 0;
/* Inicializa e conecta-se ao wi-fi */
init_wifi();
if (!bme.begin(0x76)) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
Serial.begin(115200); //Start the Serial connection
}
}
void loop(){
int sensorValue = analogRead(LIGHT_SENSOR);
Rsensor = (float)(1023-sensorValue)*10/sensorValue;
Serial.print("Luminância cd/m2 = ");
Serial.println(sensorValue);
Serial.print("Intensidade luminosa cd(candela) = ");
Serial.println(Rsensor,DEC);//show the ligth intensity on the serial monitor;
Serial.print("Temperatura = ");
Serial.print(bme.readTemperature());
Serial.println("ºC");
Serial.print("Pressão = ");
Serial.print(bme.readPressure() / 100.0F);
Serial.println(" hPa");
Serial.print("Altitude aprox. = ");
Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
Serial.println("m");
Serial.print("Humidade = ");
Serial.print(bme.readHumidity());
Serial.println("%");
int pin = 8;
unsigned long duration;
unsigned long starttime;
unsigned long sampletime_ms = 30;
unsigned long lowpulseoccupancy = 0;
float ratio = 0;
float concentration = 0;
duration = pulseIn(pin, LOW);
lowpulseoccupancy = lowpulseoccupancy+duration;
if ((millis()-starttime) >= sampletime_ms) //if the sampel time = = 30s
ratio = lowpulseoccupancy/(sampletime_ms10.0);
concentration = 1.1pow(ratio,3)-3.8pow(ratio,2)+520ratio+0.62;
Serial.print("Fumos, Poeiras/Partículas = ");
Serial.print(concentration);
Serial.println(" pcs/0.01cf");
Serial.println("\n");
lowpulseoccupancy = 0;
starttime = millis();
Serial.println();
delay(10000);
}
ERROS:
C:\Users\ruigo\Downloads\ConnectWifi_Sensor\sensor_multiplo_FUNCIONA\sensor_multiplo_FUNCIONA.ino: In function 'void setup()':
C:\Users\ruigo\Downloads\ConnectWifi_Sensor\sensor_multiplo_FUNCIONA\sensor_multiplo_FUNCIONA.ino:127:12: error: a function-definition is not allowed here before '{' token
void loop(){
^
exit status 1
Compilation error: a function-definition is not allowed here before '{' token
Poderá ajudar-me, por favor?
O ESP dá sempre erro de ligação (muitos e diversos erros de ligação)
Obrigado.
Rui