Skip to content

Instantly share code, notes, and snippets.

@electronut
Created December 19, 2014 10:11
Show Gist options
  • Save electronut/0574eed177f699b92851 to your computer and use it in GitHub Desktop.
Save electronut/0574eed177f699b92851 to your computer and use it in GitHub Desktop.
Plot LM35 data on thingspeak.com using an Arduino and an ESP8266 WiFi module.
// esp8266_test.ino
//
// Plot LM35 data on thingspeak.com using an Arduino and an ESP8266 WiFi
// module.
//
// Author: Mahesh Venkitachalam
// Website: electronut.in
#include <SoftwareSerial.h>
#include <stdlib.h>
// LED
int ledPin = 13;
// LM35 analog input
int lm35Pin = 0;
// replace with your channel's thingspeak API key
String apiKey = "T2RJXWQAVXG4ZV39";
// connect 10 to TX of Serial USB
// connect 11 to RX of serial USB
SoftwareSerial ser(10, 11); // RX, TX
// this runs once
void setup() {
// initialize the digital pin as an output.
pinMode(ledPin, OUTPUT);
// enable debug serial
Serial.begin(9600);
// enable software serial
ser.begin(9600);
// reset ESP8266
ser.println("AT+RST");
}
// the loop
void loop() {
// blink LED on board
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
// read the value from LM35.
// read 10 values for averaging.
int val = 0;
for(int i = 0; i < 10; i++) {
val += analogRead(lm35Pin);
delay(500);
}
// convert to temp:
// temp value is in 0-1023 range
// LM35 outputs 10mV/degree C. ie, 1 Volt => 100 degrees C
// So Temp = (avg_val/1023)*5 Volts * 100 degrees/Volt
float temp = val*50.0f/1023.0f;
// convert to string
char buf[16];
String strTemp = dtostrf(temp, 4, 1, buf);
Serial.println(strTemp);
// TCP connection
String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += "184.106.153.149"; // api.thingspeak.com
cmd += "\",80";
ser.println(cmd);
if(ser.find("Error")){
Serial.println("AT+CIPSTART error");
return;
}
// prepare GET string
String getStr = "GET /update?api_key=";
getStr += apiKey;
getStr +="&field1=";
getStr += String(strTemp);
getStr += "\r\n\r\n";
// send data length
cmd = "AT+CIPSEND=";
cmd += String(getStr.length());
ser.println(cmd);
if(ser.find(">")){
ser.print(getStr);
}
else{
ser.println("AT+CIPCLOSE");
// alert user
Serial.println("AT+CIPCLOSE");
}
// thingspeak needs 15 sec delay between updates
delay(16000);
}
@Badreddinez
Copy link

Hello, I am trying to use the same code but for 6 data readings (NO2, CO, Pressure, temperature, Ozone, and Humidity). I am using similar code to yours and I would like to ask you few questions:

  1. How the Shield connects to WiFi? Should it be already configured?
  2. I am sending data each 15 seconds, but I received lot of "AT+CIPCLOSE" if possible to explain to me why is that?
    Thank you so much in advance.

@luffyzoro94
Copy link

// prepare GET string
String getStr = "GET /update?api_key="; <-------need to change or not?
getStr += apiKey;
getStr +="&field1=";
getStr += String(strTemp);
getStr += "\r\n\r\n";

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment