The first thing i am gonna say is: Sorry for mi english.
I am getting some issues with this board.
I need to send the value of a variable that registers the weight to ThingsSpeak
The code I use on the arduino Uno is this:
#include <ArduinoJson.h>
#include <HX711.h>
#define CALIBRACION 32780.0
byte pinData = 3;
byte pinClk = 2;
HX711 bascula;
void setup() {
Serial.begin(9600);
bascula.begin(pinData, pinClk);
bascula.set_scale(CALIBRACION);
bascula.tare();
}
StaticJsonDocument<1000> doc;
void loop() {
float p = bascula.get_units();
doc["peso"] = abs(p);
if(Serial.available()>=0)
{
serializeJson(doc, Serial);
}
delay(30000); // 30 detik
}
And the code i used in the ESP:
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#ifndef STASSID
#define STASSID "CasaCS2g" //access point wifi name (SSID)
#define STAPSK "-------" //access point wifi password
#endif
const char* ssid = STASSID;
const char* password = STAPSK;
const char* host = "api.thingspeak.com";
const uint16_t port = 443;
// SHA1 fingerprint of the certificate
const char fingerprint[] PROGMEM = "-------------B:F7:35:FE:15:EA:82:9F:5F:55:54:3E";
//WiFiClientSecure client;
int data1;
void ESPSendData(int peso)
{
char buf[100];
sprintf(buf, "GET /update?api_key=---------------&field1=%lu&field2=%lu HTTP/1.1\r\nHost: api.thingspeak.com\r\n\r\n", peso);
//WiFiClient client;
WiFiClientSecure client;
Serial.printf("Using fingerprint '%s'\n", fingerprint);
client.setFingerprint(fingerprint);
if (!client.connect("api.thingspeak.com", 443)) {
Serial.println("Failed to connect with 'api.thingspeak.com' !");
}
else {
int timeout = millis() + 5000;
client.print(buf);
while (client.available() == 0) {
if (timeout - millis() < 0) {
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
}
int size;
while ((size = client.available()) > 0) {
uint8_t* msg = (uint8_t*)malloc(size);
size = client.read(msg, size);
//Serial.write(msg, size);
free(msg);
}
}
}
void setup() {
Serial.begin(9600);
while (!Serial) continue;
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
/* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default,
would try to act as both a client and an access-point and could cause
network-issues with your other WiFi-devices on your WiFi-network. */
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
//Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
StaticJsonDocument<1000> doc;
DeserializationError error = deserializeJson(doc, Serial);
if (error) return;
data1=doc["peso"];
if (data1>0) {
ESPSendData(data1);
}
delay(60000); // 60 detik
}