mathworks / thingspeak-arduino

ThingSpeak Communication Library for Arduino, ESP8266 and ESP32
https://thingspeak.com
431 stars 231 forks source link

esp8266 #42

Closed amatera409 closed 5 years ago

amatera409 commented 5 years ago

it works on my adafruit esp8266.. thanks guys..

code:

/* WriteVoltage

Reads an analog voltage from pin 0, and writes it to a channel on ThingSpeak every 20 seconds.

ThingSpeak ( https://www.thingspeak.com ) is an analytic IoT platform service that allows you to aggregate, visualize and analyze live data streams in the cloud.

Copyright 2017, The MathWorks, Inc.

Documentation for the ThingSpeak Communication Library for Arduino is in the extras/documentation folder where the library was installed. See the accompaning licence file for licensing information. */

include "ThingSpeak.h"

// // This example selects the correct library to use based on the board selected under the Tools menu in the IDE. // Yun, Ethernet shield, WiFi101 shield, esp8266 and MKR1000 are all supported.
// EPS32 is only partially compatible. ADC analogRead() for ESP32 has not yet been implemented in the SparkFun library. It will always return 0.
// With Yun, the default is that you're using the Ethernet connection. // If you're using a wi-fi 101 or ethernet shield (http://www.arduino.cc/en/Main/ArduinoWiFiShield), uncomment the corresponding line below //

//#define USE_WIFI101_SHIELD //#define USE_ETHERNET_SHIELD

// #define (ARDUINO_ARCH_ESP8266)

include

char ssid[] = "ssid";    //  your network SSID (name) --subject to change to your own network and password
char pass[] = "pwd";   // your network password
int status = WL_IDLE_STATUS;
WiFiClient  client;

// On ESP8266: 0 - 1023 maps to 0 - 1 volts

define VOLTAGE_MAX 1.0

define VOLTAGE_MAXCOUNTS 1023.0

/*


Visit https://www.thingspeak.com to sign up for a free account and create a channel. The video tutorial http://community.thingspeak.com/tutorials/thingspeak-channels/ has more information. You need to change this to your channel, and your write API key IF YOU SHARE YOUR CODE WITH OTHERS, MAKE SURE YOU REMOVE YOUR WRITE API KEY!! *****/ unsigned long myChannelNumber = 5752; // your thingspeak channel no. const char * myWriteAPIKey = "AWJZGFEIB4DT1"; //your thingspeak writeapikey.

void setup() { WiFi.begin(ssid, pass);

  ThingSpeak.begin(client);

}

void loop() { // read the input on analog pin 0: int sensorValue = analogRead(A0); // Convert the analog reading // On Uno,Mega,YunArduino: 0 - 1023 maps to 0 - 5 volts // On ESP8266: 0 - 1023 maps to 0 - 1 volts // On ESP32: 0 - 4095 maps to 0 - 1 volts, but will always return 0 as analogRead() has not been implemented yet // On MKR1000,Due: 0 - 4095 maps to 0 - 3.3 volts float voltage = sensorValue * (VOLTAGE_MAX / VOLTAGE_MAXCOUNTS);

// 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. ThingSpeak.writeField(myChannelNumber, 1, voltage, myWriteAPIKey); delay(20000); // ThingSpeak will only accept updates every 15 seconds. }

jasontwinters commented 5 years ago

You're welcome!