ESP8266 wifi module data logger?

​hello everyone, can anybody do a simpler project with this? I just want to collect data using arduino Analog0 input with an LDR photoresistor. collect the data and simply post it on Thingspeak ?!?!
.
all the examples I find are very complicated and include libraries. I need my project to be much simpler. just read some data from the analog sensor and display the value on the cloud.
.
can anybody please help me with this? I have updated the firmware on my ESP8266 and I can connect to my house network using the CoolTerm software.
.
any help is highly appreciated !!!
thank you,
akin
.
useful links
1. http://www.electrodragon.com/w/Wi07c
2. http://www.esp8266.com/
3. http://www.espruino.com/ESP8266
4. https://nurdspace.nl/ESP8266
5. http://hackaday.com/tag/esp8266/

sort by: active | newest | oldest
diy_bloke2 years ago

#include <SoftwareSerial.h>
int sensor_temp = A0;
int value_temp;
int sensor_light = A1;
int value_light;
int sensor_water = A2;
int value_water;
int status;

#define DEBUG FALSE //comment out to remove debug msgs

//*-- Hardware Serial
#define _baudrate 9600

//*-- Software Serial
//
#define _rxpin 2
#define _txpin 3
SoftwareSerial debug( _rxpin, _txpin ); // RX, TX



//*-- IoT Information
#define SSID "[SSID]"
#define PASS "[PASSW]"
#define IP "184.106.153.149" // ThingSpeak IP Address: 184.106.153.149
//
// GET /update?key=[THINGSPEAK_KEY]&field1=[data 1]&filed2=[data 2]...;
String GET = "GET /update?key=[ThingSpeak_(Write)API_KEY]";

void setup() {
// Serial.begin(9600);
pinMode (8, OUTPUT); //red led - temp
pinMode (10, OUTPUT); //yellow led - light
pinMode (12, OUTPUT); //blue led - water
pinMode (5, OUTPUT); //green led - overall health

Serial.begin( _baudrate );
debug.begin( _baudrate );

sendDebug("AT");
delay(5000);
if(Serial.find("OK"))
{
debug.println("RECEIVED: OK\nData ready to sent!");
connectWiFi();
}
}

void loop() {
status=0;
value_temp = analogRead(sensor_temp);
Serial.print("temperature");
Serial.println( value_temp );
value_light = analogRead(sensor_light);
Serial.print("light");
Serial.println( value_light );
value_water = analogRead(sensor_water);
Serial.print("water");
Serial.println( value_water );
String temp =String(value_temp);// turn integer to string
String light= String(value_light);// turn integer to string
String water=String(value_water);// turn integer to string
updateTS(temp,light, water);
delay(5000); //added delay here

if (value_temp < 290){
digitalWrite (8, HIGH); // plant too cold - red led on
status=1;
}
else{
digitalWrite (8, LOW); // normal temp - red led off
}
//-------------
if (value_light < 300) {
digitalWrite (10, HIGH); // not enough light - yellow led on
delay(300); // wait for a second
digitalWrite(10, LOW); // turn the LED off by making the voltage LOW
delay(300);
status=1;
}
else{
digitalWrite (10, LOW); // enough light - yellow led off
}
//-----------------
if (value_water < 300) {
digitalWrite (12, HIGH); // plant thirsty - blue led on
status=1;
}
else{
digitalWrite (12, LOW); // soil is moist - blue led off
}

if(status==0) {
digitalWrite (5, HIGH);
delay(300); // wait for a second
digitalWrite(5, LOW); // turn the LED off by making the voltage LOW
delay(300); // wait for a second);
}
else{
digitalWrite (5, LOW);
}

}
//----- update the Thingspeak string with 3 values
void updateTS( String T, String H , String W)
{
//
String cmd = "AT+CIPSTART=\"TCP\",\"";// Setup TCP connection
cmd += IP;
cmd += "\",80";
sendDebug(cmd);
delay(2000);
if( Serial.find( "Error" ) )
{
debug.print( "RECEIVED: Error\nExit1" );
return;
}

cmd = GET + "&field1=" + T +"&field2="+H+ "&field3=" + W +"\r\n";
Serial.print( "AT+CIPSEND=" );
Serial.println( cmd.length() );
if(Serial.find( ">" ) )
{
debug.print(">");
debug.print(cmd);
Serial.print(cmd);
}
else
{
sendDebug( "AT+CIPCLOSE" );//close TCP connection
}
if( Serial.find("OK") )
{
debug.println( "RECEIVED: OK" );
}
else
{
debug.println( "RECEIVED: Error\nExit2" );
}
}

void sendDebug(String cmd)
{
debug.print("SEND: ");
debug.println(cmd);
Serial.println(cmd);
}

boolean connectWiFi()
{
Serial.println("AT+CWMODE=1");//WiFi STA mode - if '3' it is both client and AP
delay(2000);
//Connect to Router with AT+CWJAP="SSID","Password";
// Check if connected with AT+CWJAP?
String cmd="AT+CWJAP=\""; // Join accespoint
cmd+=SSID;
cmd+="\",\"";
cmd+=PASS;
cmd+="\"";
sendDebug(cmd);
delay(5000);
if(Serial.find("OK"))
{
debug.println("RECEIVED: OK");
return true;
}
else
{
debug.println("RECEIVED: Error");
return false;
}

cmd = "AT+CIPMUX=0";// Set Single connection
sendDebug( cmd );
if( Serial.find( "Error") )
{
debug.print( "RECEIVED: Error" );
return false;
}
}

Akin Yildiz (author)  diy_bloke2 years ago

this code works with basic analog sensors on analog inputs A0-A1-A2

I have connected an LDR photocell for light, NTC thermsitor for temperature and soil probes for water level for my smart plant pot ans was able to upload data to thingspeak and then tweet if an alarm was triggered

.

the LEDs on digital outputs are for notifying the users nearby

.

contact me us for any questions you may have

thank you for the answer and everything helping/viewing

20141125_114807.jpgplant pot.png
Akin Yildiz (author) 3 years ago

just providing me with the code will help plenty. I have all the tools/items required to finish this project, all I need is the code pretty much.. this is the closest thing online BUT there are libraries used for the temperature sensor. imagine it was a simple NTC thermistor OR and LED photoresistor instead, with no libraries. much simpler/shorter code and just simple number values logged on thingspeak.

https://www.instructables.com/id/ESP8266-Wifi-Tempe...

AcmeUK2 years ago

IMPORTANT

In the latest version of the ESP8266 firmware AT+CIOBAUD is no longer supported and returns ERROR.

The replacement command is AT+IPR.

See here for further details :- www.esp8266.com/viewtopic.php?f=13&t=718

Hope this saves someone a bit of time.

Akin Yildiz (author)  AcmeUK2 years ago

hello acme,

thanks for the input. my code luckily didn't need that command. good for future reference tho.. this wifi module is amazing

Well taking the one-wire stuff out would be pretty easy. I don't understand the objection to libraries. Are you short of memory ?

Akin Yildiz (author)  steveastrouk3 years ago

no. I just don't understand/can't read it. too advanced for me. plus, I don't think they are even needed. how would you write the code if literally it just recorded an LDR data online with the ESP?

Here's a sketch that compiles, with the Dallas stuff removed, and an NTC thermistor on analoginput 3. See if it works.

<pre>

#include <stdlib.h>
#define SSID "[YOUR_SSID]"
#define PASS "[YOUR_PASSWORD]"
#define IP "184.106.153.149" // thingspeak.com

int NTCpin= 3;

int tempC = 0;

String GET = "GET /update?key=[THINGSPEAK_KEY]&field1=";

void setup()
{
Serial.begin(9600);
sendDebug("AT");
delay(5000);
if(Serial.find("OK")){
connectWiFi();
}
}

void loop(){
tempC = analogRead(NTCpin);
char buffer[10];
String tempF = dtostrf(tempC, 4, 1, buffer);
updateTemp(tempF);
delay(60000);
}

void updateTemp(String tenmpF){
String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += IP;
cmd += "\",80";
sendDebug(cmd);
delay(2000);
cmd = GET;
cmd += tenmpF;
cmd += "\r\n";
Serial.print("AT+CIPSEND=");
Serial.println(cmd.length());
if(Serial.find(">")){
Serial.print(cmd);
}else{
sendDebug("AT+CIPCLOSE");
}
}
void sendDebug(String cmd){
Serial.println(cmd);
}

boolean connectWiFi(){
Serial.println("AT+CWMODE=1");
delay(2000);
String cmd="AT+CWJAP=\"";
cmd+=SSID;
cmd+="\",\"";
cmd+=PASS;
cmd+="\"";
sendDebug(cmd);
delay(5000);
}

</pre>

Akin Yildiz (author)  steveastrouk3 years ago

what if you were to rewrite it without converting the temperature values into F or C. I just want raw numbers. just as it reads from the sensor, it's going to be a number like 540. i don't mind if it is not C or F. imagine it is an photoresistor. add something like;

int sensor_light = A0;

int value_light;

in the beginning, like you did above with the NTC

.

I changed my SSID & PASS also added the api key. this is what I see on the serial monitor.

AT

AT+CWMODE=1

AT+CWJAP="[8xxxxb]","[2xxxxxxxx9]"

AT+CIPSTART="TCP","184.106.153.149",80

AT+CIPSEND=49

AT+CIPCLOSE

AT+CIPSTART="TCP","184.106.153.149",80

AT+CIPSEND=49

AT+CIPCLOSE

it keeps repeating this, but no data is uploaded on thingspeak still..

Do you know if the module will access the internet on its own ? Can you write a thingspeak example into the arduino and try that ?

Akin Yildiz (author)  steveastrouk3 years ago

I don't know how to write that example, BUT while I was uploading the new firmware to the module and the initial setup, I was always able to list the access points and connect to my house wifi. what does the serial monitor readings say? does it give an error as to what is wrong? the first time I tried the setup with the previous unaltered code, the serial monitor wouldn't go as far in the code. it always stopped after the IP numbers if i remember correctly, never did the CIPSEND line.. if you can do the example you speak of, I can try it real quick. thank you so much for your help steve !!

Its looking like it doesn't like the format of the data you're sending it. Without the actual hardware its hard to work out what to do next. I deleted the original serial monitor bits in the code. Maybe you should put them back in, and put an external serial port on another couple of pins for debugging. Messy, but necessary.

Akin Yildiz (author)  steveastrouk3 years ago

I believe to have figured a few things out, hopefully I will have time to try it out tomorrow. the serial monitor was able to do some other stuff which I didn't get to catch and share with you. I will keep you posted.

I can´t tell anything about the network connection thing, but if I understand the script above in the right way you are trying to read out an 'integer' value via "analogRead" (which gives back "0-1023") but treat it as a 'double' value ("+/- xxx,xxxxxxxx" [just as an example]) and then try to convert it into a 'string' via the "dtostrf" function which needs a 'double' as input parameter that it probably doesn´t get (???).

You should be able to directly 'print' "tempC" out as an integer number to the network, I think. And that would be the raw numbers you wanted, e.g. "light" or "temperature" is a value between 0-1023.

Hope I got that right...

There are also a typo (I think?) check variable names tempF and tenmpF in the script.

But I think you won´t need them either, just print out whatever you get from "analogRead(XYZ)" directly, without converting to a string.

Or if you do need a string to print to thingspeak you maybe need a different function like sprintf or itoa...

Just suggesting, maybe I´m completely wrong though... It´s been a while since I messed around with code and/or arduinos...

My version did eliminate the conversion process. The only thing left now is turning a number into a string for the transmission to Thingspeak.

Akin Yildiz (author)  steveastrouk3 years ago

so what about these lines then? are these not for some sort of a conversion?

tempC = analogRead(NTCpin);

char buffer[10];

String tempF = dtostrf(tempC, 4, 1, buffer);

updateTemp(tempF);

.

??

No, they are, as I said, converting a number into a string for the write to the server.

Akin Yildiz (author)  steveastrouk3 years ago

imagine that i'm doing exactly this: https://www.instructables.com/id/ESP8266-Wifi-Tempe... , but with an NTC thermistor and/or LDR photoresistor.

.

basically i'm trying to get https://www.instructables.com/id/plant-health-monit... online and collect data on thingspeak.