Valor de campo no disponible
3 views (last 30 days)
Show older comments
#include <ThingSpeak.h>
//#include "WiFi.h"
#include <ESP8266WiFi.h>
#include <Wire.h>
#include <Adafruit_ADS1X15.h>
#include <LiquidCrystal_I2C.h>
#define SDA_PIN D2
#define SCL_PIN D1
Adafruit_ADS1115 ads;
LiquidCrystal_I2C lcd(0x27, 16, 2);
const float NIVEL_INFERIOR = 10000.0;
const float NIVEL_SUPERIOR = 49900.0;
int litros;
int centimetros;
unsigned long tiempoadc = 0;
const char* ssid = "RED .4G";
const char* password = "xxxxxxxxxx";
const char* server = "api.thingspeak.com";
unsigned long channelID = 330507;
const char* WriteAPIKey = "xxxxxxxxxxxxxxxx";
WiFiClient cliente;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Conectando a la red WiFi...");
}
Serial.println("Conectado a la red WiFi");
// Inicialización del display LCD
Wire.begin(SDA_PIN, SCL_PIN);
ads.begin(0x48);
lcd.begin(16, 2);
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
Serial.begin(9600);
ads.setGain(GAIN_TWOTHIRDS);
ads.begin();
lcd.setCursor(0, 0);
lcd.print(" INTERPRISE MX ");
lcd.setCursor(0, 1);
lcd.print(" AGUA CRUDA ");
delay(5000);
lcd.clear();
pinMode(D6, OUTPUT);
pinMode(D5, OUTPUT);
digitalWrite(D6, HIGH);
digitalWrite(D5, HIGH);
tiempoadc = millis();
ThingSpeak.begin(cliente);
ThingSpeak.writeFields(channelID, WriteAPIKey);
}
void loop() {
if (millis() - tiempoadc > 500UL) {
tiempoadc = millis();
short adc0 = ads.readADC_SingleEnded(0);
float centimetros = (adc0 / 53.40);
float litros = (centimetros * 212.1);
if (litros < NIVEL_INFERIOR) {
digitalWrite(D5, LOW);
} else {
digitalWrite(D5, HIGH);
}
if (litros > NIVEL_SUPERIOR) {
digitalWrite(D6, LOW);
} else {
digitalWrite(D6, HIGH);
}
Serial.print("litros ");
Serial.println(litros);
Serial.print("ctm: ");
Serial.println(centimetros);
Serial.print("A0: ");
Serial.println(adc0);
lcd.setCursor(0, 0);
lcd.print("lts ");
lcd.print(litros);
lcd.setCursor(0, 1);
lcd.print("cm ");
lcd.print(centimetros);
ThingSpeak.setField (1, litros);
ThingSpeak.setField (2, centimetros);
ThingSpeak.setField (channelID, WriteAPIKey );
Serial.println("datos enviados a ThingSpeak");
delay(60000);
}
}
4 Comments
Christopher Stapels
on 3 Nov 2023
I suggest looking a the example code to help you with that error. The example uses char variable[] instead of const char *
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password
Answers (2)
Christopher Stapels
on 3 Nov 2023
Edited: Christopher Stapels
on 3 Nov 2023
Here are a few things I notice about your code.
You run this line in the setup function, im not sure why.
ThingSpeak.setField (channelID, WriteAPIKey );
You also have this line in your loop, that seems to execute the function every 500 miliseconds. That is way too frequent to write to ThingSpeak, and might result in your accoubnt being blocked.
if (millis() - tiempoadc > 500UL) {
Fortunately, your code doesnt yet send data to ThingSpeak in the loop. Make sure you increase the delay to at least 15000UL if you have a free account ot 1000 if you have a paid account.
This line at the end of the main loop is incorrect for sure (the comment says "data sent to ThingSpeak", but the setField command you have does not send data.)
ThingSpeak.setField (channelID, WriteAPIKey );
Here is where you want to use the writeFields command
have a look at this example code from the library. The example I linked is for esp32, but there is also very similar code for esp8266 in the repo.
If you fix those things, it should start to work soon, please let us know!
4 Comments
Felipe
on 5 Nov 2023
2 Comments
Christopher Stapels
on 6 Nov 2023
Yes, sometimes people like to keep the passwords in a seperate file. That way when you post your code, you arent giving them away. Do you have a question about this?
Communities
More Answers in the ThingSpeak Community
See Also
Categories
Find more on Write Data to Channel in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!