Quantcast
Channel: ThingSpeak IoT Community - Group: Hardware
Viewing all articles
Browse latest Browse all 437

chputney on Coding ..... How?

$
0
0

Here is what works for me:

 

#include <Esp.h>
#include <user_interface.h>
#include "display.h"
#include "button.h"
#include "onewire.h"
#include <ESP8266WiFi.h>

 

// boot log - connect putty to com port at 74880 baud to see boot log

// Thermostat GPIO pin defines
// pins to avoid GPIO0 (D3), GPIO2(D4), GPIO15(D8)

 

#define DS18B20 2 // D4 on mini = GPIO2 on ESP8266
#define SCL 12 // D6 on mini = GPIO12 on ESP8266
#define SDA 13 // D7 on mini = GPIO13 on ESP8266
#define BUTTON 14 // D5 on mini = GPIO14 on ESP8266

#define TIMEOUT 500 // 5 seconds

// ------------- wifi and http defines --------------------
#define ssid "xxxxx"
#define password "xxxxx"
#define THINGSPEAKIP "184.106.153.149" // thingspeak.com
#define host "api.thingspeak.com"

uint32_t RTC_Memory[10];
int Current_Temperature, Current_Setpoint;
struct rst_info *Reset_Info; // first int32 is the reason 5 = reset button, 6 = deep sleep
int SleepTime = TIMEOUT;
bool Temp_displayed = false;
bool Show_Setpoint_or_Temperature = false; // Setpoint = false, Temperature = true
String httpstring;
String ThingSpeak = "GET http://api.thingspeak.com/update?api_key=VPTOJBWMJSADEDO806RQM&field1=";

 

void setup()
{
Serial.begin(9600);
pinMode(SCL, OUTPUT);
pinMode(SDA, OUTPUT);
pinMode(DS18B20, OUTPUT);
pinMode(BUTTON, INPUT_PULLUP);
digitalWrite(BUILTIN_LED, HIGH); // make sure the LED is off

Reset_Info = ESP.getResetInfoPtr();
Serial.print("
System Reset Reason: ");
Serial.print(Reset_Info->reason);
Serial.print(" ");
Serial.println(ESP.getResetReason());

//get last temperature and setpoint
ESP.rtcUserMemoryRead(0, (uint32_t*) &RTC_Memory, 10) ;
Current_Temperature = (int)RTC_Memory[0];
if (Current_Temperature < 0 || Current_Temperature > 30) Current_Temperature = 19;
Current_Setpoint = (int)RTC_Memory[1];
if (Current_Setpoint < 10 || Current_Setpoint > 25) Current_Setpoint = 20;

// get the current temperature
Request_Temperature();
if (Reset_Info->reason == 5) // we have awoken from deep sleep
{
Report_Temperature(); // report the current temperature over wifi and go back to sleep
Goto_Sleep();
}
else // the user pushed the button - show the display

{
Display_init();
Display_clear();
Show_Setpoint_or_Temperature = true;
}

}

void loop()
{
if (Timer()) // do this every 10 milliseconds
{

ESP.wdtFeed(); // watchdog is reset

if (Temp_displayed == false)
{
Display_clear();
if (Show_Setpoint_or_Temperature) Show_Temperature(); else Show_Setpoint();
Temp_displayed = true;
}

byte but = Check_Button();
if (but > 0) SleepTime = TIMEOUT;
switch (but)
{

case 3: // 3 = Button was pressed for COUNT_UP
Current_Setpoint++;
if (Current_Setpoint > 25) Current_Setpoint = 25;
Temp_displayed = false;
Show_Setpoint_or_Temperature = false;
break;

case 4: // 4 = Button was pressed for COUNT_DOWN
Current_Setpoint--;
if (Current_Setpoint <15) Current_Setpoint = 15;
Temp_displayed = false;
Show_Setpoint_or_Temperature = false;
break;
}
SleepTime--;
if (SleepTime == 0) Goto_Sleep(); // shutdown

}
}

bool Timer(void)
{
static unsigned long Time;
if (millis() > Time)
{
Time += 10;
return true;
}
return false;
}

void Goto_Sleep(void) // sleep for 10 minutes
{
Display_off();
RTC_Memory[0] = (uint32_t)Current_Temperature;
RTC_Memory[1] = (uint32_t)Current_Setpoint;
ESP.rtcUserMemoryWrite(0, (uint32_t*) &RTC_Memory, 10) ; // save Setpoint
ESP.deepSleep(600e6, (RFMode)(2)); // no RF calibration on startup
}

void Show_Temperature(void)
{
Display_setfont(1);
Display_goto(30,0);
Display_chars("Temperature");
Display_setfont(4);
Display_goto(40,20);
Display_char(Current_Temperature/10+0x30);
Display_char(Current_Temperature%10+0x30);
Display_update();
}

void Show_Setpoint(void)
{
Display_setfont(1);
Display_goto(30,0);
Display_chars(" Setpoint ");
Display_setfont(4);
Display_goto(40,20);
Display_char(Current_Setpoint/10+0x30);
Display_char(Current_Setpoint%10+0x30);
Display_update();
}

void Show_Failure_Reason(const char *reason)
{
long timenow;
Display_init();
Display_clear();
Display_setfont(1);
Display_goto(20,26);
Display_chars(reason);
Display_update();
timenow = millis();
while ((millis() - timenow) < 3000); // show for 3 seconds
}

void Report_Temperature(void)
{
long timenow;
long WiFiTime; // time to establish wifi connection
long ClientTime; // time to connect to thingspeak server

// start wifi station

WiFi.persistent(false);
//WiFi.mode(WIFI_OFF);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);

timenow = millis();
while (WiFi.isConnected() == false)
{
delay(100);
ESP.wdtFeed();
WiFiTime = millis() - timenow;
if (WiFiTime > 10000)
{
Serial.println("WiFi Connection Timeout !");
Show_Failure_Reason("WiFi Timeout");
Goto_Sleep(); // if no connection after 10 seconds - give up
}
}
Serial.print("WiFi Connection after ");
Serial.print(WiFiTime/1000);
Serial.print(".");
Serial.print(WiFiTime%1000);
Serial.println(" seconds");

Current_Temperature = Get_Temperature();
Serial.print("Current Temperature = ");
Serial.println(Current_Temperature);
WiFiClient client;

httpstring = ThingSpeak + Current_Setpoint + "&field2=" + Current_Temperature + "&field3=" + WiFiTime;

if (client.connect(host, 80))
{
Serial.println("connected");
Serial.println(httpstring);
client.println(httpstring);

timenow = millis();
while (client.connected())
{
if (client.available())
{
String line = client.readStringUntil('
');
Serial.println(line);
}
ClientTime = millis() - timenow;
if (ClientTime > 5000)
{
Serial.println("thingspeak Connection Timeout !");
Show_Failure_Reason("Server Timeout");
Goto_Sleep(); // if no connection after 5 seconds - give up
}
}
client.stop();
Serial.println("
Disconnected");
}
else
{
Serial.println("connection failed !");
client.stop();
}

client.stop();
WiFi.mode(WIFI_OFF);
}


Viewing all articles
Browse latest Browse all 437

Trending Articles