V2: Remap many stuff

Took 3 hours 0 minutes
This commit is contained in:
2023-01-23 23:56:39 +01:00
parent 58a3df8111
commit c9a0ac68c4
32 changed files with 559 additions and 653 deletions

View File

@ -2,6 +2,7 @@
* Official proxy code for iTender GPIO Communication
**/
#include <ArduinoJson.h>
#include "HX711.h"
// Define the size of the JSON buffer
#define JSON_BUFFER_SIZE 256
@ -10,17 +11,16 @@
StaticJsonDocument<JSON_BUFFER_SIZE> incomingJson;
// Create a JSON object for outgoing messages
DynamicJsonDocument<JSON_BUFFER_SIZE> outgoingJson;
DynamicJsonDocument outgoingJson(JSON_BUFFER_SIZE);
void setup() {
// Initialize serial communication
Serial.begin(9600);
}
void(* resetFunc) (void) = 0; //declare reset function @ address 0
void (*resetFunc)(void) = 0; //declare reset function @ address 0
void loop() {
// Wait for a new line on the serial console
if (Serial.available()) {
// Read the incoming JSON message
DeserializationError error = deserializeJson(incomingJson, Serial);
@ -29,43 +29,74 @@ void loop() {
} else {
// Extract the "type" and "data" fields from the JSON object
String id = incomingJson["id"];
String type = incomingJson["type"];
int type = incomingJson["type"];
JsonVariant data = incomingJson["data"];
// Create a nested object in the root object
JsonObject outgoingData = outgoingJson.to<JsonObject>().createNestedObject("data");
outgoingData["success"] = true;
outgoingData["success"] = true;
// Handle the message based on the "type" field
switch (type) {
case "ACK":
// Handle ACK message
break;
case "SET_PIN":
case 1:
// Handle SET_PIN message
pinMode((int)data["pin"], OUTPUT);
if (data["mode"] == "DIGITAL") {
digitalWrite((int)data["pin"], data["value"]);
} else {
analogWrite((int)data["pin"], (data["value"] == 255) ? HIGH : LOW);
}
break;
case "GET_VAL":
case 2:
// Handle GET_VAL message
pinMode((int)data["pin"], INPUT);
int val;
if (data["mode"] == "DIGITAL") {
val = digitalRead((int)data["pin"]);
} else {
val = analogRead((int)data["pin"]);
}
break;
case "GET_SENSOR":
case 3:
// Handle GET_SENSOR message
/*
(WEIGHT_VAL - NO_WEIGHT_VAL) / Sensitivitätsfaktor = Gewicht in Gramm
(WEIGHT_VAL - NO_WEIGHT_VAL) / (100g_val /100) ist die Formel zur Berechnung des Gewichts in Gramm nach der Kalibrierung mit einem 100 Gramm Gewicht.
100g_val /100 gibt den Sensitivitätsfaktor an, der angibt, wie viel sich der Sensorwert ändert, wenn sich das Gewicht um ein Gramm ändert.
Durch die Division von 100g_val durch 100 wird der Sensitivitätsfaktor berechnet, und durch die Division von (WEIGHT_VAL - NO_WEIGHT_VAL) durch den Sensitivitätsfaktor wird das Gewicht in Gramm berechnet.
Beispiel:
(WEIGHT_VAL - NO_WEIGHT_VAL) / (100g_val /100) = Gewicht in Gramm
(2400 - 2000) / (2450 /100) = 80 Gramm
*/
// HX711 circuit wiring
const int LOADCELL_DATA_PIN = (int)data["pin_data"];
const int LOADCELL_CLOCK_PIN = (int)data["pin_clock"];
HX711 scale;
scale.begin(LOADCELL_DATA_PIN, LOADCELL_CLOCK_PIN);
// Get the weight value from the scale
long weight_val = scale.get_units();
outgoingData["value"] = weight_val;
break;
case "RESTART":
case 4:
resetFunc(); //call reset
break;
default:
// Handle unknown message type
outgoingData[""] = 0;
break;
}
// Prepare the outgoing JSON message
outgoingJson["id"] = id;
outgoingJson["type"] = type;
outgoingJson["data"] = "";
outgoingJson["type"] = 0;
outgoingJson["data"] = outgoingData;
// Send the outgoing JSON message
serializeJson(outgoingJson, Serial);
}
}
}
}

View File

@ -1,4 +1,3 @@
/**
#include "HX711.h"
@ -7,37 +6,45 @@ const int LOADCELL_SCK_PIN = 3;
HX711 scale;
int minus = 0;
long scale_factor = 0;
long no_weight = 0;
void setup() {
void tare() {
// put your setup code here, to run once:
Serial.begin(9600);
delay(1000);
Serial.println("TARE");
Serial.println("Alle Gewichte entfernen...");
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
scale.set_scale(429.6159259259259);
scale.tare();
delay(4000);
Serial.println("[Measureing]");
Serial.print("Null-Gewicht: ");
no_weight = scale.get_units(3);
Serial.println(no_weight);
delay(1000);
minus=abs(scale.get_units(5));
// scale.set_scale();
// scale.tare();
// Serial.println("TARE OK - PLACE WEIGHT");
//delay(5000);
Serial.println("100g Gewicht platzieren...");
delay(4000);
Serial.print("100g-Gewicht: ");
scale_factor = scale.get_units(3);
Serial.println(scale_factor);
scale_factor = scale_factor / 100;
Serial.print("Skalierungsfaktor: ");
Serial.println(scale_factor);
delay(2000);
}
int len = 0;
long sum = 0;
void loop() {
if (scale.is_ready()) {
long data = scale.get_units(5) + minus;
Serial.println(data);
}
void tare_loop() {
delay(2000);
long val = scale.get_units(1);
// (WEIGHT_VAL - NO_WEIGHT_VAL) / (100g_val /100) = Gewicht in Gramm
Serial.println( ( val - no_weight ) / scale_factor );
}
**/