Adhyoksh Jyoti
Published

IoT with ThingSpeak

An easy project to demonstrate the use of ThingDpeak in IoT.

IntermediateProtip1 hour5,984
IoT with ThingSpeak

Things used in this project

Story

Read more

Schematics

Circuit

Code

Arduino code

Arduino
#include <SoftwareSerial.h>
#define RX 2
#define TX 3
String AP = "Wifi_Name";       // AP NAME
String PASS = "Wifi_Pwd"; // AP PASSWORD
String API = "Api_Key";   // Write API KEY
String HOST = "api.thingspeak.com";
String PORT = "80";
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() {
  
 String getData = "GET /update?api_key="+ API +"&field1="+getTemperatureValue()+"&field2="+getLDRValue();
 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");
}


String getTemperatureValue()
{
  int temp;
  temp=analogRead(A1)/10.24;    
  return String(temp);   
}


String getLDRValue()
{
 int ldr=analogRead(A0); 
 return String(ldr);  
}

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;
 }

Credits

Adhyoksh Jyoti
11 projects • 9 followers
Electronics and Communication Engineering B.Tech graduate from NIT Srinagar, J&K.

Comments