Advertisement
AngyalRobert

wifiScale

Dec 14th, 2022
563
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.25 KB | Source Code | 0 0
  1. /*
  2.  *  This sketch sends data via HTTP GET requests to data.sparkfun.com service.
  3.  *
  4.  *  You need to get streamId and privateKey at data.sparkfun.com and paste them
  5.  *  below. Or just customize this script to talk to other HTTP servers.
  6.  *
  7.  */
  8.  
  9. extern "C" {
  10. #include "user_interface.h"
  11. }
  12. #include <ESP8266WiFi.h>
  13. #include <WiFiUDP.h>
  14. #include "nau7802.h"
  15.  
  16. IPAddress ip(192, 168, 57, 5);
  17. IPAddress gateway(192, 168, 57, 1);  
  18. IPAddress subnet(255, 255, 255, 0); //- See more at: http://www.esp8266.com/viewtopic.php?f=32&t=2773#sthash.SgPBmkmu.dpuf
  19.  
  20. const char* ssid     = "WifiScale";
  21. const char* password = "W171Scale";
  22. boolean wifiConnected = false;
  23. boolean enable_ontr_test= true;
  24. unsigned int localPort = 1001;
  25. WiFiUDP UDP;
  26. boolean udpConnected = false;
  27. unsigned int ConnectTimeOut= 90;  // ha 15 percig nincs kapcsolat, kikapcsol
  28.  
  29. char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet
  30. char ReplyBuffer[] = "acknowledged";
  31. // a string to send back
  32.  
  33. os_timer_t myTimer;
  34.  
  35. #define ONSW  13
  36. #define LED   14
  37. #define ONTR  15
  38. #define NAU   12
  39.  
  40. char blink_timer; // számláló a villogás időzítéséhez
  41. unsigned char kikapcs;     // számláló hogy meddig tartják nyomva a bekapcs gombot.
  42. boolean KIKAPCS= false;  // ha true, akkor kikapcs ütenet jött a pc-től
  43.  
  44. float Vacc;       // akkumulátor feszültség
  45. unsigned char need_read_accu= 0;
  46.  
  47. unsigned char AD_Cntr;  
  48. unsigned long AD_AVG;
  49. unsigned int NAU_timeout_cntr;
  50. unsigned char NAU_timeout_error;
  51. unsigned long ADResult;
  52. unsigned long AD_Table[5];
  53. unsigned char AD_ptr= 0;
  54.  
  55. unsigned char checkWifi= 50;
  56. unsigned char wifiTimeOutCntr= 10;
  57.  
  58. // start of timerCallback, 100ms-onként belép ide
  59. void timerCallback(void *pArg) {
  60.  
  61.    
  62.     // Kikapcsolja a bekapcsolva tartó tranzisztort.
  63.     if (enable_ontr_test)
  64.       digitalWrite( ONTR, LOW);
  65.     // a delay-t nem lehet itt használni, ezért így vár egy kicsit
  66.    
  67.     if (need_read_accu) need_read_accu--;
  68.     if (checkWifi) checkWifi--;
  69.  
  70.     // villogtatja a LED-et
  71.     blink_timer++;
  72.     if (blink_timer<=1){
  73.        if (KIKAPCS==0) digitalWrite(LED, HIGH);
  74.     }else{
  75.        digitalWrite(LED, LOW);
  76.        if (blink_timer>=6){
  77.         blink_timer= 0;
  78.        }
  79.     }
  80.  
  81.     // megnézi hogy nyomvatartják e a begombot, kikapcsoláshoz.
  82. /*    char s[10];
  83.     itoa( kikapcs, s, 10);
  84.     Serial.print( s);
  85.     Serial.print( "  onsw ");
  86.     itoa( digitalRead( ONSW), s, 10);
  87.     Serial.println( s);
  88. */
  89.     if (enable_ontr_test){
  90.       if (digitalRead( ONSW)== LOW || Vacc<11 || KIKAPCS) {
  91.           if (kikapcs>10){
  92.             // várja hogy elengedjék a begombot.
  93.             KIKAPCS= true;
  94.           }
  95.           else{
  96.             kikapcs++;
  97.             digitalWrite( ONTR, HIGH);
  98.           }
  99.       }
  100.       else {
  101.           digitalWrite( ONTR, HIGH);
  102.           kikapcs= 0;
  103.       }
  104.     }
  105.  
  106.     NAU_timeout_cntr--;
  107.     if (NAU_timeout_cntr==0){
  108.       NAU_timeout_error= 1;
  109.     }
  110.  
  111. } // End of timerCallback
  112.  
  113.  
  114. // connect to UDP – returns true if successful or false if not
  115. boolean connectUDP()
  116. {
  117.   boolean state = false;
  118.  
  119.   Serial.println("");
  120.   Serial.println("Connecting to UDP");
  121.  
  122.   if (UDP.begin(localPort) == 1){
  123.     Serial.println("Connection successful");
  124.     state = true;
  125.   }
  126.   else{
  127.     Serial.println("Connection failed");
  128.   }
  129.   return state;
  130. }
  131.  
  132. // connect to wifi – returns true if successful or false if not
  133. boolean connectWifi()
  134. {
  135.   boolean state = true;
  136.   int i = 0;
  137.   enable_ontr_test= false;
  138.   WiFi.config(ip, gateway, subnet);
  139.   WiFi.mode(WIFI_STA);
  140.   WiFi.begin(ssid, password);
  141.  
  142.   // Wait for connection
  143.   Serial.print("Connecting");
  144.   while (WiFi.status() != WL_CONNECTED) {
  145.     delay(200);
  146.     Serial.print(".");
  147.     if (i > 25){
  148.       state = false;
  149.       break;
  150.     }
  151.     i++;
  152.   }
  153.   if (state){
  154.     Serial.println("");
  155.     Serial.print("Connected to ");
  156.     Serial.println(ssid);
  157.     Serial.print("IP address: ");
  158.     Serial.println(WiFi.localIP());
  159.   }
  160.   else {
  161.     Serial.println("");
  162.     Serial.println("Connection failed.");
  163.     WiFi.disconnect();
  164.   }
  165.   enable_ontr_test= true;
  166.   return state;
  167. }
  168.  
  169.  
  170. // receive udp messages
  171. void udpReceive( void)
  172. {
  173.   if(wifiConnected && udpConnected){
  174.       // if there’s data available, read a packet
  175.       int packetSize = UDP.parsePacket();
  176.       if(packetSize) {
  177.         // read the packet into packetBufffer
  178.         UDP.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
  179.         Serial.print("Receive:");
  180.         Serial.println(packetBuffer);
  181.         if (strcmp(packetBuffer, "KI")==0){
  182.           kikapcs= 121;
  183.           KIKAPCS= true;
  184.         }
  185.         wifiTimeOutCntr= 10;
  186.       }
  187.   }
  188. }
  189.  
  190. /********************************************************************
  191.  *  SETUP
  192.  ********************************************************************/
  193. void setup() {  
  194.   // bekapcsolja a LED-et
  195.   pinMode(LED, OUTPUT);
  196.   digitalWrite( LED, HIGH);
  197.  
  198.   // bekapcsolja a bekapcsolva tartó tranzisztort.
  199.   pinMode(ONTR, OUTPUT);
  200.   digitalWrite( ONTR, HIGH);
  201.   pinMode(ONSW, INPUT);
  202.  
  203.   // bekapcsolja a mérleget
  204.   pinMode(NAU, OUTPUT);
  205.   digitalWrite( NAU, LOW);
  206.  
  207.   // timer interrupt konfugurálása
  208.   os_timer_setfn(&myTimer, timerCallback, NULL);
  209.   os_timer_arm(&myTimer, 100, true);
  210.  
  211.   // soros port beállítása
  212.   Serial.begin(115200);
  213.   delay(10);
  214.  
  215.   // We start by connecting to a WiFi network
  216.  
  217.   Serial.println();
  218.   Serial.println();
  219.   Serial.print("Connecting to ");
  220.   Serial.println(ssid);
  221.  
  222.   wifiConnected= connectWifi();
  223.    
  224.   if (wifiConnected){
  225.       udpConnected = connectUDP();
  226.   }
  227.  
  228.   InitNAU7802();
  229.   AD_AVG= 0;
  230.   AD_Cntr= 0;
  231.   NAU_timeout_error=0;
  232.   NAU_timeout_cntr= 20;
  233. }
  234.  
  235. /********************************************************************
  236.  * LOOP
  237.  ********************************************************************/
  238. void loop() {
  239.  
  240.   delay( 1);
  241.  
  242.   if (need_read_accu==0){
  243.     float V= analogRead(A0);
  244.     V*= 16.6;
  245.     V/=1024;
  246.     Vacc= V;
  247.     //Serial.print( "Accu: ");
  248.     //Serial.print(Vacc);
  249.     //Serial.println( " V");
  250.     need_read_accu= 20;
  251.   }
  252.  
  253.  
  254.   if (AD_Ready())
  255.   {
  256.     unsigned long AD= GetADCResult_NAU7802();
  257.  
  258.     AD>>=4;
  259.     AD_AVG+= AD;
  260.     AD_Cntr++;
  261.  
  262.     //Serial.print( " AD: ");
  263.     //Serial.print(  AD);
  264.    
  265.     if (AD_Cntr>=5) {
  266.      
  267.       AD_AVG/=AD_Cntr;
  268.       //Serial.print( " AVG: ");
  269.       //Serial.println(  AD_AVG);
  270.       AD_AVG>>=1;
  271.       if (AD_AVG & 1) AD_AVG+=1;
  272.       AD_AVG>>=1;
  273.      
  274.       AD_Table[AD_ptr++]= AD_AVG;
  275.       if (AD_ptr>4) AD_ptr= 0;
  276.      
  277.       ADResult= 0;
  278.       for (byte i=0; i<5; i++)
  279.         ADResult+= AD_Table[i];
  280.       ADResult/=5;  
  281.      
  282.       AD_AVG= 0;
  283.       AD_Cntr= 0;
  284.  
  285.       if (wifiConnected && udpConnected){
  286.         UDP.beginPacket("192.168.23.1", 1001);
  287.         dtostrf( ADResult, 8, 0, ReplyBuffer);
  288.         char s[10];
  289.         dtostrf( Vacc, 8, 2, s);
  290.         strcat( ReplyBuffer, s);
  291.         enable_ontr_test= false;
  292.         Serial.println(ReplyBuffer);
  293.         UDP.write(ReplyBuffer);
  294.         if (UDP.endPacket()) wifiTimeOutCntr= 10;
  295.         enable_ontr_test= true;
  296.       }
  297.     }
  298.     NAU_timeout_cntr= 20;
  299.   }
  300.  
  301.   if (NAU_timeout_error==1)
  302.   { InitNAU7802();
  303.     NAU_timeout_error=0;
  304.     NAU_timeout_cntr= 20;
  305.     Serial.println( "NAU TIME OUT");
  306.   }
  307.  
  308.   udpReceive();
  309.  
  310.   if (checkWifi==0) {
  311.     checkWifi= 10;
  312.     wifiTimeOutCntr--;
  313.     if (wifiTimeOutCntr==0){
  314.       Serial.println( "WIFI TIME OUT");
  315.       wifiTimeOutCntr= 10;
  316.       UDP.stop();
  317.       WiFi.disconnect();
  318.       delay( 1000);
  319.       wifiConnected= connectWifi();
  320.       if (wifiConnected) udpConnected = connectUDP();
  321.  
  322.       if (wifiConnected==0){
  323.         if (ConnectTimeOut) {
  324.           ConnectTimeOut--;
  325.           if (ConnectTimeOut==0) KIKAPCS=true;
  326.         }
  327.        
  328.       }
  329.       else
  330.       {
  331.         ConnectTimeOut= 60; // kikapcsolás 10 perc múlva.
  332.       }
  333.     }
  334.   }
  335.  
  336.   if (KIKAPCS)
  337.   {
  338.     Serial.println( "Kikapcsol");
  339.     Serial.print("Vacc:");
  340.     Serial.println(Vacc);
  341.     Serial.print("cntr:");
  342.     char s[10];
  343.     itoa( kikapcs, s, 10);
  344.     Serial.println(s);
  345.     if (ConnectTimeOut==0) Serial.println("Connection Time Out");
  346.     delay(100);
  347.     digitalWrite( ONTR, LOW);
  348.     delay( 10000);
  349.   }
  350. }
  351.  
  352.  
  353.  
  354.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement