Skip to content

Commit

Permalink
Merge pull request #2 from canusorn/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
canusorn authored Mar 6, 2022
2 parents 8d8474d + f137cf7 commit e67687b
Show file tree
Hide file tree
Showing 8 changed files with 911 additions and 36 deletions.
143 changes: 131 additions & 12 deletions examples/01acmeter/03acmeter_iotwebconf/03acmeter_iotwebconf.ino
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <IotWebConfUsing.h>
#include <ESP8266HTTPUpdateServer.h>
#include <ESP8266mDNS.h>
#include <EEPROM.h>
#include <iotbundle.h>

// 1 สร้าง object ชื่อ iot และกำหนดค่า(project)
Expand All @@ -41,10 +42,7 @@ const char wifiInitialApPassword[] = "iotbundle";
#define STRING_LEN 128
#define NUMBER_LEN 32

#define CONFIG_VERSION "0.0.1"
#define CONFIG_PIN D5
//#define IOTWEBCONF_CONFIG_USE_MDNS 80
//#define STATUS_PIN LED_BUILTIN
#define CONFIG_VERSION "0.0.2"

// -- Method declarations.
void handleRoot();
Expand Down Expand Up @@ -82,6 +80,9 @@ uint8_t wifi_off[] = {0x88, 0x44, 0x32, 0xDA, 0xCA, 0x16, 0x06, 0x09};
uint8_t wifi_ap[] = {0x3E, 0x41, 0x1C, 0x00, 0xF8, 0x00, 0x1C, 0x41, 0x3E};
uint8_t wifi_nointernet[] = {0x04, 0x12, 0xCA, 0xCA, 0x12, 0x04, 0x5F, 0xDF};
uint8_t t_connecting;
iotwebconf::NetworkState prev_state = iotwebconf::Boot;
uint8_t displaytime;
String noti;

void setup()
{
Expand All @@ -98,12 +99,28 @@ void setup()
oled.print(" IoTbundle");
oled.display();

// for clear eeprom jump D5 to GND
pinMode(D5, INPUT_PULLUP);
if (digitalRead(D5) == false)
{
delay(1000);
if (digitalRead(D5) == false)
{
oled.clear(PAGE);
oled.setCursor(0, 0);
oled.print("Clear All data\n rebooting");
oled.display();
delay(1000);
clearEEPROM();
}
}

login.addItem(&emailParam);
login.addItem(&passParam);
login.addItem(&serverParam);

// iotWebConf.setStatusPin(STATUS_PIN);
iotWebConf.setConfigPin(CONFIG_PIN);
// iotWebConf.setConfigPin(CONFIG_PIN);
// iotWebConf.addSystemParameter(&stringParam);
iotWebConf.addParameterGroup(&login);
iotWebConf.setConfigSavedCallback(&configSaved);
Expand All @@ -129,6 +146,8 @@ void setup()
server.on("/", handleRoot);
server.on("/config", []
{ iotWebConf.handleConfig(); });
server.on("/cleareeprom", clearEEPROM);
server.on("/reboot", reboot);
server.onNotFound([]()
{ iotWebConf.handleNotFound(); });

Expand Down Expand Up @@ -238,13 +257,83 @@ void displayValue()
{
oled.clear(PAGE);
oled.setCursor(0, 0);
oled.printf("Please\n\nConnect\n\nPZEM004T");
oled.printf("-Sensor-\n\nno sensor\ndetect!");
}

// display status
iotwebconf::NetworkState curr_state = iotWebConf.getState();
if (curr_state == iotwebconf::Boot)
{
prev_state = curr_state;
}
else if (curr_state == iotwebconf::NotConfigured)
{
if (prev_state == iotwebconf::Boot)
{
displaytime = 5;
prev_state = curr_state;
noti = "-State-\n\nno config\nstay in\nAP Mode";
}
}
else if (curr_state == iotwebconf::ApMode)
{
if (prev_state == iotwebconf::Boot)
{
displaytime = 5;
prev_state = curr_state;
noti = "-State-\n\nAP Mode\nfor 30 sec";
}
else if (prev_state == iotwebconf::Connecting)
{
displaytime = 5;
prev_state = curr_state;
noti = "-State-\n\nX can't\nconnect\nwifi\ngo AP Mode";
}
else if (prev_state == iotwebconf::OnLine)
{
displaytime = 10;
prev_state = curr_state;
noti = "-State-\n\nX wifi\ndisconnect\ngo AP Mode";
}
}
else if (curr_state == iotwebconf::Connecting)
{
if (prev_state == iotwebconf::ApMode)
{
displaytime = 5;
prev_state = curr_state;
noti = "-State-\n\nwifi\nconnecting";
}
else if (prev_state == iotwebconf::OnLine)
{
displaytime = 10;
prev_state = curr_state;
noti = "-State-\n\nX wifi\ndisconnect\nreconnecting";
}
}
else if (curr_state == iotwebconf::OnLine)
{
if (prev_state == iotwebconf::Connecting)
{
displaytime = 5;
prev_state = curr_state;
noti = "-State-\n\nwifi\nconnect\nsuccess\n" + String(WiFi.RSSI()) + " dBm";
}
}

if (displaytime)
{
displaytime--;
oled.clear(PAGE);
oled.setCursor(0, 0);
oled.print(noti);
Serial.println(noti);
}

// display state
if (iotWebConf.getState() == 1 || iotWebConf.getState() == 2)
if (curr_state == iotwebconf::NotConfigured || curr_state == iotwebconf::ApMode)
oled.drawIcon(55, 0, 9, 8, wifi_ap, sizeof(wifi_ap), true);
else if (iotWebConf.getState() == 3)
else if (curr_state == iotwebconf::Connecting)
{
if (t_connecting == 1)
{
Expand All @@ -256,14 +345,14 @@ void displayValue()
t_connecting = 1;
}
}
else if (iotWebConf.getState() == 4)
else if (curr_state == iotwebconf::OnLine)
{
if (iot.serverConnected)
oled.drawIcon(56, 0, 8, 8, wifi_on, sizeof(wifi_on), true);
else
oled.drawIcon(56, 0, 8, 8, wifi_nointernet, sizeof(wifi_nointernet), true);
}
else if (iotWebConf.getState() == 5)
else if (curr_state == iotwebconf::OffLine)
oled.drawIcon(56, 0, 8, 8, wifi_off, sizeof(wifi_off), true);

oled.display();
Expand Down Expand Up @@ -304,18 +393,26 @@ void handleRoot()
return;
}
String s = "<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"utf-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1, user-scalable=no\"/>";
s += "<title>Iotkiddie AC Powermeter config</title></head><body>IoTkiddie config data";
s += "<title>Iotkiddie AC Powermeter config</title>";
if (iotWebConf.getState() == iotwebconf::NotConfigured)
s += "<script>\nlocation.href='/config';\n</script>";
s += "</head><body>IoTkiddie config data";
s += "<ul>";
s += "<li>Device name : ";
s += String(iotWebConf.getThingName());
s += "<li>อีเมลล์ : ";
s += emailParamValue;
s += "<li>WIFI SSID : ";
s += String(iotWebConf.getSSID());
s += "<li>RSSI : ";
s += String(WiFi.RSSI()) + " dBm";
s += "<li>ESP ID : ";
s += ESP.getChipId();
s += "<li>Server : ";
s += serverParamValue;
s += "</ul>";
s += "Go to <a href='config'>configure page</a> to change values.";
s += "<button style='margin-top: 10px;' type='button' onclick=\"location.href='/reboot';\" >รีบูทอุปกรณ์</button><br><br>";
s += "<a href='config'>configure page</a> เพื่อแก้ไขข้อมูล wifi และ user";
s += "</body></html>\n";

server.send(200, "text/html", s);
Expand Down Expand Up @@ -360,4 +457,26 @@ bool formValidator(iotwebconf::WebRequestWrapper *webRequestWrapper)
}
*/
return valid;
}

void clearEEPROM()
{
EEPROM.begin(512);
// write a 0 to all 512 bytes of the EEPROM
for (int i = 0; i < 512; i++)
{
EEPROM.write(i, 0);
}

EEPROM.end();
server.send(200, "text/plain", "Clear all data\nrebooting");
delay(1000);
ESP.restart();
}

void reboot()
{
server.send(200, "text/plain", "rebooting");
delay(1000);
ESP.restart();
}
Binary file not shown.
79 changes: 79 additions & 0 deletions examples/02pmmeter/01pmmeter/01pmmeter.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
-PMS7003-
5V - VCC
GND - GND
D4 - TX
D3 - RX(not use in this code)
*/

#include <ESP8266WiFi.h>
#include <PMS.h>
#include <SoftwareSerial.h>
#include <iotbundle.h>

// 1 สร้าง object ชื่อ iot และกำหนดค่า(project)
#define PROJECT "PM_METER"
Iotbundle iot(PROJECT);

// 1.1.ใส่ข้อมูลไวไฟ
const char *ssid = "wifi_ssid";
const char *password = "wifi_pass";

// 1.2.ใส่ข้อมูล user ที่สมัครกับเว็บ iotkiddie.com
String email = "[email protected]";
String pass = "12345678";

SoftwareSerial pmsSerial(D4, D3); // RX,TX
PMS pms(pmsSerial);
PMS::DATA data;

#define PIN_RESET -1
#define DC_JUMPER 0

unsigned long previousMillis = 0, currentMillis = 0;

uint8_t logo_bmp[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xC0, 0xE0, 0xC0, 0xF0, 0xE0, 0x78, 0x38, 0x78, 0x3C, 0x1C, 0x3C, 0x1C, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1C, 0x3C, 0x1C, 0x3C, 0x78, 0x38, 0xF0, 0xE0, 0xF0, 0xC0, 0xC0, 0xC0, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x01, 0x00, 0x00, 0xF0, 0xF8, 0x70, 0x3C, 0x3C, 0x1C, 0x1E, 0x1E, 0x0E, 0x0E, 0x0E, 0x0F, 0x0F, 0x0E, 0x0E, 0x1E, 0x1E, 0x1E, 0x3C, 0x1C, 0x7C, 0x70, 0xF0, 0x70, 0x20, 0x01, 0x01, 0x03, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x1C, 0x3E, 0x1E, 0x0F, 0x0F, 0x07, 0x87, 0x87, 0x07, 0x0F, 0x0F, 0x1E, 0x3E, 0x1C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x1F, 0x1F, 0x3F, 0x3F, 0x1F, 0x1F, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

void setup()
{
Serial.begin(115200);
pmsSerial.begin(9600);

WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}

// 2 เริ่มเชื่อมต่อ หลังจากต่อไวไฟได้
iot.begin(email, pass);
}

void loop()
{
// 3 คอยจัดการ และส่งค่าให้เอง
iot.handle();

//------get data from PMS7003------
if (pms.read(data))
{
/* 4 เมื่อได้ค่าใหม่ ให้อัพเดทตามลำดับตามตัวอย่าง
ตัวไลบรารี่รวบรวมและหาค่าเฉลี่ยส่งขึ้นเว็บให้เอง
ถ้าค่าไหนไม่ต้องการส่งค่า ให้กำหนดค่าเป็น NAN */
iot.update(data.PM_AE_UG_1_0, data.PM_AE_UG_2_5, data.PM_AE_UG_10_0);

//------print on serial moniter------
Serial.print("PM 1.0 (ug/m3): ");
Serial.println(data.PM_AE_UG_1_0);
Serial.print("PM 2.5 (ug/m3): ");
Serial.println(data.PM_AE_UG_2_5);
Serial.print("PM 10.0 (ug/m3): ");
Serial.println(data.PM_AE_UG_10_0);

previousMillis = currentMillis;
}
}
Loading

0 comments on commit e67687b

Please sign in to comment.