Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <PubSubClient.h>
- const char* ssid = "Connectify-esptest";
- const char* password = "esp12345678";
- const char* mqtt_server = "192.168.179.1";
- String MC = "SPM0001";
- IPAddress ip(192, 168, 179, 31); // ip wemos
- IPAddress gateway(192, 168, 179, 1); // gateway
- IPAddress subnet(255, 255, 255, 0);
- int sensorPin = A0;
- int sensorValue = 0;
- unsigned long lastSend;
- WiFiClient espClient;
- PubSubClient client(espClient);
- char buffer[100] = {0};
- char message_buff[100];
- String msgString = "";
- void setup()
- {
- Serial.begin(9600);
- pinMode(D1, INPUT_PULLUP);
- client.setServer(mqtt_server, 1883);
- client.setCallback(callback);
- setup_wifi();
- String pres = pressure();
- reconnect_mqtt(pres);
- }
- void setup_wifi() {
- delay(10);
- Serial.printf("Connecting to %s ", ssid);
- WiFi.begin(ssid, password);
- WiFi.config(ip, gateway, subnet);
- while (WiFi.status() != WL_CONNECTED)
- {
- Serial.print(".");
- delay(5000);
- }
- if (WiFi.status() == WL_CONNECTED)
- {
- Serial.println("connect");
- randomSeed(micros());
- Serial.println(WiFi.localIP());
- Serial.println("connect");
- }
- }
- void loop()
- {
- if ( millis() - lastSend > 0 )
- {
- if(digitalRead(D1) == LOW){
- Serial.println("startflag");
- startflag();
- Serial.println("delay");
- delay(15000);
- String pres = pressure();
- if (pres != ""){
- publish_mqtt("vp", pres);
- }
- Serial.println("kirim mqtt");
- lastSend = millis();
- if(digitalRead(D1)==HIGH){
- Serial.println("stopflag");
- stopflag();
- }
- }
- }
- if(millis() - lastSend > 10000){
- send_mqtt();
- lastSend = millis();
- }
- client.loop();
- if (!client.connected())
- {
- String pres = pressure();
- reconnect_mqtt(pres);
- }
- if (WiFi.status() != WL_CONNECTED)
- {
- setup_wifi();
- }
- }
- void callback(char* topic, byte* payload, unsigned int length)
- {
- int i = 0;
- for (i = 0; i < length; i++)
- {
- message_buff[i] = payload[i];
- }
- message_buff[i] = '\0';
- msgString = String(message_buff);
- Serial.println(msgString);
- }
- void reconnect_mqtt(String Sbar)
- {
- if (client.connect(MC.c_str())){
- if(Sbar != ""){
- send_publish_recon("vp", Sbar);
- }
- Serial.println("dari starup");
- }
- }
- void publish_mqtt (String property, String value)
- {
- String json_strg = "{\"type\":\"SPM\",\"id\":\"M0001\",\"" + property + "\":"+ value + "}";
- char *msg_mqtt = const_cast<char*>(json_strg.c_str());
- client.publish("IOT/C/SPM/OUTPUT", msg_mqtt);
- }
- void send_publish_recon (String property, String value)
- {
- String json_strg = "{\"type\":\"SPM\",\"id\":\"M0001\",\"" + property + "\":"+ value + "}";
- char *msg_mqtt = const_cast<char*>(json_strg.c_str());
- client.publish("IOT/C/SPM/OUTPUT", msg_mqtt);
- }
- void startflag(){
- String json_strg = "{\"type\":\"SPM\",\"id\":\"M0001\",\"FLAG\":\"START\"}";
- char *stat = const_cast<char*>(json_strg.c_str());
- client.publish("IOT/C/SPM0001/FLAG", stat);
- }
- void stopflag(){
- String json_strg = "{\"type\":\"SPM\",\"id\":\"M0001\",\"FLAG\":\"STOP\"}";
- char *stat = const_cast<char*>(json_strg.c_str());
- client.publish("IOT/C/SPM0001/FLAG", stat);
- }
- void send_mqtt (){
- // if(millis()- 0 > 15000){
- String json_strg = "{\"type\":\"SPM\",\"id\":\"M0001\",\"status\":\"OK\"}";
- char *stat = const_cast<char*>(json_strg.c_str());
- client.publish("IOT/C/SPM0001/STATUS", stat);
- // }
- }
- String pressure()
- {
- sensorValue = analogRead(sensorPin);
- float v = sensorValue * (5000 / 1023.0);
- float val = map(sensorValue, 290, 478, 0, 160);
- Serial.println(val);
- if (digitalRead(D1) == LOW)
- {
- float bar = (val / 100.0);
- Serial.println(bar);
- String Sbar = String (bar);
- return Sbar;
- }
- return "";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement