// // youtube --> https://www.youtube.com/watch?v=x0txsOUhfnQ // // tcp server side for orange pi zero 3 // turn on led when LEDON message is received // turn off led when LEDOF message is received // uses gpiod library // sudo apt-get install -y libgpiod-dev // compiling... sudo gcc this_program.c -lgpiod // assumes led is connected to pc10 of orange pi zero 3 // pc10 is gpio 74, in orange pi zero 3 // do sudo gpio readall to find 74 next to PC10 // #include #include #include #include #include #include #define PORT 8080 #ifndef CONSUMER #define CONSUMER "Consumer" #endif int main(int argc, char const* argv[]){ int server_fd, new_socket; ssize_t valread; struct sockaddr_in address; int opt = 1; socklen_t addrlen = sizeof(address); char buffer[10] = {0}; char *chipname = "gpiochip0"; unsigned int line_num = 74; unsigned int val; struct gpiod_chip *chip; struct gpiod_line *line; int i, ret; chip=gpiod_chip_open_by_name(chipname); line=gpiod_chip_get_line(chip, line_num); ret=gpiod_line_request_output(line, CONSUMER, 0); val=0; // create socket file descriptor if((server_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0){ perror("socket failed"); exit(EXIT_FAILURE);} // forcefully attaching socket to port 8080 if(setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))){ perror("setsockopt"); exit(EXIT_FAILURE);} address.sin_family=AF_INET; address.sin_addr.s_addr=INADDR_ANY; address.sin_port=htons(PORT); // forcefully attaching socket to the port 8080 if(bind(server_fd, (struct sockaddr*)&address, sizeof(address)) < 0){ perror("bind failed"); exit(EXIT_FAILURE);} if(listen(server_fd, 3) < 0){ perror("listen"); exit(EXIT_FAILURE);} if((new_socket = accept(server_fd, (struct sockaddr*)&address, &addrlen)) < 0){ perror("accept"); exit(EXIT_FAILURE);} while(1){ valread=read(new_socket, buffer, 10 - 1); // subtract 1 for null terminator at the end if(strncmp(buffer, "LEDON", 5) == 0) gpiod_line_set_value(line, val); else if(strncmp(buffer, "LEDOF", 5) == 0) gpiod_line_set_value(line, !val);} close(new_socket); close(server_fd); return 0;} // // flutter app to blink led on orange pi zero 3 // // // tcp client in flutter // import 'package:flutter/material.dart'; import 'dart:io'; import 'dart:async'; late Socket s; TextStyle myStyle = TextStyle(fontWeight:FontWeight.bold, fontSize:24); void main() {runApp(MaterialApp(home:MainScreen(), theme:ThemeData(scaffoldBackgroundColor:Colors.teal[100])));} class MainScreen extends StatelessWidget{ void send(val) async {s.writeln(val);} void connect() async { print('connecting...'); s=await Socket.connect('192.168.6.106',8080); s.listen((data){}, onDone:(){}, onError:(e){}); print('connected');} @override Widget build(c){ return Scaffold(appBar:AppBar(title:Text('tcp client demo')),body:Column(children:[ SizedBox(height:50), ElevatedButton(child:Text('connect',style:myStyle), onPressed:connect), SizedBox(height:50), ElevatedButton(child:Text('led off',style:myStyle), onPressed:()=>send('LEDON')), SizedBox(height:50), ElevatedButton(child:Text('led on',style:myStyle), onPressed:()=>send('LEDOFF'))]));}}