Advertisement
metalx1000

ESP8266 Outlet 433mhz Radio Transmitter v2021

Mar 6th, 2021 (edited)
2,192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
  2. #include <ESP8266WebServer.h>
  3. #include <RCSwitch.h>
  4.  
  5. /*
  6.  * Copyright Kris Occhipinti March, 6th 2021
  7.  * https://filmsbykris.com
  8.  *This program is free software: you can redistribute it and/or modify                                
  9.  *it under the terms of the GNU General Public License as published by                                
  10.  *the Free Software Foundation, either version 3 of the License, or                                    
  11.  *(at your option) any later version.                                                                  
  12.  *                                                                                                    
  13.  *This program is distributed in the hope that it will be useful,                                      
  14.  *but WITHOUT ANY WARRANTY; without even the implied warranty of                                      
  15.  *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                        
  16.  *GNU General Public License for more details.                                                        
  17.  *                                                                                                    
  18.  *You should have received a copy of the GNU General Public License                                    
  19.  *along with this program.  If not, see <http://www.gnu.org/licenses/>.
  20.  *
  21.  */
  22.  
  23. RCSwitch mySwitch = RCSwitch();
  24.  
  25. ESP8266WebServer server(80);
  26.  
  27.  
  28. void setup() {
  29.     WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP
  30.  
  31.     // put your setup code here, to run once:
  32.     Serial.begin(115200);
  33.  
  34.     //transmit 0 --> pin D3 on Wemo Esp
  35.     mySwitch.enableTransmit(0);
  36.  
  37.     //This needs to be set to 180 for zap outlets
  38.     mySwitch.setPulseLength(180);
  39.    
  40.     // WiFi.mode(WiFi_STA); // it is a good practice to make sure your code sets wifi mode how you want it.
  41.  
  42.     //WiFiManager, Local intialization. Once its business is done, there is no need to keep it around
  43.     WiFiManager wm;
  44.  
  45.     //reset settings - wipe credentials for testing
  46.     //wm.resetSettings();
  47.  
  48.     // Automatically connect using saved credentials,
  49.     // if connection fails, it starts an access point with the specified name ( "AutoConnectAP"),
  50.     // if empty will auto generate SSID, if password is blank it will be anonymous AP (wm.autoConnect())
  51.     // then goes into a blocking loop awaiting configuration and will return success result
  52.  
  53.     bool res;
  54.     // res = wm.autoConnect(); // auto generated AP name from chipid
  55.     res = wm.autoConnect("Outlets"); // anonymous ap
  56.     //res = wm.autoConnect("AutoConnectAP","password"); // password protected ap
  57.  
  58.     if(!res) {
  59.         Serial.println("Failed to connect");
  60.         // ESP.restart();
  61.     }
  62.     else {
  63.         //if you get here you have connected to the WiFi    
  64.         Serial.println("connected...yeey :)");
  65.     }
  66.  
  67.   server.on("/", handleRoot);
  68.   server.begin();
  69. }
  70.  
  71.  
  72. void handleRoot() {
  73.   String msg = "";
  74.  
  75.   for (uint8_t i = 0; i < server.args(); i++) {
  76.     msg += server.argName(i) + ": " + server.arg(i) + "\n";
  77.   }
  78.  
  79.   //pulse seems to be either 180 or 320 for my devices
  80.   int pulse = server.arg("pulse").toInt();
  81.  
  82.   int code = server.arg("code").toInt();
  83.  
  84.   //bits 24 or 32
  85.   int bits = server.arg("bits").toInt();
  86.  
  87.   mySwitch.setPulseLength(pulse);
  88.   mySwitch.send(code, bits);
  89.  
  90.   server.send(200, "text/plain", msg);
  91.   Serial.println(msg);
  92. }
  93.  
  94. void loop() {
  95.     // put your main code here, to run repeatedly:
  96.     server.handleClient();
  97.  
  98.    
  99. }
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement