Node mcu not function correctly

Rohith1221
Posts: 1
Joined: Fri Apr 30, 2021 8:24 pm

Node mcu not function correctly

Postby Rohith1221 » Fri Apr 30, 2021 8:34 pm

I have this small project of controlling relays with an rf remote as well as a IoT platform Blynk. So I tested an rf remote and relays and put together a code for my project which when uploaded is not connecting to my wifi or giving the correct output on the serial monitor
  1. THE CODE:
  2.  
  3. #include <ESP8266WiFi.h>
  4. #include <BlynkSimpleEsp8266.h>
  5. #include <RCSwitch.h>
  6. BlynkTimer clocktime;
  7.  
  8.  
  9. #define relayfan 5
  10. #define relaylight 6
  11. #define relaylightb 7
  12. #define relayrandom 2
  13.  
  14. #define VPIN_BUTTON_1    V12
  15. #define VPIN_BUTTON_2    V13
  16. #define VPIN_BUTTON_3    V14
  17. #define VPIN_BUTTON_4    V15
  18.  
  19. RCSwitch mySwitch = RCSwitch();
  20.  
  21. int relayfanstate = HIGH;
  22. int relaylightstate = HIGH;
  23. int relaylightbstate = HIGH;
  24. int relayrandomstate = HIGH;
  25. int mainstate = HIGH;
  26. long code;
  27.  
  28.  
  29. char auth[] = "rJUG7fhgXk9Y7OlowTontA1C2irfpgs2";
  30.  
  31. const char *ssid =  "Nag";    
  32. const char *pass =  "87654321";
  33.  
  34. BLYNK_CONNECTED()
  35. {
  36.   Serial.println("blynk connected first function");
  37.   Blynk.syncVirtual(VPIN_BUTTON_1);
  38.   Blynk.syncVirtual(VPIN_BUTTON_2);
  39.   Blynk.syncVirtual(VPIN_BUTTON_3);
  40.   Blynk.syncVirtual(VPIN_BUTTON_4);
  41. }
  42.  
  43. BLYNK_WRITE(VPIN_BUTTON_1)
  44. {
  45.   Serial.println("blynk write");
  46.   relayfanstate = param.asInt();
  47.   digitalWrite(relayfan, relayfanstate);
  48. }
  49.  
  50. BLYNK_WRITE(VPIN_BUTTON_2)
  51. {
  52.   Serial.println("blynk write");
  53.   relaylightstate = param.asInt();
  54.   digitalWrite(relaylight, relaylightstate);
  55. }
  56.  
  57. BLYNK_WRITE(VPIN_BUTTON_3)
  58. {
  59.   Serial.println("blynk write");
  60.   relaylightbstate = param.asInt();
  61.   digitalWrite(relaylightb, relaylightbstate);
  62. }
  63.  
  64. BLYNK_WRITE(VPIN_BUTTON_4)
  65. {
  66.   Serial.println("blynk write");
  67.   relayrandomstate = param.asInt();
  68.   digitalWrite(relayrandom, relayrandomstate);
  69. }
  70.  
  71.  
  72. void setup()
  73. {
  74.   Serial.begin(9600);
  75.   mySwitch.enableReceive(0);
  76.   pinMode(relayfan, OUTPUT);
  77.   pinMode(relaylight, OUTPUT);
  78.   pinMode(relaylightb, OUTPUT);
  79.   pinMode(relayrandom, OUTPUT);
  80.   digitalWrite(relayfan, HIGH);
  81.   digitalWrite(relaylight, HIGH);
  82.   digitalWrite(relaylightb, HIGH);
  83.   digitalWrite(relayrandom, HIGH);
  84.   Serial.println("Start");
  85.   WiFi.begin(ssid, pass);
  86.   Serial.println("wifi config done");
  87.   Blynk.config(auth);//, ssid, pass);
  88.   Serial.println("Blynk config done");
  89. }
  90.  
  91.  
  92. void loop()
  93. {
  94.  
  95.   if (WiFi.status() != WL_CONNECTED)
  96.   {
  97.     Serial.println("Not Connected");
  98.     Serial.println("Without internet rf code recieving started");
  99.     delay(500);
  100.     if (mySwitch.available())
  101.     {
  102.       Serial.println("Switch available");
  103.       code = mySwitch.getReceivedValue();
  104.       if (mySwitch.getReceivedValue() == 8594433)
  105.       {
  106.         Serial.println("code matched without internet");
  107.         mainstate = !mainstate;
  108.         digitalWrite(relayfan, mainstate);
  109.         digitalWrite(relaylight, mainstate);
  110.         digitalWrite(relaylightb, mainstate);
  111.         digitalWrite(relayrandom, mainstate);
  112.         delay(1000);
  113.         mySwitch.resetAvailable();
  114.       }
  115.       else if(mySwitch.getReceivedValue() == 8594447)
  116.       {
  117.         digitalWrite(relayfan, LOW);
  118.         delay(1000);
  119.         mySwitch.resetAvailable();
  120.       }
  121.       else if(mySwitch.getReceivedValue() == 8594445)
  122.       {
  123.         digitalWrite(relayfan, HIGH);
  124.         delay(1000);
  125.         mySwitch.resetAvailable();
  126.       }
  127.       else if(mySwitch.getReceivedValue() == 8594436)
  128.       {
  129.         relaylightstate = !relaylightstate;
  130.         digitalWrite(relaylight, relaylightstate);
  131.         delay(1000);
  132.         mySwitch.resetAvailable();
  133.       }
  134.       else if(mySwitch.getReceivedValue() == 8594437)
  135.       {
  136.         relaylightbstate = !relaylightbstate;
  137.         digitalWrite(relaylightb, relaylightbstate);
  138.         delay(1000);
  139.         mySwitch.resetAvailable();
  140.       }
  141.       else if(mySwitch.getReceivedValue() == 8594439)
  142.       {
  143.         relayrandomstate = !relayrandomstate;
  144.         digitalWrite(relayrandom, relayrandomstate);
  145.         delay(1000);
  146.         mySwitch.resetAvailable();
  147.       }
  148.     }
  149.   }
  150.   else
  151.   {
  152.     Serial.println(" Connected");
  153.     Blynk.run();
  154.     with_internet();
  155.   }
  156.   clocktime.run();
  157. }
  158.  
  159. void with_internet()
  160. {
  161.   Serial.println("With internet rf code recieving started");
  162.   delay(500);
  163.   if (mySwitch.available())
  164.   {
  165.     Serial.println("Switch available");
  166.     code = mySwitch.getReceivedValue();
  167.     if (mySwitch.getReceivedValue() == 8594433)
  168.       {
  169.         Serial.println("code matched without internet");
  170.         mainstate = !mainstate;
  171.         digitalWrite(relayfan, mainstate);
  172.         digitalWrite(relaylight, mainstate);
  173.         digitalWrite(relaylightb, mainstate);
  174.         digitalWrite(relayrandom, mainstate);
  175.         Blynk.virtualWrite(VPIN_BUTTON_1, mainstate);
  176.         Blynk.virtualWrite(VPIN_BUTTON_2, mainstate);
  177.         Blynk.virtualWrite(VPIN_BUTTON_3, mainstate);
  178.         Blynk.virtualWrite(VPIN_BUTTON_4, mainstate);
  179.         delay(1000);
  180.         mySwitch.resetAvailable();
  181.       }
  182.       else if(mySwitch.getReceivedValue() == 8594447)
  183.       {
  184.         digitalWrite(relayfan, LOW);
  185.         Blynk.virtualWrite(VPIN_BUTTON_1, LOW);
  186.         delay(1000);
  187.         mySwitch.resetAvailable();
  188.       }
  189.       else if(mySwitch.getReceivedValue() == 8594445)
  190.       {
  191.         digitalWrite(relayfan, HIGH);
  192.         Blynk.virtualWrite(VPIN_BUTTON_1, HIGH);
  193.         delay(1000);
  194.         mySwitch.resetAvailable();
  195.       }
  196.       else if(mySwitch.getReceivedValue() == 8594436)
  197.       {
  198.         relaylightstate = !relaylightstate;
  199.         digitalWrite(relaylight, relaylightstate);
  200.         Blynk.virtualWrite(VPIN_BUTTON_2, relaylightstate);
  201.         delay(1000);
  202.         mySwitch.resetAvailable();
  203.       }
  204.       else if(mySwitch.getReceivedValue() == 8594437)
  205.       {
  206.         relaylightbstate = !relaylightbstate;
  207.         digitalWrite(relaylightb, relaylightbstate);
  208.         Blynk.virtualWrite(VPIN_BUTTON_3, relaylightbstate);
  209.         delay(1000);
  210.         mySwitch.resetAvailable();
  211.       }
  212.       else if(mySwitch.getReceivedValue() == 8594439)
  213.       {
  214.         relayrandomstate = !relayrandomstate;
  215.         digitalWrite(relayrandom, relayrandomstate);
  216.         Blynk.virtualWrite(VPIN_BUTTON_4, relayrandomstate);
  217.         delay(1000);
  218.         mySwitch.resetAvailable();
  219.       }
  220.   }
  221. }
The output:
⸮L⸮⸮⸮ՋR(⸮⸮^⸮̓#⸮⸮;'⸮⸮(Zs"⸮⸮Z|⸮⸮⸮'⸮⸮⸮'⸮⸮⸮!#⸮⸮⸮G⸮⸮⸮⸮⸮h⸮y⸮⸮⸮⸮'⸮⸮LD⸮⸮J⸮*J⸮⸮⸮⸮⸮⸮⸮LD⸮⸮⸮⸮F(⸮y⸮⸮؁'1⸮L⸮⸮⸮

This only specific to this code maybe something wrong in my code but

When I upload a default basic scetch from the Blynk website
  1. #define BLYNK_PRINT Serial
  2.  
  3.  
  4. #include <ESP8266WiFi.h>
  5. #include <BlynkSimpleEsp8266.h>
  6.  
  7. // You should get Auth Token in the Blynk App.
  8. // Go to the Project Settings (nut icon).
  9. char auth[] = "YourAuthToken";
  10.  
  11. // Your WiFi credentials.
  12. // Set password to "" for open networks.
  13. char ssid[] = "YourNetworkName";
  14. char pass[] = "YourPassword";
  15.  
  16. void setup()
  17. {
  18.   // Debug console
  19.   Serial.begin(9600);
  20.  
  21.   Blynk.begin(auth, ssid, pass);
  22.   // You can also specify server:
  23.   //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  24.   //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
  25. }
  26.  
  27. void loop()
  28. {
  29.   Blynk.run();
  30.   // You can inject your own code or combine it with other sketches.
  31.   // Check other examples on how to communicate with Blynk. Remember
  32.   // to avoid delay() function!
  33. }
Thus code works fine and is able to connect to the internet and the IoT app idk what's the problem can anyone tell me why is the code not working?

From my little research this is something called blocking code which prevents nodemcu from running it's services maybe I'm wron someone please correct me .

ESP_Sprite
Posts: 8921
Joined: Thu Nov 26, 2015 4:08 am

Re: Node mcu not function correctly

Postby ESP_Sprite » Thu May 06, 2021 8:38 am

Note that this is an ESP32 forum and you're posting about an 8266. That being said: you're using GPIO pins 6 and 7, which already are in use by the flash; using those GPIOs for something else will result in what you're seeing. (This goes for any GPIOs in the 6-11 range; generally it's a good idea not to use them for anything.)

Who is online

Users browsing this forum: No registered users and 63 guests