/* More information visit : https://netpie.io */
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
//#include <WiFiManager.h>
#include <MicroGear.h>
#include <Wire.h>
//#include <LiquidCrystal_I2C.h>
// D2 Nodemcu => SDA D1 Nodemcu => SCL
//LiquidCrystal_I2C lcd(0x27, D2, D1);
#include <time.h>
const char* ssid = "iotEEP2018" ; // Username WiFI
const char* password = "24992499pkseep" ; // password
#define APPID "Appid"
#define KEY "89saRcFF7aQDuO4"
#define SECRET "JApBUoELn7Ht5AoStSUju4KmI"
#define ALIAS "esp8266"
const int MAX_CURRENT = 330 ;
const int MIN_CURRENT = 0 ;
const int VcurrentSentMAX = 47 ;
const int VcurrentSentMIN = 0 ;
const int CrossVerical = 0 ;
float DeltaX = 0.144242;
float CuurentM ;
WiFiClient client;
MicroGear microgear(client);
int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)
int State_Relay_1 ;
int State_Relay_2 ;
int State_Relay_3 ;
int State_Relay_4 ;
int Measure_Battery ;
int Measure_C ;
/*------------configulation Pin----------------*/
const int ConfigWiFi_Pin = 0 ;
const int Relay1_PIN = 14 ;
const int Relay2_PIN = 12 ;
const int Relay3_PIN = 13 ;
const int Relay4_PIN = 15 ;
const int LED_STATUS_PIN = 2 ;
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
int Relay1 = 0 ;
int Relay2 = 0 ;
int Relay3 = 0 ;
int Relay4 = 0 ;
// command interpreter
String rcvdstring; // string received from serial
String cmdstring; // command part of received string
String parmstring; // parameter part of received string
int parmvalint; // parameter value
int sepIndex; // index of seperator
/* If a new message arrives, do this */
void onMsghandler(char *topic, uint8_t* msg, unsigned int msglen) {
Serial.println("") ;
Serial.print("Incoming message --> ");
msg[msglen] = '\0';
Serial.print((char *)msg);
Serial.println("") ;
/*Sub String */
char *m = (char *)msg;
rcvdstring = m;
// cmdInt();
/*..................... */
if (*(char *)msg == 'a')
{
digitalWrite( Relay1_PIN , HIGH);
State_Relay_1 = 1 ;
}
if (*(char *)msg == 'b')
{
digitalWrite( Relay1_PIN , LOW);
State_Relay_1 = 0 ;
}
if (*(char *)msg == 'c')
{
digitalWrite( Relay2_PIN , HIGH);
State_Relay_2 = 1 ;
}
if (*(char *)msg == 'd')
{
digitalWrite( Relay2_PIN , LOW);
State_Relay_2 = 0 ;
}
if (*(char *)msg == 'e')
{
digitalWrite( Relay3_PIN , HIGH);
State_Relay_3 = 1 ;
}
if (*(char *)msg == 'f')
{
digitalWrite( Relay3_PIN , LOW);
State_Relay_3 = 0 ;
}
if (*(char *)msg == 'g')
{
digitalWrite( Relay4_PIN , HIGH);
State_Relay_4 = 1 ;
}
if (*(char *)msg == 'h')
{
digitalWrite( Relay4_PIN , LOW);
State_Relay_4 = 0 ;
}
}
/* When a microgear is connected, do this */
void onConnected(char *attribute, uint8_t* msg, unsigned int msglen)
{
//Serial.println("Connected to NETPIE...");
/* Set the alias of this microgear ALIAS */
// microgear.setAlias(ALIAS);
}
void setup() {
pinMode(Relay1_PIN , OUTPUT);
pinMode(Relay2_PIN , OUTPUT);
pinMode(Relay3_PIN , OUTPUT);
pinMode(Relay4_PIN , OUTPUT);
pinMode(LED_STATUS_PIN , OUTPUT);
/* Add Event listeners */
/* Call onMsghandler() when new message arraives */
microgear.on(MESSAGE,onMsghandler);
microgear.on(CONNECTED,onConnected);
// lcd.begin();
Serial.begin(115200);
delay(10);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
Serial.println("Starting...");
Serial.setDebugOutput(true);
/* Initial WIFI, this is just a basic method to configure WIFI on ESP8266. */
/* You may want to use other method that is more complicated, but provide better user experience */
if (WiFi.begin(ssid, password)) {
while (WiFi.status() != WL_CONNECTED) {
delay(250);
Serial.print(".");
}
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
/* Initial with KEY, SECRET and also set the ALIAS here */
microgear.init(KEY,SECRET,ALIAS);
microgear.connect(APPID);
} //void Setup
void loop() {
/*.........................ADC input ....................................*/
sensorValue = analogRead(analogInPin);
outputValue = map(sensorValue, 0, 1023, 0, 330);
Measure_C = outputValue ;
Measure_Battery = ((DeltaX * Measure_C) - CrossVerical)/1.414 ;
if(Measure_C < 0 ) Measure_C =0;
/* To check if the microgear is still connected */
if (microgear.connected())
{
digitalWrite(LED_STATUS_PIN , HIGH);
microgear.loop();
delay(200);
microgear.publish("/State_Relay_1",State_Relay_1 );
microgear.publish("/State_Relay_2",State_Relay_2 );
delay(200);
microgear.publish("/State_Relay_3",State_Relay_3 );
microgear.publish("/State_Relay_4",State_Relay_4 );
delay(200);
microgear.publish("/Measure_Battery",Measure_Battery );
}
else
{
Serial.println("connection lost, reconnect...");
microgear.connect(APPID);
digitalWrite(LED_STATUS_PIN , LOW);
}
/*.............................................................*/
delay(100);
}
หน้าที่เข้าชม | 7,125,785 ครั้ง |
ผู้ชมทั้งหมด | 2,822,683 ครั้ง |
เปิดร้าน | 15 ก.ย. 2557 |
ร้านค้าอัพเดท | 6 ก.ย. 2568 |