Esempi di Utilizzo

...

Arduino ESP32 - Scrittura Variabile


Arduino ESP32 - Lettura Variabile




Esempio Codice
Arduino ESP32
Scrittura Variabile A00
    
 /*
    WriteSingleVariable
 */

 #include < WiFi.h >
 #include < HTTPClient.h >

 const char* ssid = "WiFi ID";
 const char* password = "WiFi Password";

 String serverName = "https://www.synteg.it/api/v1/engine/WriteSingleVariable";

 unsigned long lastTime = 0;
 unsigned long timerDelay = 5000;

 void setup() {
     Serial.begin(115200);

     WiFi.begin(ssid, password);
     Serial.println("Connecting");
     while (WiFi.status() != WL_CONNECTED) {
     delay(500);
     Serial.print(".");
     }
     Serial.println("");
     Serial.print("Connected to WiFi network with IP Address: ");
     Serial.println(WiFi.localIP());
 }

 void loop() {
     //Send an HTTP POST request every 10 minutes
     if ((millis() - lastTime) > timerDelay) {
     //Check WiFi connection status
     if (WiFi.status() == WL_CONNECTED) {
         HTTPClient http;

         String serverPath = serverName;

         http.begin(serverPath.c_str());

         // JSON da inviare
         String jsonPayload = R"({

        {
        "API_KEY": "00000000-0000-00000000-0000-00000000",
        "VariableName": "A01",
        "Value": "hello"
}
         )";

         http.addHeader("Content-Type", "application/json");

         // Send HTTP GET request
         int httpResponseCode = http.POST(jsonPayload);

         if (httpResponseCode > 0) {
         Serial.print("HTTP Response code: ");
         Serial.println(httpResponseCode);
         String payload = http.getString();
         Serial.println(payload);
         }
         else {
         Serial.print("Error code: ");
         Serial.println(httpResponseCode);
         }
         // Free resources
         http.end();
     }
     else {
         Serial.println("WiFi Disconnected");
     }
     lastTime = millis();
     }
 }
    
        
Esempio Codice
Arduino ESP32
Lettura Variabile A00
    
    /*
    ReadSingleVariable
    */

    #include < WiFi.h >
    #include < HTTPClient.h >

    const char* ssid = "WiFi ID";
    const char* password = "WiFi Password";

    String serverName = "https://www.synteg.it/api/v1/engine/ReadSingleVariable";

    unsigned long lastTime = 0;
    unsigned long timerDelay = 5000;

    void setup() {
        Serial.begin(115200);

        WiFi.begin(ssid, password);
        Serial.println("Connecting");
        while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
        }
        Serial.println("");
        Serial.print("Connected to WiFi network with IP Address: ");
        Serial.println(WiFi.localIP());
    }

    void loop() {
        //Send an HTTP POST request every 10 minutes
        if ((millis() - lastTime) > timerDelay) {
        //Check WiFi connection status
        if (WiFi.status() == WL_CONNECTED) {
            HTTPClient http;

            String serverPath = serverName;

            http.begin(serverPath.c_str());

            // JSON da inviare
            String jsonPayload = R"({
            "API_KEY": "00000000-0000-00000000-0000-00000000",
            "VariableName": "A00"
        })";

            http.addHeader("Content-Type", "application/json");

            // Send HTTP GET request
            int httpResponseCode = http.POST(jsonPayload);

            if (httpResponseCode > 0) {
            Serial.print("HTTP Response code: ");
            Serial.println(httpResponseCode);
            String payload = http.getString();
            Serial.println(payload);
            }
            else {
            Serial.print("Error code: ");
            Serial.println(httpResponseCode);
            }
            // Free resources
            http.end();
        }
        else {
            Serial.println("WiFi Disconnected");
        }
        lastTime = millis();
        }
    }