SHARE
TWEET

iotpres

a guest Nov 14th, 2018 102 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ESP8266WiFi.h>
  2. #include <PubSubClient.h>
  3.  
  4.  
  5. const char* ssid = "Connectify-esptest";
  6. const char* password = "esp12345678";
  7. const char* mqtt_server = "192.168.179.1";
  8. String MC = "SPM0001";
  9.  
  10. IPAddress ip(192, 168, 179, 31); // ip wemos
  11. IPAddress gateway(192, 168, 179, 1); // gateway
  12. IPAddress subnet(255, 255, 255, 0);
  13.  
  14.  
  15.    
  16. int sensorPin = A0;
  17. int sensorValue = 0;
  18.  
  19. unsigned long lastSend;
  20.  
  21. WiFiClient espClient;
  22. PubSubClient client(espClient);
  23.  
  24. char buffer[100] = {0};
  25. char message_buff[100];
  26. String msgString = "";
  27.  
  28.  
  29. void setup()
  30. {
  31.  
  32.   Serial.begin(9600);
  33.   pinMode(D1, INPUT_PULLUP);
  34.   client.setServer(mqtt_server, 1883);
  35.   client.setCallback(callback);
  36.   setup_wifi();
  37.   String pres = pressure();
  38.   reconnect_mqtt(pres);
  39.  
  40. }
  41.  
  42. void setup_wifi() {
  43.  
  44.   delay(10);
  45.   Serial.printf("Connecting to %s ", ssid);
  46.   WiFi.begin(ssid, password);
  47.   WiFi.config(ip, gateway, subnet);
  48.  
  49.   while (WiFi.status() != WL_CONNECTED)
  50.   {
  51.     Serial.print(".");
  52.     delay(5000);
  53.   }
  54.  
  55.   if (WiFi.status() == WL_CONNECTED)
  56.   {
  57.     Serial.println("connect");
  58.     randomSeed(micros());
  59.     Serial.println(WiFi.localIP());
  60.     Serial.println("connect");
  61.   }
  62.  
  63. }
  64.  
  65. void loop()
  66. {
  67.    
  68.   if ( millis() - lastSend > 0 )
  69.   {
  70.    
  71.    if(digitalRead(D1) == LOW){
  72.     Serial.println("startflag");
  73.     startflag();
  74.     Serial.println("delay");
  75.     delay(15000);
  76.    
  77.     String pres = pressure();
  78.     if (pres != ""){
  79.      publish_mqtt("vp", pres);
  80.     }
  81.     Serial.println("kirim mqtt");
  82.     lastSend = millis();
  83.  
  84.     if(digitalRead(D1)==HIGH){
  85.       Serial.println("stopflag");
  86.       stopflag();
  87.    }
  88.    }
  89.   }
  90.  
  91.   if(millis() - lastSend > 10000){
  92.     send_mqtt();
  93.     lastSend = millis();
  94.   }
  95.  
  96.   client.loop();
  97.   if (!client.connected())
  98.   {
  99.     String pres = pressure();
  100.     reconnect_mqtt(pres);
  101.  
  102.   }
  103.  
  104.   if (WiFi.status() != WL_CONNECTED)
  105.   {
  106.     setup_wifi();
  107.   }
  108. }
  109.  
  110.  
  111.  
  112. void callback(char* topic, byte* payload, unsigned int length)
  113. {
  114.  
  115.   int i = 0;
  116.   for (i = 0; i < length; i++)
  117.   {
  118.     message_buff[i] = payload[i];
  119.   }
  120.   message_buff[i] = '\0';
  121.   msgString = String(message_buff);
  122.   Serial.println(msgString);
  123.  
  124. }
  125.  
  126.  
  127. void reconnect_mqtt(String Sbar)
  128. {
  129.  
  130.   if (client.connect(MC.c_str())){
  131.    
  132.    if(Sbar != ""){
  133.     send_publish_recon("vp", Sbar);
  134.    }
  135.      
  136.   Serial.println("dari starup");
  137.    
  138.   }
  139. }
  140.  
  141.  
  142.  
  143. void publish_mqtt (String property, String value)
  144. {
  145.   String json_strg = "{\"type\":\"SPM\",\"id\":\"M0001\",\"" + property + "\":"+ value + "}";
  146.   char *msg_mqtt = const_cast<char*>(json_strg.c_str());
  147.   client.publish("IOT/C/SPM/OUTPUT", msg_mqtt);
  148. }
  149.  
  150. void send_publish_recon (String property, String value)
  151. {
  152.   String json_strg = "{\"type\":\"SPM\",\"id\":\"M0001\",\"" + property + "\":"+ value + "}";
  153.   char *msg_mqtt = const_cast<char*>(json_strg.c_str());
  154.   client.publish("IOT/C/SPM/OUTPUT", msg_mqtt);
  155. }
  156.  
  157. void startflag(){
  158.    String json_strg = "{\"type\":\"SPM\",\"id\":\"M0001\",\"FLAG\":\"START\"}";
  159.  char *stat = const_cast<char*>(json_strg.c_str());
  160.   client.publish("IOT/C/SPM0001/FLAG", stat);
  161. }
  162.  
  163. void stopflag(){
  164.    String json_strg = "{\"type\":\"SPM\",\"id\":\"M0001\",\"FLAG\":\"STOP\"}";
  165.  char *stat = const_cast<char*>(json_strg.c_str());
  166.   client.publish("IOT/C/SPM0001/FLAG", stat);
  167. }
  168.  
  169. void send_mqtt (){
  170. //  if(millis()- 0 > 15000){
  171.   String json_strg = "{\"type\":\"SPM\",\"id\":\"M0001\",\"status\":\"OK\"}";
  172.  char *stat = const_cast<char*>(json_strg.c_str());
  173.   client.publish("IOT/C/SPM0001/STATUS", stat);
  174. //  }
  175. }
  176.  
  177.  
  178. String pressure()
  179. {
  180.  
  181.   sensorValue = analogRead(sensorPin);
  182.   float v = sensorValue * (5000 / 1023.0);
  183.   float val = map(sensorValue, 290, 478, 0, 160);
  184.   Serial.println(val);
  185.  
  186.   if (digitalRead(D1) == LOW)
  187.   {
  188.     float bar = (val / 100.0);
  189.     Serial.println(bar);
  190.     String Sbar = String (bar);
  191.     return Sbar;
  192.   }
  193.   return "";
  194.  }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top