Devacron.com

K-Type sensor readings in cloud with Arduino + ESP8266 [IoT]

 

The last few nights I am playing with an Esp8266 module that I had laying around for months and never connected it to my local wifi. Its one of the first modules, the ESP-01. The plan was to connect it to an Arduino mini pro or Arduino nano which has a K-Type temperature sensor attached to it. Then the readings of this sensor would be passed to ESP8266 through the serial pins (RX/TX) and send them to Thingspeak.com.

Actually it took me a while because I had to battle with different voltages (Esp8266 needs 3.3v NOT 5v) and due to the lack of ftdi chip in arduino mini pro. But finally I succeeded and here is the code and the result.

#include <SPI.h>
#include <Adafruit_MAX31855.h>
#include<stdlib.h>
#define SSID "YOUR SSID"//your network name
#define PASS "YOUR PASSWORD"//your network password
#define IP "184.106.153.149" // thingspeak.com
#define Baud_Rate 115200 //Another common value is 9600
#define DELAY_TIME 10000 //time in ms between posting data to ThingSpeak

int thermoCLK = 11;
int thermoCS = 12;
int thermoDO = 13;
Adafruit_MAX31855 thermocouple(thermoCLK, thermoCS, thermoDO);
String GET = "GET /update?key=YOUR_KEY&field1=";
bool updated;

void setup()
{
  Serial.begin(Baud_Rate);
  Serial.println("AT");
  
  delay(5000);
  
  if(Serial.find("OK")){
    //connect to your wifi netowork
    bool connected = connectWiFi();
    if(!connected){
      Serial.println("failure, not connected");
    }
  }else{
    Serial.println("failure, need to check your values and try again");
  }                     
}
 
void loop()
{
double c = thermocouple.readCelsius();
   if (isnan(c)) 
   {
     Serial.print("T/C Problem");
   } 
   else 
   { 
     Serial.print(c);
     updated = updateTemp(String(c));
   }

 delay(DELAY_TIME);
}


bool updateTemp(String temp){
  //initialize your AT command string
  String cmd = "AT+CIPSTART=\"TCP\",\"";
  
  //add IP address and port
  cmd += IP;
  cmd += "\",80";
  
  //connect
  Serial.println(cmd);
  delay(2000);
  if(Serial.find("Error")){
    return false;
  }
  
  //build GET command, ThingSpeak takes Post or Get commands for updates, I use a Get
  cmd = GET;
  cmd += temp;  
  cmd += "\r\n";
  
  //Use AT commands to send data
  Serial.print("AT+CIPSEND=");
  Serial.println(cmd.length());
  if(Serial.find(">")){
    //send through command to update values
    Serial.print(cmd);
  }else{
    Serial.println("AT+CIPCLOSE");
  }
  
  if(Serial.find("OK")){
    //success! Your most recent values should be online.
    return true;
  }else{
    return false;
  }
}
 
boolean connectWiFi(){
  //set ESP8266 mode with AT commands
  Serial.println("AT+CWMODE=1");
  delay(2000);

  //build connection command
  String cmd="AT+CWJAP=\"";
  cmd+=SSID;
  cmd+="\",\"";
  cmd+=PASS;
  cmd+="\"";
  
  //connect to WiFi network and wait 5 seconds
  Serial.println(cmd);
  delay(5000);
  
  //if connected return true, else false
  if(Serial.find("OK")){
    return true;
  }else{
    return false;
  }
}

You can check my github repository for more. You will also find there an example with a TMP36 temperature sensor.

Exit mobile version