Close

interface with ThingSpeak platform

A project log for Rezodo: Long Range irrigation and weather station

Rezodo aims at building a distributed irrigation system and a weather station with farmers. It's also a fully open platform for IoT learning

jp-gleyzesJP Gleyzes 04/18/2023 at 15:500 Comments

Interfacing Rezodo with ThingSpeak is very simple. You can even look at tutorial devoted to smart agriculture applications

In a few words, you need first to create an account then a channel and get an API key.

Once you have your key, simply enter it into the arduino code. Only two lines!

// ThingSpeak settings
char thingserver[] = "api.thingspeak.com";
String writeAPIKey = "CNP89KTF45WWS4YF";

Then this procedure will upload two fields to your channel:

void ThingSpeakPost(void)
{
  WiFiClient client;
  if (!client.connect(thingserver, 80))
  {
    Serial.println("Connection failed");
    client.stop();
    return;
  }
  else
  {
    // Create data string to send to ThingSpeak.
    String data =  "field1=" + String(sensorValues[1][0][0]) + "&field2=" + String(sensorValues[2][0][0])  ; //shows how to include additional field data in http post
#ifdef W_DEBUG
    // Just for testing, print out the message on serial port
    Serial.println("==> ThingSpeak POST message ");
    Serial.println(data);
#endif
    // POST data to ThingSpeak.
    if (client.connect(thingserver, 80)) {
      client.println("POST /update HTTP/1.1");
      client.println("Host: api.thingspeak.com");
      client.println("Connection: close");
      client.println("User-Agent: ESP32WiFi/1.1");
      client.println("X-THINGSPEAKAPIKEY: " + writeAPIKey);
      client.println("Content-Type: application/x-www-form-urlencoded");
      client.print("Content-Length: ");
      client.print(data.length());
      client.print("\n\n");
      client.print(data);
      delay(300);
    }
#ifdef DEBUG_THINGSPEAK
    // Just for testing, print out the message on serial port
    Serial.println("==> ThingSpeak POST message ");
    Serial.println(data);
#endif
    client.stop();
  }
}

As you can see values are passed as strings and uploaded inside a html POST. Nothing complex!

This being done you have access to the full power of this platform.

Channels can even be public or private. Have a look to the sensor values here (only one moisture probe  "field2" is connected while dummy values are sent to field1 !)

Discussions