ESP8266



Escribir en ThingSpeak


Ejemplo básico de escritura

Este es un ejemplo de la Biblioteca ThingSpeak

Escribe en un campo un número que incrementa cada 20 segundos

#include "ThingSpeak.h"
  #include <ESP8266WiFi.h>
  
  //----------------  Fill in your credentails   ---------------------
  char ssid[] = "MySSID";             // your network SSID (name) 
  char pass[] = "MyPassword";         // your network password
  unsigned long myChannelNumber = 0;  // Replace the 0 with your channel number
  const char * myWriteAPIKey = "";    // Paste your ThingSpeak Write API Key between the quotes 
  //------------------------------------------------------------------
  
  WiFiClient  client;
  
  int number = 0;
  
  void setup() {
    //Initialize serial and wait for port to open:
    Serial.begin(9600);
    while (!Serial) {
      ; // wait for serial port to connect. Needed for native USB port only
    }
  
    WiFi.mode(WIFI_STA);
    ThingSpeak.begin(client); 
  }
  
  void loop() {
  
    // Connect or reconnect to WiFi
    if(WiFi.status() != WL_CONNECTED){
      Serial.print("Attempting to connect to SSID: ");
      Serial.println(SECRET_SSID);
      while(WiFi.status() != WL_CONNECTED){
        WiFi.begin(ssid, pass);
        Serial.print(".");
        delay(5000);     
      } 
      Serial.println("\nConnected.");
    }
    
    // Write to ThingSpeak. There are up to 8 fields in a channel, allowing you to store up to 8 different
    // pieces of information in a channel.  Here, we write to field 1.
    int x = ThingSpeak.writeField(myChannelNumber, 1, number, myWriteAPIKey);
    
    // Check the return code
    if(x == 200){
      Serial.println("Channel update successful.");
    }
    else{
      Serial.println("Problem updating channel. HTTP error code " + String(x));
    }
  
    number++;
    if(number > 99){
      number = 0;
    }
    
    delay(20000); // Wait 20 seconds before sending a new value
  }