Thinkspeak Post

by mtamim

https://github.com/mathworks/thingspeak-arduino

WRITE===========================================================================
#include “ThingSpeak.h”

#include <ESP8266WiFi.h>
char ssid[] = “tamim”;    //  your network SSID (name)
char pass[] = “66666666”;   // your network password
int status = WL_IDLE_STATUS;
WiFiClient  client;

#define VOLTAGE_MAX 1
#define VOLTAGE_MAXCOUNTS 1023.0

unsigned long myChannelNumber = 224553;
const char * myWriteAPIKey = “KOZX23HGEK7HFW3S”;

void setup() {
Serial.begin(115200);
Serial.println();
Serial.println(“Connecting “);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(“.”);
}
Serial.println();
ThingSpeak.begin(client);
}

void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
float voltage = sensorValue * (VOLTAGE_MAX / VOLTAGE_MAXCOUNTS);
voltage=random(100);
// 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, 2, voltage, myWriteAPIKey);
Serial.println(voltage);
delay(10000); // ThingSpeak will only accept updates every 15 seconds.
}

READ================================================================================#include “ThingSpeak.h”
#include <ESP8266WiFi.h>

char ssid[] = “<YOURNETWORK>”;    //  your network SSID (name)
char pass[] = “<YOURPASSWORD>”;   // your network password
int status = WL_IDLE_STATUS;
WiFiClient  client;

/*
This is the ThingSpeak channel number for the MathwWorks weather station
https://thingspeak.com/channels/12397.  It senses a number of things, including
Wind Direction, Wind Speed, Humidity, Temperature, Rainfall, and Atmospheric Pressure.

Temperature is stored in field 4
*/

unsigned long weatherStationChannelNumber = 12397;
unsigned int temperatureFieldNumber = 4;

void setup() {

WiFi.begin(ssid, pass);
ThingSpeak.begin(client);

}

void loop() {
// Read the latest value from field 4 of channel 12397
float temperatureInF = ThingSpeak.readFloatField(weatherStationChannelNumber, temperatureFieldNumber);

Serial.print(“Current temp is: “);
Serial.print(temperatureInF);
Serial.println(” degrees F”);

delay(30000); // Note that the weather station only updates once a minute
}

===========================================================================

/*
ESP8266 –> ThingSpeak Channel via MKR1000 Wi-Fi

This sketch sends the value of Analog Input (A0) to a ThingSpeak channel
using the ThingSpeak API (https://www.mathworks.com/help/thingspeak).

Requirements:

* ESP8266 Wi-Fi Device
* Arduino 1.6.9+ IDE
* Additional Boards URL: http://arduino.esp8266.com/stable/package_esp8266com_index.json
* Library: esp8266 by ESP8266 Community

ThingSpeak Setup:

* Sign Up for New User Account – https://thingspeak.com/users/sign_up
* Create a new Channel by selecting Channels, My Channels, and then New Channel
* Enable one field
* Note the Channel ID and Write API Key

Setup Wi-Fi:
* Enter SSID
* Enter Password

Tutorial: http://nothans.com/measure-wi-fi-signal-levels-with-the-esp8266-and-thingspeak

Created: Feb 3, 2017 by Hans Scharler (http://nothans.com)
*/

#include <ESP8266WiFi.h>

// Wi-Fi Settings
const char* ssid = “XXX”; // your wireless network name (SSID)
const char* password = “XXXXXXXX”; // your Wi-Fi network password

WiFiClient client;

// ThingSpeak Settings
const int channelID = XXX;
String writeAPIKey = “XXXXXXXXXXXXXXXX”; // write API key for your ThingSpeak Channel
const char* server = “api.thingspeak.com”;
const int postingInterval = 20 * 1000; // post data every 20 seconds

void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
}

void loop() {
if (client.connect(server, 80)) {

// Measure Analog Input (A0)
int valueA0 = analogRead(A0);

// Construct API request body
String body = “field1=”;
body += String(valueA0);

Serial.print(“A0: “);
Serial.println(valueA0);

client.print(“POST /update HTTP/1.1\n”);
client.print(“Host: api.thingspeak.com\n”);
client.print(“Connection: close\n”);
client.print(“X-THINGSPEAKAPIKEY: ” + writeAPIKey + “\n”);
client.print(“Content-Type: application/x-www-form-urlencoded\n”);
client.print(“Content-Length: “);
client.print(body.length());
client.print(“\n\n”);
client.print(body);
client.print(“\n\n”);

}
client.stop();

// wait and then post again
delay(postingInterval);
}

Advertisements