Go Down

Topic: sketch help / uploading temp reading from A0  (Read 164 times) previous topic - next topic

MrHutchinsonMN

I am working on a project for my homemade greenhouse, using an arduino uno, thermistor, esp2866 wifi, and 5v relay. I need help modifying the included sketch, so it sends temp results of the thermistor (currently on pin A0 ) to thingspeak. 

Here is the sketch that needs to be modified:

Code: [Select]
#include <SoftwareSerial.h>
#define RX 10
#define TX 11
String AP = "xxx";       // CHANGE ME
String PASS = "xxx"; // CHANGE ME
String API = "xxx";   // CHANGE ME
String HOST = "api.thingspeak.com";
String PORT = "80";
String field = "field0";
int countTrueCommand;
int countTimeCommand;
boolean found = false;
int valSensor = 1;
SoftwareSerial esp8266(RX,TX);
 
 
void setup() {
  Serial.begin(9600);
  esp8266.begin(115200);
  sendCommand("AT",5,"OK");
  sendCommand("AT+CWMODE=1",5,"OK");
  sendCommand("AT+CWJAP=\""+ AP +"\",\""+ PASS +"\"",20,"OK");
}
void loop() {
 valSensor = getSensorData();
 String getData = "GET /update?api_key="+ API +"&"+ field +"="+String(valSensor);
sendCommand("AT+CIPMUX=1",5,"OK");
 sendCommand("AT+CIPSTART=0,\"TCP\",\""+ HOST +"\","+ PORT,15,"OK");
 sendCommand("AT+CIPSEND=0," +String(getData.length()+4),4,">");
 esp8266.println(getData);delay(1500);countTrueCommand++;
 sendCommand("AT+CIPCLOSE=0",5,"OK");
}
int getSensorData(){
  return random(1000); // Replace with
}
void sendCommand(String command, int maxTime, char readReplay[]) {
  Serial.print(countTrueCommand);
  Serial.print(". at command => ");
  Serial.print(command);
  Serial.print(" ");
  while(countTimeCommand < (maxTime*1))
  {
    esp8266.println(command);//at+cipsend
    if(esp8266.find(readReplay))//ok
    {
      found = true;
      break;
    }
 
    countTimeCommand++;
  }
 
  if(found == true)
  {
    Serial.println("OYI");
    countTrueCommand++;
    countTimeCommand = 0;
  }
 
  if(found == false)
  {
    Serial.println("Fail");
    countTrueCommand = 0;
    countTimeCommand = 0;
  }
 
  found = false;
 }




PaulS

Quote
Here is the sketch that needs to be modified
It currently does something that you haven't explained.

It is to do something that you haven't properly explained.

You can't get from point A to point B if you don't know where either point is.

Robin2

Here is the sketch that needs to be modified:
What does it actually do and what do you want it to do that is different?

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

MrHutchinsonMN

#3
Apr 09, 2019, 09:04 pm Last Edit: Apr 09, 2019, 09:05 pm by MrHutchinsonMN
Ultimately, I am attempting to find a sketch that logs temperature data, and fires off my 5v relay when the temps dip below 50 degrees Fahrenheit. I found the sketch posted and was able to get wifi to connect and make a connection to my thingspeak channel. However, I am not sure how to change the way it is currently written ( int valSensor = 1;, void loop() {
 valSensor = getSensorData) and post data from A0, instead. I wanted to get that much working before adding the code for the relay (have a sketch that works for that but need to merge the two).


I have found other sketches that I thought should work but was unsuccessful in getting them to do what I needed. Just the joys of another novice.

Thanks for the advice :)



Robin2

Ultimately, I am attempting to find a sketch that logs temperature data, and fires off my 5v relay when the temps dip below 50 degrees Fahrenheit. I found the sketch posted and was able to get wifi to connect and make a connection to my thingspeak channel.
Does it send any data? If so what data?

Knowing what it does do seems to me an essential prerequisite for figuring out how to get it to do something different. YMMV.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

PaulS

Quote
Ultimately, I am attempting to find a sketch
For myself, I find that it is much faster to fire up the IDE and start typing. Google is great, but the chances of finding a sketch that someone else wrote that uses exactly the same hardware, on exactly the same pins, and is documented well are pretty small.

Quote
and post data from A0, instead
Changing
Code: [Select]
   return random(1000);
to
Code: [Select]
   return analogRead(0); // Read analog pin 0
seems incredibly simple.

MrHutchinsonMN

I was not  able to work more on the project last week but will confirm the results soon.

I appreciate your help!

kc2jca

#7
Apr 15, 2019, 03:58 pm Last Edit: Apr 15, 2019, 04:02 pm by kc2jca
What is the output of the thermister? A0 is going to give you a value from 0-1023 (1024 units).

For a LM36 or TMP36, you would normally convert this to
Centigrade Temp by ADC_Value * 5 / 1024... 5 being the reference Voltage if you are in default.
Then subtract whatever offset, and divide by 10 to get the temperature.

The thermister data sheet should tell you how many mV per degree the output would provide. Then it seems to me that you can just:

if(temp <= 50)...turn on relay... else...turn off relay


But I have a friend who tells me I over think these things.

Go Up