Hey, I'm new to this side of development.
I'm trying to connect an analog sensor to ESP8266 to send a POST request to my API, so far I have got the API working, the POST request working from ESP8266 to API, but I am struggling on how to get the sensor data from the analog pin on the ATTiny85 to the ESP8266 to include in the POST request.
Here is the code:
ATTiny85
#include <SoftwareSerial.h>
SoftwareSerial ESPSerial(3, 4); // RX: 3, TX:4
void setup() {
pinMode(3, INPUT);
pinMode(4, OUTPUT);
ESPSerial.begin(9600);
}
void loop() {
int analogValue = analogRead(A1);
ESPSerial.println(String(analogValue));
delay(120000);
}
ESP8266
/*******************************************************************
A sample project for making a HTTP/HTTPS GET request on an ESP8266
and parsing it from JSON
https://github.com/sponsors/witnessmenow/
*******************************************************************/
// ----------------------------
// Standard Libraries
// ----------------------------
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <SoftwareSerial.h>
// ----------------------------
// Additional Libraries - each one of these will need to be installed.
// ----------------------------
#include <ArduinoJson.h>
#include <Fetch.h>
const char* ca = \
"-----BEGIN CERTIFICATE-----\n" \
"MIIFVzCCAz+gAwIBAgINAgPlk28xsBNJiGuiFzANBgkqhkiG9w0BAQwFADBHMQsw\n" \
"CQYDVQQGEwJVUzEiMCAGA1UEChMZR29vZ2xlIFRydXN0IFNlcnZpY2VzIExMQzEU\n" \
"MBIGA1UEAxMLR1RTIFJvb3QgUjEwHhcNMTYwNjIyMDAwMDAwWhcNMzYwNjIyMDAw\n" \
"MDAwWjBHMQswCQYDVQQGEwJVUzEiMCAGA1UEChMZR29vZ2xlIFRydXN0IFNlcnZp\n" \
"Y2VzIExMQzEUMBIGA1UEAxMLR1RTIFJvb3QgUjEwggIiMA0GCSqGSIb3DQEBAQUA\n" \
"A4ICDwAwggIKAoICAQC2EQKLHuOhd5s73L+UPreVp0A8of2C+X0yBoJx9vaMf/vo\n" \
"27xqLpeXo4xL+Sv2sfnOhB2x+cWX3u+58qPpvBKJXqeqUqv4IyfLpLGcY9vXmX7w\n" \
"Cl7raKb0xlpHDU0QM+NOsROjyBhsS+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ko8cdLiA6Ef\n" \
"MgfdG+RCzgwARWGAtQsgWSl4vflVy2PFPEz0tv/bal8xa5meLMFrUKTX5hgUvYU/\n" \
"Z6tGn6D/Qqc6f1zLXbBwHSs09dR2CQzreExZBfMzQsNhFRAbd03OIozUhfJFfbdT\n" \
"6u9AWpQKXCBfTkBdYiJ23//OYb2MI3jSNwLgjt7RETeJ9r/tSQdirpLsQBqvFAnZ\n" \
"0E6yove+7u7Y/9waLd64NnHi/Hm3lCXRSHNboTXns5lndcEZOitHTtNCjv0xyBZm\n" \
"2tIMPNuzjsmhDYAPexZ3FL//2wmUspO8IFgV6dtxQ/PeEMMA3KgqlbbC1j+Qa3bb\n" \
"bP6MvPJwNQzcmRk13NfIRmPVNnGuV/u3gm3c\n"\
"-----END CERTIFICATE-----\n";
char ssid[] = "xxxxxxxx"; // your network SSID (name)
char password[] = "xxxxxxxx"; // your network key
SoftwareSerial ATTinySerial(3, 5); // RX: 3, TX:5
long moistureValue = 250;
long maxValue = 450;
long minValue = 120;
WiFiClientSecure client;
// The finger print will change every few months.
#define PLANT_SENSOR_FINGERPRINT "99 E6 41 90 6B BC 1B 48 BA 62 D3 F6 2A C7 9E 8D 32 05 A6 96"
const int analogInPin = 2;
void setup() {
Serial.begin(115200);
ATTinySerial.begin(9600);
connectWifi();
}
void connectWifi() {
// Connect to the WiFI
WiFi.disconnect();
WiFi.mode(WIFI_STA);
delay(100);
// Attempt to connect to Wifi network:
Serial.printf("\nDevice is connecting to WiFi using SSID %s and Passphrase %s.\n", ssid, password);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
}
void setMockSoilValue() {
if (moistureValue < 200) {
moistureValue = random(400, 450);
};
moistureValue = (moistureValue - random(10, 50));
}
void makeHTTPSRequest(String moistureLevel) {
// int sensorValue = analogRead(analogInPin);
// setMockSoilValue();
// delay(1000);
DynamicJsonDocument data(192);
data["id"] = "3";
data["analogValue"] = moistureLevel;
String output = "";
serializeJson(data, output);
Serial.println(output);
RequestOptions options;
options.method = "POST";
options.headers["Content-Type"] = "application/json";
options.headers["Content-Length"] = String(output.length());
options.body = output;
// options.caCert = ca;
Response response = fetch("https://xxxxxxxxxxxxx.run.app/plants/3/data", options);
Serial.println("Request Sent");
}
void loop() {
if(ATTinySerial.available() > 0) {
String moistureLevel = ATTinySerial.readString();
Serial.println(moistureLevel);
makeHTTPSRequest(moistureLevel);
}
Serial.print(".");
delay(30000);
}
I have the ATTiny85 pin 4 connected to pin 3 on the ESP8266
However this is failing, and the only information I get on the Serial Monitor is:
01:32:12.792 -> Device is connecting to WiFi using SSID xxxxxxxx and Passphrase xxxxxxxx.
01:32:12.792 -> ...........
01:32:19.034 -> WiFi connected
01:32:19.034 -> ...........................................
Any help would be much appreciated