วันจันทร์ที่ 18 ธันวาคม พ.ศ. 2560

อ้าว v.1

/*  NETPIE ESP8266 basic sample                            */
/*  More information visit : https://netpie.io             */

#include <ESP8266WiFi.h>
#include <MicroGear.h>

const char* ssid     = "Tony24HoursEX";
const char* password = "123456789123";

#define APPID   "Tony24Hours3"
#define KEY     "hUqvkEgoRp5xCRM"
#define SECRET  "7a8d2U9QQut3SOInwYwFoBIdt"
#define ALIAS   "Node1"

#include <Wire.h>                   // Include library
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET -1
Adafruit_SSD1306 OLED(OLED_RESET);  // New object OLED

WiFiClient client;


int vr    = 0;
int timer = 0;

MicroGear microgear(client);

void onMsghandler(char *topic, uint8_t* msg, unsigned int msglen)
{
  Serial.print("Incoming message --> ");
  msg[msglen] = '\0';
  Serial.println((char *)msg); 
}
void onConnected(char *attribute, uint8_t* msg, unsigned int msglen)
{
  Serial.println("Connected to NETPIE...");
  microgear.setAlias(ALIAS);
}
void setup()
{
  OLED.begin(SSD1306_SWITCHCAPVCC, 0x3C); 
  pinMode(A0, INPUT);
  /* Add Event listeners */
  /* Call onMsghandler() when new message arraives */
  microgear.on(MESSAGE, onMsghandler);
  /* Call onConnected() when NETPIE connection is established */
  microgear.on(CONNECTED, onConnected);

  Serial.begin(115200);
  Serial.println("Starting...");

  /* 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 */
 unsigned long timeoutconnect = millis();
  if (WiFi.begin(ssid, password))
  {
    while (WiFi.status() != WL_CONNECTED)
    {
      delay(500);
      Serial.print(".");
      if (millis() - timeoutconnect > 8000)
      {
        break;
      }
    }
  }

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

  /* connect to NETPIE to a specific APPID */
  microgear.connect(APPID);
}

void loop()
{
  if (microgear.connected())
  {
    vr = analogRead(A0);
    if(vr>500)
    {
      vr = 500;
    }
    OLED.clearDisplay();               //Clear display
    OLED.setTextColor(WHITE);          //Set text color
    OLED.setCursor(50,0);               //Set display start position
    OLED.setTextSize(1);               //Set text size x1   
    OLED.println("VALUE");
    OLED.setCursor(50,10);
    OLED.setTextSize(2);
    OLED.println(String(vr));             //Type message
    OLED.display();
    if(timer>=1000)
  {
    microgear.loop();   
    Serial.println("connected");   
    microgear.chat("Node2",vr);
    //microgear.publish("/Vr1",String(vr));
    timer = 0;
  }
  else timer += 100;
  }
  else
  {
    Serial.println("connection lost, reconnect...");
    if (timer >= 5000)
    {
      microgear.connect(APPID);
      timer = 0;
    }
    else timer += 100;
  }
  delay(100);
}

ไม่มีความคิดเห็น:

แสดงความคิดเห็น