Wednesday, August 11, 2021

Energia code for IoT Based Solar tracking system using cc3200

 #ifndef __CC3200R1M1RGC__

#include <SPI.h>
#endif
#include <WiFi.h>
#include <Wire.h>
#include "Adafruit_TMP006.h"
#include <stdlib.h>

char thingSpeakAddress[] = "api.thingspeak.com";
String writeAPIKey = "LN531UWIR5RRSZCL";
const int updateThingSpeakInterval = 16 * 1000; 
char buffer[25];
char ssid[] = "R2C";
char password[] = "r2cudt@2021";
WiFiClient client;
unsigned long lastConnectionTime = 0; 
boolean lastConnected = false; 
const unsigned long postingInterval = 10*1000; 
int failedCounter = 0;
Adafruit_TMP006 tmp006(0x41); 

void setup() 
{
    Serial.begin(115200);
    pinMode(2,INPUT);
    pinMode(10,OUTPUT);
    pinMode(29,OUTPUT);
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);
    WiFi.begin(ssid, password);
    while ( WiFi.status() != WL_CONNECTED) 
    { 
      Serial.print(".");
      delay(300);
    }
    if (! tmp006.begin()) 
    {
      Serial.println("No sensor found");
      while (1);
    }
    Serial.println("\nYou're connected to the network");
    Serial.println("Waiting for an ip address");
    while (WiFi.localIP() == INADDR_NONE) 
    {
      Serial.print(".");
      delay(300);
    }
    Serial.println("\nIP Address obtained");
    printWifiStatus();

}

void loop() 
{
    while (client.available()) 
    {
      char c = client.read();
      Serial.print(c);
    }
    if (!client.connected() && lastConnected) 
    {
      Serial.println();
      Serial.println("disconnecting.");
      client.stop();
    }
    
  ///////////////////////////////////  solar Tracker System ///////////////////////////////////

 
    if (!client.connected() && (millis() - lastConnectionTime > postingInterval)) 
    {
        
        
        int solarpanel= analogRead(6);
        int solarstatus=0;
        int volt=0;
        if (solarpanel<200)
        {
            digitalWrite(9,LOW); 
            digitalWrite(10,LOW);
            solarstatus=1;
        }
        else
        {
            digitalWrite(9,LOW); 
            digitalWrite(10,HIGH); 
            delay(400);             // TRACKING FOR POSITION 
            digitalWrite(9,HIGH); 
            digitalWrite(10,LOW);
            solarstatus=0;
        }



 
            Serial.print("SOLAR TRACKING STATUS");
            if(solarpanel>200)
            {
                  volt=solarpanel*(3.0/4096.0); 
                  Serial.print(solarpanel);
                  Serial.print(" V");
            }
            else
            {
                   Serial.print("voltage not produced");
            }
            
            String sir = dtostrf(volt,3,3,buffer);
            Serial.print(sir);
            Serial.print(" V");
            String st= dtostrf(solarstatus,3,3,buffer);
            Serial.print(st);
            updateThingSpeak("field1=" + sir+"&field2=" + st);

    }
    lastConnected = client.connected();
}
void updateThingSpeak(String tsData)
{
      if (client.connect(thingSpeakAddress, 80))
      {
          client.print("POST /update HTTP/1.1\n");          
          client.print("Host: api.thingspeak.com\n");
          client.print("Connection: close\n");    
          client.print("X-THINGSPEAKAPIKEY: "+writeAPIKey+"\n");
          client.print("Content-Type: application/x-www-form-urlencoded\n");
          client.print("Content-Length: ");
          client.print(tsData.length());
          Serial.println(">>TSDATALength=" + tsData.length());
          client.print("\n\n");
          client.print(tsData);
          Serial.println(">>TSDATA=" + tsData);
          lastConnectionTime = millis();
          if (client.connected())    
          {
              Serial.println("Connecting to ThingSpeak...");
              Serial.println();
              failedCounter = 0;          
          }          
          else          
          {          
              failedCounter++;          
              Serial.println("Connection to ThingSpeak failed ("+String(failedCounter, DEC)+")");          
              Serial.println();          
          }
    }  
    else
    {
        failedCounter++;
        Serial.println("Connection to ThingSpeak Failed ("+String(failedCounter, DEC)+")");
        Serial.println();
        lastConnectionTime = millis();
    }

}
int getLength(int someValue) 
{
    int digits = 1;  
    int dividend = someValue / 10;
    while (dividend > 0)
    {
      dividend = dividend / 10;
      digits++;
    }
    return digits;
}
void printWifiStatus() 
{
    Serial.print("SSID: ");
    Serial.println(WiFi.SSID());
    IPAddress ip = WiFi.localIP();
    Serial.print("IP Address: ");
    Serial.println(ip);
    long rssi = WiFi.RSSI();
    Serial.print("signal strength (RSSI):");
    Serial.print(rssi);
    Serial.println(" dBm");
}

USD TO INR C++ PROGRAMMING

  NALLAMUTHU GOUNDER MAHALINGAM COLLEGE                                                 POLLACHI-642001                                     ...